Learning Arrays Question

Hi everyone.

I am trying to learn how to use and incorporate arrays into my code, but I don’t think I have a 100% handle on it yet.

The following is an array that I have been trying to write, but I am not getting results that I would suspect.

I set up the array pin numbers

in void set up I set all pins in array to OUTPUTS
and use a digitalWrite to set all pins HIGH

I then have a function to load the Outlet_Screen and call the Outlet_Status function.

The Outlet_Status is used to digitalRead the array, and then draw a fillCircle either green or red dependent upon whether the pin is HIGH or LOW.

The results that I would expect is when I navigate to the Outlet Screen all fillCircles would be drawn green since they are all set HIGH in void setup().

However, I am getting no results at all. No fillCircles are being drawn and screen remains blank.
I have tried it various ways, but have not yet figured out my issue.

Any help locating where I have gone wrong would be appreciated. Thank you!

int OUTLETS[8] ={A8,A9,A10,A11,A12,A13,A14,A15};                    //set the pins in the array

void setup(){
for (int on=0; on<7; on++) pinMode(OUTLETS[on], OUTPUT);        //array sets outlet box pins as outputs
 
for (int on=0; on<7; on++) digitalWrite(OUTLETS[on], HIGH);       //array sets outlet pins HIGH/ON
}

 
void Outlet_Screen(){
     
   Outlet_Status();                                                          
   
   myGLCD.loadBitmap(0,0,240,252, "OUTLETS.raw");       //loads outlet screen
   
   myGLCD.setColor(0,0,255);
   myGLCD.drawRoundRect(8,309,231,255);                      //Draw Clock Button   
 }


 void Outlet_Status(){                      //Function indicates power status with Green or Red light
   if (outlet_screen == true){
   for (int on=0; on<7; on++) digitalRead(OUTLETS[on]);
   
   if (OUTLETS[0] == HIGH){
   myGLCD.setColor(0,255,0);           //outlets are default "ON"  
   myGLCD.fillCircle(101,51,11);       //Green Fill Circles will indicate this 
   }
   else if (OUTLETS[0] == LOW){
   myGLCD.setColor(255,0,0);           //outlet "OFF"  
   myGLCD.fillCircle(101,51,11);       //Red Fill Circle will indicate this
   }
   
   if (OUTLETS[1] == HIGH){
   myGLCD.setColor(0,255,0);
   myGLCD.fillCircle(101,94,11);
   }
   else if (OUTLETS[1] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(101,94,11); 
   }

   if (OUTLETS[2] == HIGH){
   myGLCD.setColor(0,255,0);
   myGLCD.fillCircle(101,137,11);
   }   
   else if (OUTLETS[2] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(101,137,11); 
   }
   
   if (OUTLETS[3] == HIGH){
   myGLCD.setColor(0,255,0);
   myGLCD.fillCircle(101,181,11);
   }
   else if (OUTLETS[3] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(101,181,11); 
   } 
 
   if (OUTLETS[4] == HIGH){
   myGLCD.setColor(0,255,0);  
   myGLCD.fillCircle(222,51,11);
   }
   else if (OUTLETS[4] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(222,51,11); 
   }
 
   if (OUTLETS[5] == HIGH){
   myGLCD.setColor(0,255,0);    
   myGLCD.fillCircle(222,95,11);
   }
   else if (OUTLETS[5] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(222,95,11);
   }
   
   if (OUTLETS[6] == HIGH){
   myGLCD.setColor(0,255,0);   
   myGLCD.fillCircle(222,137,11);
   }
   else if (OUTLETS[6] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(222,137,11); 
   }
   if (OUTLETS[7] == HIGH){
   myGLCD.setColor(0,255,0);   
   myGLCD.fillCircle(222,181,11);
   }
   else if (OUTLETS[7] == LOW){
   myGLCD.setColor(255,0,0);
   myGLCD.fillCircle(222,181,11); 
   }  
 } 
}


void loop(){


      if (settings_screen == true){
        
        if((y<=166) && (y>=121)){
        if((x<=222) &&(x>=177)){
          waitForIt(177,121,222,166);
          x=0;
          y=0;
          settings_screen = false;
          outlet_screen = true;
          Outlet_Screen();
        }
       }
      }
for (int on=0; on<7; on++)

There are eight elements in your array - did you mean to miss off the last one?

No. I didn't mean to leave it off.

I had originally set it to 8, however when it didn't work the first time I had changed it to 7. My thinking was that the first index was zero[0].

The first index is zero, and the last index of an eight element array is seven.

for (int on=0; on<7; on++) digitalRead(OUTLETS[on]);

Read the pin and discard the result.
Not good.

I have changed it back to 8. I started to confuse myself when I was trying to manipulate the code to get the desired result.

Besides that error the arrays seem proper??

Perhaps I should be looking in my status function with a problem in the if & else if statements.

if (OUTLETS[0] == HIGH){

Does it seem likely that A0 has the same value as HIGH (1)?

Read the pin and discard the result.
Not good.

Ah. Ok. I guess I don't understand how it is discarding the result.

Should I remove that for loop, and digitalRead each?

I was trying to be efficient

AWOL:

if (OUTLETS[0] == HIGH){

Does it seem likely that A0 has the same value as HIGH (1)?

I couldn't be sure without completely guessing.

When you say HIGH(1) are you referring to HIGH(1) and LOW would then be LOW(0)?

OUTLETS [0] has the value A0.
That is never ever going to be equal to one.

I guess I don't understand how it is discarding the result.

OK, put it this way - where do you think the value returned by the digitalRead is going to end up?

AWOL:
OUTLETS [0] has the value A0.
That is never ever going to be equal to one.

I guess I don't understand how it is discarding the result.

OK, put it this way - where do you think the value returned by the digitalRead is going to end up?

I think I'm starting to see. I need to store or direct the read?

something like:

outletState = digitalRead(OUTLETS[on]);

where outletState is where the digitalRead ends up?

something like:

outletState = digitalRead(OUTLETS[on]);

If you call that in a loop, outletState will contain the state of the last pin in the array. You need an array of states, too.

outletState[on] = digitalRead(OUTLETS[on]);

PaulS:

something like:

outletState = digitalRead(OUTLETS[on]);

If you call that in a loop, outletState will contain the state of the last pin in the array. You need an array of states, too.

outletState[on] = digitalRead(OUTLETS[on]);

Thank you both so much! It's starting to sink in.

I've been making some progress, and have it producing the results I would like. However, the code is not that efficient. Before you clued me in on needing an outletState array as well I wrote a digitalRead of every index = to outletState

Example:

outletState = digitalRead(OUTLETS[0]);
if (outletState == HIGH){
....
else if (outletState == LOW){
....

and then to the next if & else if

outletState = digitalRead(OUTLETS[1]);

and so on through the index.

Now I can try slimming it down a bit.

Thanks to you both!!