Go Down

Topic: Centipede Shield (Read 397 times) previous topic - next topic

pop326

Nov 16, 2012, 06:05 pm Last Edit: Nov 16, 2012, 06:16 pm by pop326 Reason: 1
Having trouble using array's with centipede shield.
Is there a max length, for array in arduino.
Using port 0 (chip1), all input (16), in an array. compiler accepts it as valid,
but nothing works.
Code: [Select]

const int buttonPin[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; // pushbutton and limit switch pins
int buttonState = 0;// variable for reading the pushbutton status

Wire.begin(); // start I2C
 CS.initialize(); // set all registers to default
 CS.portMode(0, 0b1111111111111111); // set all pins on chip 1 to input
  CS.portMode(1, 0b1111111111111111); // set all pins on chip 0 to output
  CS.portMode(2, 0b0000000000000000);
  CS.portMode(3, 0b0000000000000000);

CS.pinMode(Relay1, OUTPUT);
  CS.pinMode(Relay2, OUTPUT);
  CS.pinMode(Relay3, OUTPUT);
  CS.pinMode(Relay4, OUTPUT);

CS.pinMode(sensor1, INPUT);
  CS.pinMode(sensor2, INPUT);
  CS.pinMode(sensor3, INPUT);
  CS.pinMode(sensor4, INPUT);

/*..Initialize the pushbutton pins as an input..*/
 int x;
 for(int x = 0; x < 15; x++)
   {
   CS.pinMode(buttonPin[x], INPUT);
   }
int x;
 for(int x = 0; x < 15; x++)
 
     {
     buttonState = CS.digitalRead(buttonPin[x]);
     }

if (buttonState == HIGH && buttonPin[x] == 4)//Up Button press
     {    
     CS.digitalWrite(Relay2, RELAY_ON); //Activate Main Pump UP
     }

//Sensor... 
if (CS.digitalRead(sensor1) == HIGH  && CS.digitalRead(sensor2) == HIGH)
     {
     CS.digitalWrite(Relay1, RELAY_ON);
     }
    else
     {
     CS.digitalWrite(Relay1, RELAY_OFF);
     }




can't figure it out.

codes to large to post all.

Sensors work perfect with relays, buttons-nothing


Thanks

PeterH

If you look at your code you'll see that the forum software has scrambled it. To prevent that you need to enclose it in [ code ] [ /code ] tags. (Just click on the # button when you are composing your message.)

You need to post your whole sketch if we're to have any chance of understanding the problem, and you need to be clear about what it's intended to do and how the actual behaviour differs from that.
I only provide help via the forum - please do not contact me for private consultancy.

robtillaart


What also helps for readability of the code is to press CTRL-T (windows) in the IDE . It auto formats code to consequent style.

If the code is too large to post, try to minimize it to a sketch that has the same "bug". That makes it easier to debug too.

Quote
Is there a max length, for array in arduino.

No, until RAM is up0.

Code: [Select]

/*..Initialize the pushbutton pins as an input..*/
  int x;  <<<< does nothing --> remove it
  for(int x = 0; x < 15; x++) CS.pinMode(buttonPin[x], INPUT);  // simple loops can be on one line


Code: [Select]

  for(int x = 0; x < 15; x++)
  {
    buttonState = CS.digitalRead(buttonPin[x]);
  }

if (buttonState == HIGH && buttonPin[x] == 4)//Up Button press
      {   
      CS.digitalWrite(Relay2, RELAY_ON); //Activate Main Pump UP
      }

//Sensor... 
if (CS.digitalRead(sensor1) == HIGH  && CS.digitalRead(sensor2) == HIGH)
     {
     CS.digitalWrite(Relay1, RELAY_ON);
     }
    else
     {
     CS.digitalWrite(Relay1, RELAY_OFF);
     }


this will not work as buttonstate is overwritten 14 times. so only the state of the last button is remembered
have a look at this
Code: [Select]

  for(int x = 0; x < 15; x++)
  {
    buttonState = CS.digitalRead(buttonPin[x]);

    if ((buttonState == HIGH) && (buttonPin[x] == 4) )   //Up Button press
    {
      //Activate Main Pump UP   
      CS.digitalWrite(Relay2, RELAY_ON);               
    }
  }

Now the test of buttonstate is within the for loop. Every pin is tested and the value of buttonstate is checked f

See the difference?.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

pop326

If I remove
Code: [Select]
int x=0;

I get:
In function 'void loop'():
error: name lookup of 'x' changed for new iso 'for' scoping
error: using obsolete binding at 'x'

HazardsMind

Ok, maybe you need to make it,

Code: [Select]


for(x = 0; x < 15; x++) //NOTE THE CHANGE
  {
    buttonState = CS.digitalRead(buttonPin[x]);

    if ((buttonState == HIGH) && (buttonPin[x] == 4) )   //Up Button press
    {
      //Activate Main Pump UP   
      CS.digitalWrite(Relay2, RELAY_ON);               
    }
  }


declair "int x" outside the function, because I think you might be using "x" in another function, and it might not be able to see the change in "x".

Try that, if it still dont work, post the full code.

Henry_Best



declair "int x" outside the function, because I think you might be using "x" in another function, and it might not be able to see the change in "x".

Try that, if it still dont work, post the full code.


Better still would be to use a meaningful name for the variable, such as 'checkeachbutton'. When returning to code you wrote a year ago, it might give you a clue as to what it does.

pop326

It worked,
I deleted the
int x;
added the extra { } and the extra ( )
and it works beautiful.
Man, electronics today sure are different than in my day,
When you made an AM radio out of a toilet paper roll, some copper wire and a safety pin.
You guys are great,

Thank you very much

pop326

It finally sunk into this old mush melon.
I was using the 'for' loop, for each button statement. Which worked
Then I realized you can put all the buttons in one 'for' loop.
And that works, with less problems.



Go Up