int count=4;
void setup() {
pinMode (2, INPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
pinMode (13, OUTPUT);
}
void loop() {
while(count > 0) {
if (digitalRead(2) == HIGH) {
digitalWrite (11, HIGH);
delay (4000);
digitalWrite(11, LOW);
digitalWrite (12, HIGH);
delay (7000);
digitalWrite(12, LOW);
digitalWrite (13, HIGH);
delay (8000);
digitalWrite(13, LOW);
count = count - 1;
}
}
}
Show a wiring diagram of how your button is wired.
Welcome to the forum
Most of your code is inside the test for pin 2 being HIGH so it will only run when pin 2 is HIGH
I presume that you want to press the button and have the LEDs flash 4 times. Is that right ?
Yes, that is what i want to do, what i have to do to do that?
Separate the test for button presses from the LED code
In loop(), when you detect a button press set a boolean variable to true. Separately in loop() if the boolean is true then do what you want with the LEDs and set the boolean back to false to prevent it repeating
There are some extra steps that you might want to add later, such as detecting when the button becomes pressed rather than when it is pressed, but they can come later
I can fix it changing this:
void loop() {
if (digitalRead(2) == HIGH) {
while(count > 0) {
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.