Problems with two buttons and 5 Leds

Hi! I'm quite new to Arduino and have been having some programming problems lately for a personal project. I have two pushbuttons and 5 LEDs and I want to be able to turn ON or OFF the LEDs based on the number of pushes saved in the code.

What I want my code to say is:
Push button1 ++;
Push Button2 --;
If pushes = 1, LED1 On and others off
and so on until 5. If I reach 5 or more pushes the 5 LEDs are ON, if I get to 0 all LEDs are OFF. I don't know if I explained myself quite well haha.

I have checked simpler codes to see if my circuit was working right and it does, and I tried tearing the code a part to see what parts are wrong but I can't seem to figure it out :frowning: and I'm running out of options and examples to use. Here is what I have written until now:

const int button1 = 2;      // increasing button
const int button2 = 3;      // decreasing button
const int led1 =  11;
const int led2 =  10;
const int led3 =  9;
const int led4 =  6;
const int led5 =  5;         // defining LEDs


int buttonPushCounter = 0;  //pushcounter to know number of pushes after each loop
int buttonState = 0;
int lastButtonState = 0;    //what was the last thing done with the buttons
int buttonState1 = 0;       //to check what has button1 done
int buttonState2 = 0;       //to check what has button2 done
// unsigned long currentMillis = 0;   // put these to make it work with millis() after making it work with delay



void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);       //all the LEDS

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);     //the 2 buttons
  // currentMillis= millis();  //put these to make it work with millis() after making it work with delay
  Serial.begin(9600);          //to debug
}


void loop() {
  buttonState1 = digitalRead(button1); // read state of button1
  buttonState2 = digitalRead(button2); // read state of button2

  if (buttonState != lastButtonState) {      //check if button1 and button 2 have been pressed
    if (buttonState1 == HIGH && buttonState2 == LOW)
    {
      buttonPushCounter ++;                           //if button1 has been pressed but not button2 +1 in counter
    }

    else if (buttonState2 == HIGH && buttonState1 == LOW)
    {
      buttonPushCounter --;                             //if button2 has been pressed but not button1 -1 in counter
    }

    else
    {
      delay(10);                                      //if something else do nothing
    }

  }

  lastButtonState = buttonState;                  // save button state so I can now amount of leds to turn on
  controlled ();                                  // run control LED void to know what LEDS to turn on
  delay (100);// currentMillis= millis();

}


void controlled() {                              //void with code for on or off leds

  if ( buttonPushCounter == 1  )                //if 1  was the pushcounter result turn LED1 ON
  {
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led1, HIGH);
  }


  else if (buttonPushCounter == 2)             //if 2  was the pushcounter result turn LED1 and LED2 ON
  {
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, HIGH);
  }


  else if (buttonPushCounter == 3)         //if 3  was the pushcounter result turn LED1 , LED2 and LED3 ON
  {
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, HIGH);
  }


  else if (buttonPushCounter == 4)       //if 4  was the pushcounter result turn LED1, LED2 , LED3 and LED4 ON
  {
    digitalWrite(led5, LOW);
    digitalWrite(led4, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, HIGH);
  }

  else if (buttonPushCounter >= 5)        //if 5 or more was the pushcounter result turn all LEDs ON
  {
    digitalWrite(led5, HIGH);
    digitalWrite(led4, HIGH);
    digitalWrite(led3, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led1, HIGH);
  }

  else                                    //if the pushcounter result is 0 turn all LEDs off
  {
    digitalWrite(led5, LOW);
    digitalWrite(led4, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led1, LOW);
  }

}                                       //end of code

I have also been trying to use millis() instead of delay because I have read it is better to use millis() than delay, but have been quite unable to get it to work on the code.
All help will be appreciated!!

What doesn't happen that you want to happen? What happens that you don't want to happen?

It doesn't work :frowning:
It does nothing at all.

Have you tested the buttons and LEDs with a simplified sketch to make sure they work?

How are the input buttons wired ?

Yes, I used simpler codes to test the buttons and leds to see if there was any trouble but they worked fine, I'll post now a sketch of my circuit to show you.

Your code formatting is a mess. I'm not going to walk through it, please fix it and repost.

Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

@aarg was quicker ...

