Lighting up LEDs based on how many times a button has been pressed

I have three LEDs I want to progressively turn on with three clicks. I have no error messages but nothing happens and I don't really know what to do.

int buttonState = 1;
int button_press_counter = 0;
int last_buttonState = 1;
int led_state = LOW;
int led_state1 = LOW;
int led_state2 = LOW;
void setup()
{
    pinMode(12, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(2, INPUT_PULLUP);
}

void loop()
{
    buttonState = digitalRead(2);
    if (buttonState != last_buttonState)
    {   
      if (buttonState == LOW) 
       {
       button_press_counter++;
       }
      	if (button_press_counter == 1);
     	{
        	led_state == HIGH;
        }
        if (button_press_counter == 2);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
        }
        if (button_press_counter == 3);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
            led_state2 == HIGH;
        }
    }
    digitalWrite(12, led_state);
    digitalWrite(10, led_state1);
    digitalWrite(8, led_state2);
}

How ia the button wired ?
Your loop spins really fast.. if you don’t wait for button release you’ll be in trouble (same with bouncing)
Where is last_buttonState updated ?

Don’t forget to update last_buttonState !

Sorry, but how should I do that?


    buttonState = digitalRead(2);
    if (buttonState != last_buttonState)
    {   
      last_buttonState = buttonState;  //  <————<<<<<
      if (buttonState == LOW) 

You could try it like this..

#include <mechButton.h>

#define BTN_PIN   2
#define LED_1     12
#define LED_2     10
#define LED_3     8

mechButton  theButton(BTN_PIN);  // A debounced button.
int         state;               // TO hold our "state" (How many clicks)

void setup() {
   
    pinMode(LED_1, OUTPUT);               // Setup  LED pins.
    pinMode(LED_2, OUTPUT);
    pinMode(LED_3, OUTPUT);
    theButton.setCallback(btnClicked);    // Attach a callback to the button.
    state = 0;
}


// Will be called when the button has been clicked. (Changed states)
void btnClicked(void) {

   if (!theButton.trueFalse()) {          // If the button has gone to ground. (Clicked)..
      state++;                            // Bump up our state. Or, click count.
      if (state>3) {                      // If the count is bigger than three..
         state = 0;                       // We'll wrap around to zero.
      }
      switch (state) {                    // Now depending on the count we'll..
         case 0 :                         // Zero?
            digitalWrite(LED_1, LOW);     // All off.
            digitalWrite(LED_2, LOW);
            digitalWrite(LED_3, LOW);
         break;
         case 1 :                         // One?
            digitalWrite(LED_1, HIGH);    // ALl but first off.
            digitalWrite(LED_2, LOW);
            digitalWrite(LED_3, LOW);
         break;
         case 2 :
            digitalWrite(LED_1, HIGH);    // Two?
            digitalWrite(LED_2, HIGH);    // The first two are on.
            digitalWrite(LED_3, LOW);
         break;
         case 3 :
            digitalWrite(LED_1, HIGH);    // Three?
            digitalWrite(LED_2, HIGH);    // All on.
            digitalWrite(LED_3, HIGH);
         break;
      }
   }
}


void loop(void) {

   idle();     // The only thing we call in loop() is idle(). This makes things, like the button, run.
}

This uses a debounced button library. You will need to install LC_baseTools from the IDe library manager to compile this.

Good luck

-jim lee

I'm not sure if I did something wrong but that doesn't seem to work.

Show us your changes i.e. your complete sketch.



Show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

Since the OP is getting showered with code samples, here is another one. However, if it is school work, it should not be submitted as such, otherwise teacher may ask awkward questions about it.

uint8_t button_press_counter = 0 ;

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  static uint32_t buttonPressedAtMs = millis() ; 
  if ( digitalRead(2) == LOW && millis() - buttonPressedAtMs > 300 ) { // debounce lockout
    buttonPressedAtMs = millis() ;
    if ( ++button_press_counter > 3 ) button_press_counter = 0 ;
  }
  digitalWrite(12, button_press_counter >= 1 ? HIGH : LOW );
  digitalWrite(10, button_press_counter >= 2 ? HIGH : LOW );
  digitalWrite(8,  button_press_counter >= 3 ? HIGH : LOW );
}

