Pages: [1]   Go Down
Author Topic: Problem when reading multiple buttons [solved]  (Read 579 times)
0 Members and 1 Guest are viewing this topic.
Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following code is supposed to Serial.print the state(0, 1) of 10 buttons every time there is a change in their state.
To do so I create two arrays which are compared to detect a change in State. The first array contains the states of the buttons when they were last read, and the second contains the new button States. It works for all the buttons except for the last (allButtons[9]) where it will print the states endlessly until I let go.
Code:
//Real Variables
const int floors = 5;                                                    //number of floor the elevator travels to
const int functButtons = 2;                                              //numer of function buttons in the cabin (all buttons except floors)
const int menuButtons = 3;//number of buttons the device contains to control the menu

//Total amount of
const int totalButtons = functButtons + floors + menuButtons;            //Total amount of Buttons

//Pins assigned to Buttons
const int numButton[] = {4, 5, 6, 7, 8};                                 //pins assigned to each button in ascending order
const int functButton[] = {2, 3};                                        //pins assigned to each function button in order
const int menuButton[] = {9, 10, 11};                                    //pins assigned to each menu button in order
int allButtons[totalButtons];

//Arrays for LED and Button state
boolean buttonStates[totalButtons];                                      //holds state of buttons
boolean buttonMem1[totalButtons];
boolean buttonMem2[totalButtons];

void setup() {
  Serial.begin (9600);                                                   //Start Serial connection

  //Declare Button pins as INPUT
  for (int i = 0; i < floors; i++) {                                     //Declare numButtons as INPUT
    pinMode(numButton[i], INPUT);
  }
  for (int i = 0; i < functButtons; i++) {                               //Declare functButtons as INPUT
    pinMode(functButton[i], INPUT);
  }
  for (int i = 0; i < menuButtons; i++) {                                //Declare menuButtons as INPUT
    pinMode(menuButton[i], INPUT);
  }
  
  //compose allButtons[] adding numButton[], functButton[] and menuButton[] info
  for (int i = 0; i < floors; i++) {
    allButtons[i] = numButton[i];
  }
  for (int i = floors; i < (floors + functButtons); i++) {
    allButtons[i] = functButton[i - floors];
  }
  for (int i = (floors + functButtons); i < (floors + functButtons + menuButtons); i++) {
    allButtons[i] = menuButton[i - (floors + functButtons)];
  }
}

void loop() {
  if (ButtonChange() == HIGH) {
    for (int i = 0; i < totalButtons; i++) {
        Serial.print(i);
        Serial.print("\t");
        Serial.println(buttonStates[i]);
      }
    Serial.println("");
  }
}

void ScanButtons() {
  for (int i = 0; i < totalButtons; i++) {
    buttonStates[i] = digitalRead(allButtons[i]);
  }
}

boolean ButtonChange() {
  for (int i = 0; i < totalButtons; i++) {
    buttonMem1[i] = buttonStates[i];
  }
  ScanButtons();
  for (int i = 0; i < totalButtons; i++) {
    buttonMem2[i] = buttonStates[i];
  }
  for (int i = 0; i < totalButtons; i++) {
    if (buttonMem1[i] != buttonMem2[i]) {
      return HIGH;
    }
  }
}

Thanks in advance for your time and help smiley-grin
« Last Edit: January 03, 2013, 06:24:23 am by RonaldPoFo » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 error: 'totalButtons' was not declared in this scope

You seem to be missing the line that defines "totalButtons".

I added that at the top and it complained about the "ScanButtons();" line:
error: expected constructor, destructor, or type conversion before ';' token

Please post code that at least compiles. smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about that johnwasser, the code is part of a larger program and I treid make it easier to read. I've corrected it now and made sure it compiles and runs properly, well, except for my problem. smiley-grin

Well, won't make that mistake again, thanks johmwasser!
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This may not be the bug you're looking for, but ButtonChange should probably return something falsey when it finds no button has changed.

-br
Logged

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This may not be the bug you're looking for, but ButtonChange should probably return something falsey when it finds no button has changed.

-br
YESS, that was it!! Thanks a lot!!
« Last Edit: December 26, 2012, 10:13:29 am by RonaldPoFo » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi RonaldPoFo,

I am trying a similar logic (though simpler) of what you have been doing.
Could you please post the fixed code? Thank you.
Logged

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am trying a similar logic (though simpler) of what you have been doing.
Could you please post the fixed code? Thank you.
What do you mean by fixed code?
« Last Edit: December 26, 2012, 10:12:27 am by RonaldPoFo » Logged

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been studying the solution and don't get why it worked properly with all buttons except 9, the last. Do functions maintain their value after being ended or go back to nul?? Why did only button 9 loop?
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can't say exactly, but consider this: the calling function expects to find a return value on the stack after the called function returns, and it is going to use whatever is there.  You didn't put anything there, so the calling function was seeing random stack garbage.  Perhaps that garbage was different in the '9' case somehow.

When things get non-deterministic in C like this, they can go off the rails in weird ways very fast.  You are fortunate to have had a recognizable and consistent symptom, if this was in fact the bug.  Sometimes it just goes out to lunch and hangs and you are left scratching your head.

It's surprising you didn't get a warning from the compiler about the missing return statement.  It might be worth putting things back for a moment and scrutinizing the compiler output to see if it tried to help warn you.

-br
Logged

Barcelona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Indeed
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope, no message from compiler. Well, thanks for the explanation. Off to the next bug!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's surprising you didn't get a warning from the compiler about the missing return statement.
The compiler generated a warning. The Arduino core code is so full of warnings that the Arduino team simply suppresses all warnings (instead of fixing the problems). So, you simply didn't get to see the warning that the compiler did generate.
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Off to the next bug!!

Well as they say, if de-bugging is the business of taking errors out of code, then programming must be the business of putting the errors in!
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Pages: [1]   Go Up
Jump to: