Go Down

Topic: Problem when reading multiple buttons [solved] (Read 773 times) previous topic - next topic

RonaldPoFo

Dec 25, 2012, 06:40 pm Last Edit: Jan 03, 2013, 12:24 pm by RonaldPoFo Reason: 1
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: [Select]

//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 :D

johnwasser

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. :)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

RonaldPoFo

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. :D

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

billroy

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

RonaldPoFo

#4
Dec 25, 2012, 08:07 pm Last Edit: Dec 26, 2012, 04:13 pm by RonaldPoFo Reason: 1
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!!

noidsign

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.

RonaldPoFo

#6
Dec 26, 2012, 01:13 pm Last Edit: Dec 26, 2012, 04:12 pm by RonaldPoFo Reason: 1
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?

RonaldPoFo

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?

billroy

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

RonaldPoFo

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

PaulS

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.

JimboZA


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!
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Go Up