Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: ap16 on Feb 13, 2018, 01:09 pm

Title: How to toggle led
Post by: ap16 on Feb 13, 2018, 01:09 pm
I want to toggle 1 led. When button is pressed led comes on and when pressed again goes off.
Title: Re: How to toggle led
Post by: Grumpy_Mike on Feb 13, 2018, 02:05 pm
That will be a state change then. Look in the IDE under File-> Examples -> 02 Digital -> StateChangeDetection
Title: Re: How to toggle led
Post by: ap16 on Feb 14, 2018, 01:57 am
is there a way to do it without buttonpushcounter
Title: Re: How to toggle led
Post by: larryd on Feb 14, 2018, 03:00 am
Did your try?  StateChangeDetection
Title: Re: How to toggle led
Post by: 3Dgeo on Feb 14, 2018, 03:22 am
Here you go (connect button to PIN2 and ground):
Code: [Select]

const int buttonPin = 2;    //  button is connected to pin

bool buttonState = 0;       // variable to hold the button state
bool Mode = 0;              // What mode is the light in?


void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);    // Set the switch pin as input
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  if (!digitalRead(buttonPin)) // cheks if button is pressed (cheks if it's LOW, becouse PULLUP inverts signal: LOW = button pressed)
  {
    if (!buttonState) // same as: (buttonState == false) ---I made it a bit compact, but it may be confusinf to you
    {
      buttonState = true;
      Mode = !Mode; // this inverts button mode: If Mode was True - it will make it False and viseversa
    }
  }
  else buttonState = false;

  // blink LED
  digitalWrite(LED_BUILTIN, Mode); // remember: TRUE = HIGH = 1 and FALSE = LOW = 0 (You can use whatever You like)
  delay(5);
}
Title: Re: How to toggle led
Post by: Wawa on Feb 14, 2018, 04:05 am
Shorter version.
Leo..
Code: [Select]
const byte buttonPin = 2; // button between pin2 and ground

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (!digitalRead(buttonPin)) {
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    delay(500);
  }
}
Title: Re: How to toggle led
Post by: Grumpy_Mike on Feb 14, 2018, 08:47 am
is there a way to do it without buttonpushcounter
You need to know what the last state produced by the button to know what the next state should be. So how ever you dress it up you have to count the presesses up to a maximum of two.

What does that question really mean? Does it mean you can't understand the example and you think we deliberately want to make things complex?
Title: Re: How to toggle led
Post by: IoT_hobbyist on Feb 24, 2020, 01:34 am
I am sorry for answering an old post. But I hope it is useful for some googling this issue.  You can see how to toggle LED by button in this tutorial (https://arduinogetstarted.com/tutorials/arduino-button-toggle-led). The tutorial includes code WITH and WITHOUT debouncing
Title: Re: How to toggle led
Post by: noiasca on Feb 24, 2020, 07:03 am
I am sorry for answering an old post.
I don't like, when old posts are getting pushed.

The 3 answers from 2018 were ok and are still ok.
The "solutions" in your linked tutorial ain't no better than the provided solutions. Using "int" for each and any variable is bad coding behavior!