Hi there
I just started with programming arduino UNO
i tried a few things
so i wanted to build something new
what i wanted to do is
i have a switch and 1 LED
with the switch i klick 1 time then release then the LED needed to Blink 5 times and stop till i push the switch
this is what i have
int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
int ledOnTime=500;
int ledOffTime=500;
int buttonState = 0; // variable for reading button status
void setup() {
pinMode(ledPin, OUTPUT); // initialize LED pin as output
pinMode(buttonPin, INPUT); // initialize button pin as input
}
void loop() {
buttonState = digitalRead(buttonPin); // read the button state
if (buttonState == HIGH) { // pushbutton is pressed.
digitalWrite(ledPin, HIGH); // turn LED on
delay(ledOnTime);
digitalWrite(ledPin,LOW);
delay(ledOffTime);
} }
Welcome to the Forum. The good news is, you have already done some things correctly in your code. Please read the two posts
How to use this forum - please read. and
Read this before posting a programming question ...
at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.
Check out the Control Structures section of the reference, specifically "for" and "while".
To blink 5 times you would do something like this:
int blinkCount = 5;
void loop() {
buttonState = digitalRead(buttonPin); // read the button state
for(int i=0; i < blinkCount; i++)
{
if (buttonState == HIGH) { // pushbutton is pressed.
digitalWrite(ledPin, HIGH); // turn LED on
delay(ledOnTime);
digitalWrite(ledPin,LOW);
delay(ledOffTime);
}
}
}
This will blink as long as the button is held down, you will have to change it to wait for the button to be released once it is pressed.
i know how to blink it when button is pressed
but the problem is
i want to press button 1 time
then led blink 5 times
then press again for another 5 times blink
i want to press button 1 time
then led blink 5 times
When the button becomes pressed ** set a boolean variable to true and save the time from millis(). Each time through loop() check whether the variable is true. If it is then check whether the blink period has elapsed by subtracting the start millis() from millis() now. If the blink period has elapsed change the state of the LED (on to off or vice versa) and add 1 to the flash count***. If the LED has flashed 5 times then set the boolean variable to false to stop it flashing again.
**StateChangeDetection example in the IDE
*** BlinkWithoutDelay example in teh IDE