push button mode control

Hello all,

I’m new and excited about Arduino. For my project, I am trying to use three push buttons as a mode control. When a mode is selected I would like the other modes to deactivate and so on. I have been using the example code to get me started. Two problems, how can I deactivate the other two modes when a mode is selected? Also, how can I keep the A and C modes from coming on when the Arduino is powered. Currently, the A and C mode LEDs are on every time the Arduino is powered. Any help is very much appreciated.

JB

mode_select_buttons_abc.ino (4.72 KB)

This is what I have so far. Thanks

/* The circuit: Buttons: * 5V -> 220 ohm resistor -> A Button + -> A Button - -> pin 2 & GND * 5V -> 220 ohm resistor -> B Button + -> B Button - -> pin 4 & GND * 5V -> 220 ohm resistor -> c Button + -> c Button - -> pin 6 & GND LEDs: * 5V -> 220 ohm resistor -> A LED + -> A LED - -> pin 3 ??No Ground?? * 5V -> 220 ohm resistor -> B LED + -> B LED - -> pin 5 * 5V -> 220 ohm resistor -> c LED + -> c LED - -> pin 7 */

// this constant won't change: const int AButton = 2; // the pin that the pushbutton is attached to const int ALed = 3; // the pin that the LED is attached to const int BButton = 4; const int BLed = 5; const int cButton = 6; const int cLed = 13;

// Variables will change: int AButtonPushCounter = 0; // counter for the number of button presses int AButtonState = 0; // current state of the button int ALastButtonState = 0; // previous state of the button int BButtonPushCounter = 0; // counter for the number of button presses int BButtonState = 0; // current state of the button int BLastButtonState = 0; // previous state of the button int cButtonPushCounter = 0; // counter for the number of button presses int cButtonState = 0; // current state of the button int cLastButtonState = 0; // previous state of the button

void setup() { // initialize the button pin as a input: pinMode(AButton, INPUT); pinMode(BButton, INPUT); pinMode(cButton, INPUT); // initialize the LED as an output: pinMode(ALed, OUTPUT); pinMode(BLed, OUTPUT); pinMode(cLed, OUTPUT); // initialize serial communication: Serial.begin(9600); }

void loop() { // read the pushbutton input pin: AButtonState = digitalRead(AButton); delay(50); BButtonState = digitalRead(BButton); delay(50); cButtonState = digitalRead(cButton); delay(50);

// compare the AButtonState to its previous state if (AButtonState != ALastButtonState) { // if the state has changed, increment the counter if (AButtonState == HIGH) {

// if the current state is HIGH then the button // went from off to on: AButtonPushCounter++; Serial.println("A On"); Serial.println("A Button Pushed In"); Serial.print("number of button cycles: "); Serial.println(AButtonPushCounter); } else { // if the current state is LOW then the button // went from on to off: Serial.println("A Button Released"); } } // save the current state as the last state, //for next time through the loop ALastButtonState = AButtonState;

// turns on the LED every other button pushs by // checking the modulo of the button push counter. // the modulo function gives you the remainder of // the division of two numbers: if (AButtonPushCounter % 2 == 0) { digitalWrite(ALed, HIGH); } else { digitalWrite(ALed, LOW); } if (BButtonState != BLastButtonState) { // if the state has changed, increment the counter if (BButtonState == HIGH) {

// if the current state is HIGH then the button // went from off to on: BButtonPushCounter++; Serial.println("B Button Pushed In"); Serial.print("number of button cycles: "); Serial.println(BButtonPushCounter); } else { // if the current state is LOW then the button // wend from on to off: Serial.println("B Button Released"); } } // save the current state as the last state, //for next time through the loop BLastButtonState = BButtonState;

// turns on the LED every other button pushs by // checking the modulo of the button push counter. // the modulo function gives you the remainder of // the division of two numbers: if (BButtonPushCounter % 2 == 0) { digitalWrite(BLed, HIGH); } else { digitalWrite(BLed, LOW); } if (cButtonState !=cLastButtonState) { // if the state has changed, increment the counter if (cButtonState == HIGH) {

// if the current state is HIGH then the button // went from off to on: cButtonPushCounter++; Serial.println("c Button Pushed In"); Serial.print("number of button cycles: "); Serial.println(cButtonPushCounter); } else { // if the current state is LOW then the button // went from on to off: Serial.println("c Button Released"); } } // save the current state as the last state, //for next time through the loop cLastButtonState = cButtonState;

// turns on the LED every other button pushs by // checking the modulo of the button push counter. // the modulo function gives you the remainder of // the division of two numbers: if (cButtonPushCounter % 2 == 0) { digitalWrite(cLed, HIGH); } else { digitalWrite(cLed, LOW); } }

Arduino is powered. Currently, the A and C mode LEDs are on every time the Arduino is powered

make all your outputs low in void setup

still digesting your code, its not the way I would do it, but that doesnt make me right, or you wrong

Those pins are floating if not pressed aren’t they? You need to rework the wiring.

Osgeld, Thank you for your comment. I switched my OUTPUTs to low and the problem has reversed. Mode B now comes on when the Arduino is powered and Mode A and C are off. I am very open to a more efficient way to do things.

Nick, thank you as well. What about doing something like this? I ran out of time to test it. I'll try it when I get home.

GND ------> Button --------> resistor --------> input pin | | +5V ---> resistor --|

do this

  // initialize the button pin as a input:
  pinMode(AButton, INPUT);
  pinMode(BButton, INPUT);
  pinMode(CButton, INPUT);
  digitalWrite(Abutton, HIGH);
  digitalWrite(Bbutton, HIGH);
  digitalWrite(Cbutton, HIGH);

then you can simply do

GND — BUTTON — INPUT PIN

writing HIGH to a pin set as INPUT will turn on an internal pullup resistor

Osgeld,

Thanks for the internal pull up resistor info. Works as advertised and it cleaned up a lot of mess on my breadboard. Any ideas as to why mode A and C come on when the Arduino is powered?