Code with blocks Arduino LEDs ON/OFF with one push button

Hello

I have an Arduino UNO and switch pushbuttons with protectors like this:

image

I want to code an interruptor so that when press the pushbutton, two LEDs go HIGH, and when pressing again they go LOW.

I have searched the following help links:

I need to understand the basics for the kids. We use blocks to code. We use Tinkercad for simulation and mBlock to translate the block-code solution into Arduino code. This is what I got:

Circuit:

Arduino code automatically generated by Tinkercad:

// C++ code
//
int buttonState = 0;

int timesPressed = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    timesPressed = (timesPressed + 1);
    if (timesPressed % 2 == 0) {
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      timesPressed = 0;
    }
    delay(100); // Wait for 100 millisecond(s)
  }
}

Block-based solution:

Problem:

Sometimes it bugs, flashing the LEDs or even having to press twice for the LEDs to change state:

Arduino

Question:

What can be improved from the code (if possible, a block-based answer)? Why does they flash or have to press twice in order to change the LEDs state?

Considerations:

  • At this time I am unable to test the code in real life.
  • Of course I could use an interruptor like this:

    But we don't have one, so a push button is mandatory.

Thank you!

  • What does this mean ?

Thank you for your fast answer!

I've read about debouncing and stuff, but in order to solve it you mean to code the solution using https://docs.arduino.cc/built-in-examples/digital/Debounce/?

Oh, I see.

The code without bug should change a lot from the initial code? Can you help me, please?

The kids and I code with blocks.

Go to Tinkercad, start a new project (circuit), drag the components and code with blocks. Then go to the Text view and copy the generated Arduino code

  • When writing code for switch operations, look for a change in switch state i.e. when the switch changes from a HIGH to a LOW or vice versa.
    Do not look at the current level of the switch.
1 Like

I was thinking maybe using another if condition to avoid immediate change of state, but I couldn't. Any ideas?

I was thinking about these and tried with the following blocks:

// C++ code
//
int buttonState = 0;

int timesPressed = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(2);
  if (buttonState == HIGH) {
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    timesPressed = (timesPressed + 1);
  }
  if (buttonState == LOW) {
    if (timesPressed % 2 == 0) {
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      timesPressed = 0;
    }
  }
  delay(100); // Wait for 100 millisecond(s)
}

It still requires that sometimes have to press more than once, but at least the flickering problem seems to have disappeared.

Also, I put wait 0.1 seconds out the if but I don't know if it is correct.

  • Not tested

  • Try this to see how things react.

//================================================^================================================

#define PRESSED          HIGH
#define RELEASED         LOW

#define LEDon            HIGH
#define LEDoff           LOW

const byte mySwitch    = 2;
const byte firstLED    = 11;
const byte secondLED   = 12;

byte timesPressed;
byte buttonState;
byte lastButtonState   = LOW;


//================================================^================================================
void setup()
{
  pinMode(mySwitch, INPUT);
  
  pinMode(firstLED, OUTPUT);
  pinMode(secondLED, OUTPUT);

} //END of   setup()


//================================================^================================================
void loop()
{
  buttonState = digitalRead(mySwitch);

  //has there been a change in the switch's state ?
  if (lastButtonState != buttonState)
  {
    //update to this new state
    lastButtonState = buttonState;

    //poor mans de-bounce
    delay(50); //wait for 50 millisecond(s)

    //has the switch been pressed ?
    if (buttonState == PRESSED)
    {
      digitalWrite(firstLED, LEDon);
      digitalWrite(secondLED, LEDon);
      timesPressed = (timesPressed + 1);
    }

    //has the switch been released ?
    else if (buttonState == RELEASED)
    {
      if (timesPressed % 2 == 0)
      {
        digitalWrite(firstLED, LEDoff);
        digitalWrite(secondLED, LEDoff);
        timesPressed = 0;
      }
    }
  }
  
} //END of   loop()


//================================================^================================================

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.