Using 2 buttons to move through multiple function/modes in Arduino

Using 2 buttons to move through multiple function/modes in Arduino

 

Description

Many times there are resource constraints and it is required to have fewer buttons but these buttons should have multiple functions, so to tackle this problem I built a logic which uses 2 buttons to move back and forth between various modes. This program is implemented in Arduino ide and is tested on Arduino UNO but it should work fine on most of the Arduino boards, with little tweaks it should work with other microcontrollers as well. You can find my code below:  


//Using 2 buttons to move through various modes using arduino

const PROGMEM int Mode1Select = 2;                   //Button to increment the mode
const PROGMEM int Mode2Select = 3;                   //Button to decrement the mode


int ChPushCounter = 0;   // counter for the number of button presses
int ChIncState = 0;         // current state of the button
int ChDecState = 0;         // current state of the button
int lastChIncState = 1;     // previous state of the button
int lastChDecState = 1;     // previous state of the button

void setup() {
  pinMode(Mode1Select, INPUT_PULLUP);
  pinMode(Mode2Select, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  ChannelSelectFunction();
}


//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Channel Select Function
void ChannelSelectFunction() {
  ChIncState = digitalRead(Mode1Select);

  // compare the buttonState to its previous state
  if (ChIncState != lastChIncState) {
    // if the state has changed, increment the counter
    if (ChIncState == LOW && ChPushCounter <= 3) {
      // if the current state is HIGH then the button went from off to on:
      ChPushCounter++;
    }
    else if (ChIncState == LOW && ChPushCounter == 4) {
      // if the current state is HIGH then the button went from off to on:
      ChPushCounter = 1;
    }
    // save the current state as the last state, for next time through the loop
    lastChIncState = ChIncState;
    if (ChPushCounter == 1 && ChIncState == LOW) {
      Serial.println("Mode increment 1");
      delay(100);
    }
    else if (ChPushCounter == 2 && ChIncState == LOW) {
      Serial.println("Mode increment 2");
      delay(100);
    }

    else if (ChPushCounter == 3 && ChIncState == LOW) {
      Serial.println("Mode increment 3");
      delay(100);
    }
    else if (ChPushCounter == 4 && ChIncState == LOW) {
      Serial.println("Mode increment 4");
      delay(100);
    }
  }

  // For decreasing the channel
  ChDecState = digitalRead(Mode2Select);

  // compare the buttonState to its previous state
  if (ChDecState != lastChDecState) {
    // if the state has changed, increment the counter
    if (ChDecState == LOW && ChPushCounter >= 2) {
      // if the current state is HIGH then the button went from off to on:
      ChPushCounter--;
    }
    else if (ChDecState == LOW && ChPushCounter == 1) {
      // if the current state is HIGH then the button went from off to on:
      ChPushCounter = 4;
    }
    // save the current state as the last state, for next time through the loop
    lastChDecState = ChDecState;
    if (ChPushCounter == 1 && ChDecState == LOW) {
      Serial.println("Mode decrement 1");
      delay(100);
    }
    else if (ChPushCounter == 2 && ChDecState == LOW) {
      Serial.println("Mode decrement 2");
      delay(100);
    }

    else if (ChPushCounter == 3 && ChDecState == LOW) {
      Serial.println("Mode decrement 3");
      delay(100);
    }
    else if (ChPushCounter == 4 && ChDecState == LOW) {
      Serial.println("Mode decrement 4");
      delay(100);
    }
  }


}



Please do share if you have any interesting ideas to modify the code and the applications where you used this.
Thank you😉

Adityapratap Singh

Post a Comment