input analog array > output digital array

Hello,
I 'm using arduino uno and I'm trying to build a program, that according to the combination of the 5-analog-pins array that serves as my input, it generates the appropriate output to a 3-digital-pins array.

I'm feeding the analog pins with voltage between 0V and 3.2V and I have the anIn array (analog input).

Then I created another array (which is basically my input as I want it), the In array, and if the 1st pin for example of the analog input array takes a value <512 the corresponding element in the In array, turns LOW. If it takes a value more than 512, the corresponding element in the In array, turns HIGH.

Finally the last array of three elements is my digital output and the outcome depends on the In array. The logic behind the last step is not so hard to explain but I guess you dont need it.

My code doesn't seem to work. I'm kind of new at this and I really need it to work, so any help is much appreciated.

sketch_jul15a.ino (2.2 KB)

To save others the hassle of downloading.

int anIn[] = {A0, A1, A2, A3, A4}; // an array of 5 inputs for the 5 sensors
int In[5];
int Out[] = {8, 9, 10}; // an array of 3 outputs: 1st(8) > turn right , 2nd(9) > turn left , 3rd(10) > forward
int timer = 800;           

void setup() 
  
{
  // set the anIn array as INPUT
  for (int i=0; i < 5; i++) {
    pinMode(anIn[i], INPUT);
  }

  // set the Out array as OUTPUT
  for (int i=0; i < 3; i++) {
    pinMode(Out[i], OUTPUT);
  }

}


void loop() 
{

  for (int i=0; i < 5; i++) {
    analogRead(anIn[i]);
  }
  
  for (int i=0; i < 5; i++) {
    if (anIn[i] < 512)
    {
      In[i] = LOW;
    }
    else
    { 
      In[i] = HIGH;
    }
  }


  if (In[5] == (LOW, LOW, LOW, LOW, LOW))
  {
    digitalWrite (Out[3], (LOW, LOW, LOW));
  }
  else if (In[5] == (LOW, LOW, LOW, HIGH, LOW) ||
           In[5] == (LOW, LOW, LOW, LOW, HIGH) ||
      
           In[5] == (LOW, LOW, LOW, HIGH, HIGH) ||
           In[5] == (LOW, LOW, HIGH, HIGH, LOW) || 
           In[5] == (LOW, LOW, HIGH, LOW, HIGH) ||
           In[5] == (LOW, HIGH, LOW, LOW, HIGH) ||
      
           In[5] == (LOW, LOW, HIGH, HIGH, HIGH) ||
           In[5] == (LOW, HIGH, LOW, HIGH, HIGH) ||
           In[5] == (HIGH, LOW, LOW, HIGH, HIGH) ||
           In[5] == (HIGH, LOW, HIGH, HIGH, LOW) ||
                 
           In[5] == (LOW, HIGH, HIGH, HIGH, HIGH) ||
           In[5] == (HIGH, LOW, HIGH, HIGH, HIGH))
      {
        digitalWrite (Out[3], (HIGH, LOW, HIGH));
      }
  
  else if (In[5] == (HIGH, LOW, LOW, LOW, LOW) ||
           In[5] == (LOW, HIGH, LOW, LOW, LOW)||
      
           In[5] == (HIGH, HIGH, LOW, LOW, LOW) ||
           In[5] == (LOW, HIGH, HIGH, LOW, LOW) || 
           In[5] == (HIGH, LOW, HIGH, LOW, LOW) ||
           In[5] == (HIGH, LOW, LOW, HIGH, LOW) ||
      
           In[5] == (HIGH, HIGH, HIGH, LOW, LOW) ||
           In[5] == (HIGH, HIGH, LOW, HIGH, LOW) ||
           In[5] == (HIGH, HIGH, LOW, LOW, HIGH) ||
           In[5] == (LOW, HIGH, HIGH, LOW, HIGH) ||
                 
           In[5] == (HIGH, HIGH, HIGH, HIGH, LOW) ||
           In[5] == (HIGH, HIGH, HIGH, LOW, HIGH))
  {
    digitalWrite (Out[3], (LOW, HIGH, HIGH));
  }
  else 
  {
    digitalWrite (Out[3], (LOW, LOW, HIGH));    
  }

    delay(timer);
}
  if (In[5] == (LOW, LOW, LOW, LOW, LOW))

A five element array has no index 5.
Anyway, you can't compare entire arrays like that, or assign to them like this     digitalWrite (Out[3], (LOW, LOW, HIGH)); .

for what it's worth I tried to include my code to the post the same way you did, but I didn't find the option so I just attached it. thank's for that and for the answer :slight_smile:

  for (int i=0; i < 5; i++) {
    if (anIn[i] < 512)

You can pretty much be guaranteed that A0, A1, A2, A3, A4 will always be less than 512.
Did you mean to record the analogue readings you took, or was that just an approximate surrogate for "delayMicroseconds (500)"?

No I mean to record the analog readings.
If I'm not mistaken they should be betwwen 0 and 1023. So I'm basically "asking" if the voltage is lower or higher than 2.5 V

So I'm basically "asking" if the voltage is lower or higher than 2.5 V

Where? The analogRead() function is used to ask the pin for a value.

  for (int i=0; i < 5; i++) {
    analogRead(anIn[i]);
  }

You're reading the 0...1023 analogue value, and chucking it away, hence my comment about the delayMicroseconds

PaulS I meant that generally that is what I want to know so that I can create the second array

if (anIn < 512)

  • {*
    _ In = LOW;_
    * }*
    * else*
    * {*
    _ In = HIGH;
    * }*
    AWOL I don't unerstand how I am chucking it away :/. I think I'm asking it to read the value and then use it to create the second array_

I think I'm asking it to read the value and then use it to create the second array

The value that analogRead() returns goes where?

   int val = analogRead(pinArray[someIndex]); // Store the result in a variable
   if(val > 512)
      someOtherArray[someOtherIndex] = 1;
   else
      someOtherArray[someOtherIndex] = 1;

Stratis:
AWOL I don't unerstand how I am chucking it away :/. I think I'm asking it to read the value and then use it to create the second array

Where is the second array you imagine you're creating?

PaulS
ok. I get what you re saying.
I suppose since I want to read an array, "val" in your example, should be another array?

AWOL
the second array is the In array with values LOW and HIGH.

for (int i=0; i < 5; i++) {
if (anIn < 512)

  • {*
    _ In = LOW;_
    * }*
    * else*
    * {*
    _ In = HIGH;
    * }
    }*
    I guess I'm not doing it right. And I guess you're saying the same thing with PaulS, right?_
int anIn[] = {A0, A1, A2, A3, A4};

A0 typically has the value 14, A1 is 15, A2 is 16 and so on.

In my experience, these are always less than 512.

Would that be correct?

for (int i=0; i < 5; i++) {
   if (analogRead(anIn[i]) < 512)
   {
     In[i] = LOW;
   }
   else
   { 
     In[i] = HIGH;
   }
 }

And AWOL if I put a 4V signal in an analog input, shouldn't it read around 818?

Yes, reading the pin is good.
Yes, 4 volts DC will give a reading around 820, assuming a reference voltage of 5V

Use better names. All variables and functions are reduced to memory addresses when the code is compiled, so verbosity is free. There is no need to be terse.

anIn is a meaningless name. Since it's storing pin numbers, call it something like analogSensorPins. In and Out are similarly meaningless. If you ever go back to this code after several months of never looking at it, you'll have no clue what's doing what.