Like how did they come up with it ? :wink:


I did it online. My code is the following after your suggestion:

int buttonState = 1;
int button_press_counter = 0;
int last_buttonState = 1;
int led_state = LOW;
int led_state1 = LOW;
int led_state2 = LOW;
void setup()
{
    pinMode(12, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(2, INPUT_PULLUP);
}

void loop()
{
    buttonState = digitalRead(2);
    if (buttonState != last_buttonState)
    {   
      last_buttonState = buttonState;
      if (buttonState == LOW) 
       {
       button_press_counter++;
       }
      	if (button_press_counter == 1);
     	{
        	led_state == HIGH;
        }
        if (button_press_counter == 2);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
        }
        if (button_press_counter == 3);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
            led_state2 == HIGH;
        }
    }
    digitalWrite(12, led_state);
    digitalWrite(10, led_state1);
    digitalWrite(8, led_state2);
}

Suggest you use diagonal pins on the switch; this will ensure it is wired properly.

Try:


int buttonState = 1;
int button_press_counter = 0;
int last_buttonState = 1;
int led_state = LOW;
int led_state1 = LOW;
int led_state2 = LOW;
void setup()
{
    pinMode(12, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(2, INPUT_PULLUP);
}

void loop()
{
    buttonState = digitalRead(2);
    if (buttonState != last_buttonState)
    {   
      last_buttonState = buttonState;
      if (buttonState == LOW) 
       {
       button_press_counter++;

      	if (button_press_counter == 1);
     	{
        	led_state == HIGH;
        }
        else if (button_press_counter == 2);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
        }
        else if (button_press_counter == 3);
     	{
             led_state == HIGH;
             led_state1 == HIGH;
             led_state2 == HIGH;
         }

   if (button_press_counter == 4 );
    {
             //turn OFF LEDs on the 4th count      
             led_state == LOW;
             led_state1 == LOW;
             led_state2 == LOW;
             Counter = 0;
    }

      }
    }

    digitalWrite(12, led_state);
    digitalWrite(10, led_state1);
    digitalWrite(8, led_state2);
 

} //END of loop()

I don't know what you mean by diagonal pins they don't seem to be available on the program I'm using, Tinkercad. The code you sent had a few errors but I think I fixed them though I may have broken the code because it still does nothing.


int buttonState = 1;
int button_press_counter = 0;
int last_buttonState = 1;
int led_state = LOW;
int led_state1 = LOW;
int led_state2 = LOW;
void setup()
{
    pinMode(12, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(2, INPUT_PULLUP);
}

void loop()
{
    buttonState = digitalRead(2);
    if (buttonState != last_buttonState)
    {   
      last_buttonState = buttonState;
      if (buttonState == LOW) 
       {
       button_press_counter++;

      	if (button_press_counter == 1);
     	{
        	led_state == HIGH;
        }
        if (button_press_counter == 2);
     	{
        	led_state == HIGH;
            led_state1 == HIGH;
        }
        if (button_press_counter == 3);
     	{
             led_state == HIGH;
             led_state1 == HIGH;
             led_state2 == HIGH;
         }

   if (button_press_counter == 4 );
    {
             //turn OFF LEDs on the 4th count      
             led_state == LOW;
             led_state1 == LOW;
             led_state2 == LOW;
             button_press_counter = 0;
    }

      }
    }
  
    digitalWrite(12, led_state);
    digitalWrite(10, led_state1);
    digitalWrite(8, led_state2);
 

} //END of loop()

“I don't know what you mean by diagonal pins”

Corner to corner.


Like this?

Does Tinkercad model any kind of switch contact bounce?

Guessing no, it doesn’t:

If you build this circuit for real, it may not function as smoothly.

a7

Yes

Ok. Still does not work though with the code I most recently sent.

// Does this sketch switch the led on which is attached to pin 8
void setup() {
   pinMode(8, OUTPUT);
   digitalWrite(8, HIGH ) ;
}

void loop() { }

It that doesn't do anything, you have other problems.

= is not the same as ==

Make lines like this:
led_state == HIGH
look like this:
led_state = HIGH