I am trying to make something where if a button is pressed, it turns an led on and off very fast. The way the code is now is that whenever the button is pressed, it executes the code until you unpress it. Is there a simple way I can fix this? Thanks.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
delay(16);
digitalWrite(ledPin, HIGH);
delay(16);
digitalWrite(ledPin, LOW);
So, when the PB button is pressed, you want the LED to go on then off, once only.
But, if you let go of the PB switch and push it again, do you what the LED to sequence again?
So when the PB is pressed (like pressing the home button on a phone), I want the LED to wait 16ms (which should be about 60Hz/1 frame), then to turn on for 16ms, and back off. When the PB is not being pressed, the LED should be off. In my current setup, instead of the program running once when the PB is pressed, it loops over and over.
I'm using this to simulate a TV when you pull a trigger in Duck Hunt for the NES, so when the trigger is pulled I would like the program to run once and when it is not, the LED should be off.
const byte buttonPin = 2; // the number of the pushbutton pin
const byte ledPin = 13; // the number of the LED pin
boolean flag = true;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
} //END of setup()
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (flag == true && buttonState == HIGH)
{
delay(16);
// turn LED on:
digitalWrite(ledPin, LOW);
delay(16);
digitalWrite(ledPin, HIGH);
delay(16);
digitalWrite(ledPin, LOW);
flag = false;
}
if (flag == false && buttonState == LOW)
{
flag = true;
digitalWrite(ledPin, LOW);
}
} //END of loop()
Yeah so the "flag" is basically like a variable and whenever the button is pressed it turns true and at the end it turns the "flag" false. The else statement uses and AND gate and checks to see if the "flag" is false and the button is not pressed. Makes sense, I'm just kinda new to Arduino, I got it today. Thanks for the help.
When 'flag' is true your code is allowed to check for a HIGH on your switch.
When 'flag' is false your code is allowed to check for a LOW on your switch.
Setting 'flag' false once a HIGH is detected prevents the 'if()' from running more than once.
You will find that 'flags' will be useful in preventing code form running or letting different code to run.
In the future:
Using delay() freezes your code/sketch from executing during the delay time.
This is very often not desired.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?