Pages: [1]   Go Down
Author Topic: Centipede Shield  (Read 391 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: November 16, 2012, 12:16:54 pm by pop326 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14024
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
/*..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:
  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:
  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?.

Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I remove
Code:
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'
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3785
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, maybe you need to make it,

Code:

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.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

London
Offline Offline
Edison Member
*
Karma: 48
Posts: 1526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Pages: [1]   Go Up
Jump to: