This project is for work and I'm extremely stuck. Here is a brief synopsis: I have an Arduino Nano connected to another controller that will output digital high/low to the Arduino. The Arduino needs to interpret those signals and store them in an array, while also converting them into a simple number. For example, 0001 is 1, 0010 is 2, 0011 is 3, etc. I want it to ignore 0000.
Can anyone explain a way to possibly do this? I put the code I have so far below. I've done quite a bit of searching and I've not seen any examples or previous attempts like this, which seems crazy because I'd think this was something people do fairly often.
#include <SPI.h>
int input_array[] = {A1, A2, A3, A4}; //trying to store the inputs as an array.
void setup() {
for(int i = 0; i < input_count; i++){ //sets all the analog pins 1 through 4 as digital inputs.
pinMode(input_array[i], INPUT);
}
//The following for loop should read the array holding the values for the input commands.
for(int a = 0; a < 4; a++)
{
int new_input_state[a] = digitalRead(input_array[a]);
int current_input_state[a] = new_input_state[a]; //current state will be considered the old state after an input change later on.
}
}
/*
0000=needs to be ignored. The default state of the inputs will be 0000.
0001= 1 through 1000 = 8
*/
void loop()
{ //The first part of the loop is meant to recognize the state change of the inputs.
if(input_arrary[a] != current_input_state[a])
{
//take the new input and convert it to a number from binary
//update the current_input_state[a] from the new input_array[a] data.
//save the new data to the SD card
//tell the bench the state has been changed
}
for(int a = 0; a < 4; a++)
{
int new_input_state[a] = digitalRead(input_array[a]);
int current_input_state[a] = new_input_state[a]; //current state will be considered the old state after an input change later on.
}
That code is in setup() so it is only executed one time during startup. Then values are never updated in loop().
The Serial port is your best troubleshooting tool. Use serial prints to monitor the progress of the program and the values of variables.
Thank you both for your replies. But my issue isn't with the setup, my issue is as I stated: I'm trying to convert 4 digital inputs into a single number integer. As in, if I got 0100, the code would recognize that is a 4. This is what I'm unsure of, but I will be sure to move my for loop from the setup so that it continues to detect changes.
This may help for a beginning, assuming you're reading digital values. I changed your pins for digital ones, but using A* should be OK.
#include <SPI.h>
#define NUMBER 4
int input_pin[NUMBER] = {2, 3, 4, 5};
byte current_state[NUMBER] = {0};
void setup() {
Serial.begin(115200);
for (int i = 0; i < NUMBER; i++) pinMode(input_pin[i], INPUT);
}
/*
0000=needs to be ignored. The default state of the inputs will be 0000.
0001= 1 through 1000 = 8
*/
void loop() {
int value = 0;
//The first part of the loop is meant to recognize the state change of the inputs.
for (int a = 0; a < 4; a++) {
current_state[a] = digitalRead(input_pin[a]);
value = value * 2 + current_state[a];
//save the new data to the SD card
//tell the bench the state has been changed
}
Serial.print ("Global state : ");
Serial.println(value);
delay(1000);
}
One read per second to have some time to see the result. Of course, maybe you don't need that delay.
I'm on the newer end of things, so take this with a grain of salt.
But conceptually I'm really confused by the premise of your project.
Here's what I mean: how will your nano interpret two "on" signals (1's) as two distinct inbound on/high signals, as opposed to just a stead state of the first on/high signal. In other words, don't you need some kind of timing or clock element here?
Let's say you're trying to send 0001. Won't your loop just quickly read the through the first sent low/off/0 state as 4 0s, because the signal is off/low for some duration presumably longer than the duration of 4 loop iterations?
So, that's a good point Steve20016. How about this...instead of me putting up the butchered snippets of code and praying someone understands my question, I'll put everything down in a pseudo code format. Hopefully then what I'm trying to accomplish will be more clear.
/*
*Arduino listens for input changes.
*Inputs come from the four different binary 1/0 inputs.
*There is a saved "state" of the inputs that will be used as a reference for input changes.
*An if loop compares the current state with the saved state of the input array.
*Since each value in the array is checked 1 at a time, then if there is a change detected, then
there will be a delay executed. This delay should allow the full update of the inputs.
*Example: Saved input is 0010. A new input of 0001 is sent. The array would see a 1 changed to a zero,
and then the delay would execute. This delay allows the inputs to be fully changed, then the state
of the input array is read again, and set equal to the saved state.
*AFter the input saved state has been updated.
*We take the new input array stored values and determine what relay switch is to be opened. (Ideally,
I had thought it would be good to convert the input into a simple integer number that could be associated
with a desired function that could be called up from an array of values.)
*Example: The new Saved_State[] gets converted through *binary magic* and outputs Number value "N". So now, execute function_array[N].
*After the function has been executed, the Nano sends a response to the mechanism providing the inputs that the change has been completed
and that it can accept new inputs.
*/