I want to make 2 LEDs blink back and forth continuously, but they only do so once, and the second one is very dim.
code:
int ledone = 12;
int ledtwo = 10;
int keepgoing = 1;
void setup() {
pinMode(ledone, OUTPUT);
pinMode(ledtwo, OUTPUT);
}
void loop() {
while (keepgoing == 1) {
digitalWrite(ledone, HIGH);
delay(80);
digitalWrite(ledone, LOW);
digitalWrite(ledtwo, HIGH);
delay(80);
digitalWrite(ledtwo, LOW);
delay(500);
}
}
I am using an Arduino Uno and one 220 ohm resistor for each LED.
James Waldby - jwpat7
8,9203 gold badges20 silver badges33 bronze badges
asked Apr 28, 2017 at 2:29
anonanon
1 Answer 1
I think the problem is, that your delays are just too short, so you see the blinking not as blinking, but as a dim lit led.
Maybe you can try the following code:
void loop() {
while (keepgoing == 1) {
digitalWrite(ledone, HIGH);
digitalWrite(ledtwo, LOW);
delay(500);
digitalWrite(ledone, LOW);
digitalWrite(ledtwo, HIGH);
delay(500);
}
}
Is this working?
-
I tried this code and each LED only blinked once. This is really confusing!anon– anon2017年04月29日 01:56:19 +00:00Commented Apr 29, 2017 at 1:56
lang-cpp
ledtwo = 10
toledtwo = 9
, ie to some other pin, and rewire accordingly – maybe one of your IO pins has a problem or your wires are loose. Also try changing those fairly-short 80-ms delays to perhaps twice or three times as long.if
,keepgoing = 1
will always evaluate as true,which will keep thewhile
statement going.keepgoing == 1
also will evaluate as true, given thatkeepgoing
was initialized to 1.