incrementarduino-unoarduino-ide

How to increment Values with buttons on Arduino UNO


I'm doing a project for uni using an Arduino and part of it involves making a a sort of menu where a pushbutton (or pushbuttons) are used to switch between cases. Each case has a specific LED bound to it so the user knows what option they have selected, the code can be seen below. What ends up happening is all the LEDS turn on instead of just one at at a time and the button ends up doing nothing. I can turn on individual LEDS by changing the selection value and I've run the same button set up on other code so its not a hardware issue. I'm not amazing at programming but it looks like it should work so am I missing something really simple?

`
#define buttonPin A3 // Input Push Button

int selection = 0; // value used for selection 

void setup() {
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {

if(buttonPin, HIGH){
       selection++;
    }

  switch(selection){

    case 0:
      digitalWrite(2,HIGH);
    break;

    case 1:
      digitalWrite(3,HIGH);
    break;
    
     case 2:
      digitalWrite(4,HIGH);
    break;

     case 3:
      digitalWrite(5,HIGH);
    break;

     case 4:
      digitalWrite(6,HIGH);
    break;
   
    }

    

}
`

Solution

  • Where you increase the selection you should add a test for maximum and decide there if to reverse the order (selection--) or to start from zero.

    Also, you need to turn off all non selected leds.

    [EDIT]

    Looking at your edited code there are few issues I would like to point:

    1. The debouncer blocks your code, check out millis() as a way to count time while doing other tasks.

    2. I always PULLUP my inputs and check for LOW state to determine a pushed button (there are tons of discussions about that so I will not go into details).

    3. As shown below, using an array for the led pins will simplify your code.

    ....

    const int buttonPin_up = 7;
    const int buttonPin_down = 8;
    int selection = 0; // value used for selection
    
    // Using an array will simplify the program and will allow you to select any pin
    // even if it's not in a sequence of numbers (and still use a FOR loop to handle it)
    int led[] = {2,3,4,5,6};
    
    // Get the number of elements in the array
    const int numOfLeds = sizeof(led) / sizeof(led[0]);
    
    void setup() {
      pinMode(buttonPin_up, INPUT);
      pinMode(buttonPin_down, INPUT);
    
      for (int i=0; i<numOfLeds; i++) {
        pinMode(led[i], OUTPUT);
      }
    }
    
    void loop() {
      if (digitalRead(buttonPin_up) == HIGH) {
        selection += 1;
        delay(750);
      }
      if (digitalRead(buttonPin_down) == HIGH) {
        selection -= 1;
        delay(750);
      }
      
      if (selection > 4) {
        selection = 0;
      }
      
      if (selection < 0) {
        selection = 4;
      }
    
      for (int i=0; i<numOfLeds; i++) {
        bool state = LOW;
        if (i == selection) state = HIGH;
        digitalWrite(i, state);
      }
    }
    

    ....