Hey guys. I started using Arduino yesterday and I am trying to make a program that can toggle blinking of the led. So whenever I press the button, I want to start blinking the led and when I press the button once again, I want the blinking to stop. I tried approaching the problem like so:
But I am having to press and hold the button for a little bit before it turns off. I am assuming this is happening because the if statement is happening at a particular instant of the program where if I dont provide the input, the program keeps going on.
Help would be appreciated!
We can't help when there's no code to help with. I might guess that your code uses delays and a blocking loop which makes it very unresponsive but who knows?
Steve
Have a look at the BlinkWithoutDelay example in your IDE.
Note: it contains some small mistakes that won't matter for almost 25 days of running.
Here is an example sketch that toggles the LED on pin 13 with a button press. Timing is done with millis() like in the begginer's guide to millis() tutorial and modified code from the state change detection tutorial. The main modification is the switch is wired to ground and a digital input with the internal pull up resistor enabled so the switch is active LOW, instead of active HIGH.
// This is to illustrate using tne state change detectin method to
// toggle the state of a pin (Pin 13, the on board LED)
// a momentary switch (pushbutton) is wired from pin 8 to ground
// by C Goulding aka groundFungus
// this constant won't change:
const int buttonPin = 8; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
slipstick:
We can't help when there's no code to help with. I might guess that your code uses delays and a blocking loop which makes it very unresponsive but who knows?
Steve
https://imgur.com/a/b1BmOuv
This is the image of the program sorry.
Please read "How to use this forum" at the top of every forum and post your code inline as described in #7 in there.
Code is text. Many, perhaps most, people will not waste their time going off site to look at pictures of text.
Steve