but once more

  1. Clean up your code with CTRL(T) to see what belongs to what
  2. Your code is not easily readable - better use variable names which are better "self-explaining"
  3. Delete all lines which you pasted from wherever and have no function in your code
  4. Comment your code (what is this line doing etc.)
  5. I suspect you also have debouncing problems -> study a bit about how to debounce your buttons.
  6. Then start with Serial.print() commands to see if your buttons are really not bouncing anymore (one click -> one reaction)
  7. Replace most of your if/else statements with switch/case (e.g. in your controlint() function)
  8. Start replacing the Serial.print() statements with digitalWrite() to swich on/off your LEDs

okay sorry for the mess, I reposted it with comments and cleared the useless bits.
I have looked for debouncing but don't quite understand it, I'll look into it more and try it in my code.
What do you mean switch and case? I have read a bit about them but don't understand how to implement them or how they work.

Thank you for all your help.

controlled() is a terrible name for a function that controls LEDs, because "controlled" is a word in the English language. But thanks for the rest of the effort. It helps a lot.

One end of one of your resistors is not connected to anything. +5V and ground from the Arduino are shorted together.

There is no mechanism for buttonstate to change, because it has no connection with the input button variables. So the code inside the if block that checks buttonstate is always skipped.

Here is an example with switch case as part of your code:

  void controlled() {                              //void with code for on or off leds

    switch (buttonPushCounter) {
      case 1:                                        //if 1  was the pushcounter result turn LED1 ON
        digitalWrite(led5, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led1, HIGH);
        break;
        
      case 2:                                  //if 2  was the pushcounter result turn LED1 and LED2 ON
        digitalWrite(led5, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led2, HIGH);
        digitalWrite(led1, HIGH);
        break;

      // further case statements

      default:
        // statement (or empty)
        break;
    }

  }

It doesn't work at all because you never update buttonState and so if (buttonState != lastButtonState) never returns true.

Here are the relevant bits fixed.

int buttonPushCounter = 0;  //pushcounter to know number of pushes after each loop
int buttonState1 = 0;       //to check what has button1 done
int buttonState2 = 0;       //to check what has button2 done
int lastButtonState1 = 0;       //to check what has button1 done
int lastButtonState2 = 0;       //to check what has button2 done

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);       //all the LEDS

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);     //the 2 buttons
  // currentMillis= millis();  //put these to make it work with millis() after making it work with delay
  Serial.begin(9600);          //to debug
}


void loop() {
  lastButtonState1 = buttonState1;
  lastButtonState2 = buttonState2;

  buttonState1 = digitalRead(button1); // read state of button1
  buttonState2 = digitalRead(button2); // read state of button2

  if(lastButtonState1 == HIGH && buttonState1 == LOW && buttonState2 == HIGH ) 
  {
    buttonPushCounter ++;                           //if button1 has been pressed but not button2 +1 in counter
    delay(10); // debounce
  }

  if (lastButtonState2 == HIGH && buttonState2 == LOW && buttonState1 == HIGH )
  {
    buttonPushCounter --;                             //if button2 has been pressed but not button1 -1 in counter
    delay(10); // debounce
  }

  controlled ();                                  // run control LED void to know what LEDS to turn on

}

The Automaton 'Led Fuel Gauge' example features a set of 6 leds that light up according to the resistance of a pot:

The 'cmp' object sends an EVT_STEP or EVT_BACK event to the 'step' object to turn an extra led on or off whenever the pot's resistance changes.

It would be fairly easy to change the code to use two button machines to send these events instead. Let me know if you need more help.

I made a little introduction for people new to the Automaton way.

Thanks for your help! I finally got the leds to turn on :smiley:

Hi,

Tell us, what was the problem, how did you solve it.

This is a learning forum and it would be appreciated if you rewarded the helpers, and anyone using this thread to solve their problem, with the solution.

Thanks.. Tom.. :slight_smile:

Oh, okay.
So my problem was that I had to buttons, one increased the number of pushes the other decreased said number, and a number of LEDs were supposed to turn on depending of the result of pushes made. But I was unable to count the pushes because I didn't know how to control the two buttonts very well. USing some of the code and information explained over I managedto count the pushes, although I can't save the result when I put the leds in the code.

although I can't save the result when I put the leds in the code.

I am not clear what you mean. Can you please describe the problem and post your latest program.

Thanks for the concert but I made it work I had written a line of code the wrong way :stuck_out_tongue: now it's working fine

Up up and away without feedback, seems to be normal in these days :sob: