How: Combinations Of Push Buttons Call Different Functions

Hi Tom,

I tried some of what you talked about here. I have two questions. First, how do I most effectively have the Arduino try adding and checking who is high? IE, if either of my buttons are pushed, see if which ones are pushed. I tried using this:

if (digitalRead(TopButton) || digitalRead(MidButton) == LOW)

It seemed unreliable. For some reason, whichever digitalRead in comes first seems to not always trigger the function. The full sketch can be found below. Similarly, I seem to have an unreliable system going with the not changing when you accidentally release one button first problem. I tried doing this:

 if (digitalRead(TopButton) == HIGH  && digitalRead(MidButton) != HIGH) //some lame attempt at making things not change back on button release. Not so effective.
        {
          delay(1000);
        }

But it proved to be a tad bit buggy. I am pondering how to debounce with this setup. Perhaps I need to take a far different approach?

Thanks again!

Here's the code!

const int TopButton = 5;    // Button A - For Top Out
const int TopLed = 12;      // LED A, When 1 button pushed, this is corrisponds to A.
const int MidButton = 6;    //"" But B
const int MidLed = 11;  // ""But B
byte Stateread = 0;// Keeps track of buttons pushed

void A()
{
  digitalWrite(TopLed, HIGH);
  digitalWrite(MidLed, LOW);

}


void AB()
{
  digitalWrite(TopLed, HIGH);
  digitalWrite(MidLed, HIGH);

}


void B() { 
  digitalWrite(TopLed, LOW);
  digitalWrite(MidLed, HIGH);

}

void setup() {
  pinMode(TopButton, INPUT_PULLUP);
  pinMode(TopLed, OUTPUT);
  pinMode(MidButton, INPUT_PULLUP);
  pinMode(MidLed, OUTPUT);
  Serial.begin(9600);
  AB();

}

void loop() {

  if (digitalRead(TopButton) || digitalRead(MidButton) == LOW) {   
    Stateread = 0;
    if (digitalRead(TopButton) == LOW) {  //If A reads HIGH, then add 2 to the Stateread variable
      Stateread = Stateread + 2;
    }
    if (digitalRead(MidButton) == LOW) {  //If B reads HIGH, then add 4 to the Stateread variable
      Stateread = Stateread + 4;
    }

    // do something different depending on the range value:
    switch (Stateread) {
      case 2:    // your hand is on the sensor
        Serial.println("TOP");
        A();
        break;
      case 4:    // your hand is close to the sensor
        Serial.println("MID");
        B();
        break;
      case 6:    // your hand is a few inches from the sensor
        Serial.println("BOTH");
        AB();
        if (digitalRead(TopButton) == HIGH  && digitalRead(MidButton) != HIGH) //some lame attempt at making things not change back on button release. Not so effective.
        {
          delay(1000);
        }
        break;

    }
    delay(10);  // delay in between reads for stability
  }
}

Marty