0

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.

  • i have corrected keepgoing == 1, instead of keepgoing = 1, please check now. – goddland_16 Apr 28 '17 at 2:45
  • By "second one", do you mean one of the LEDs? If so swap the leads to them and then report back if the same kind of dimness still occurs – maybe one of your LEDs is just dimmer than the other. If the dimness swaps, then try changing ledtwo = 10 to ledtwo = 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. – James Waldby - jwpat7 Apr 28 '17 at 2:57
  • @goddland_16, as a condition for an if, keepgoing = 1 will always evaluate as true,which will keep the while statement going. keepgoing == 1 also will evaluate as true, given that keepgoing was initialized to 1. – James Waldby - jwpat7 Apr 28 '17 at 3:02
  • swapping fixed my brightness, but how do I make my loop keep repeating, it only executes once :( – Jameson1997 Apr 28 '17 at 3:08
  • @JamesWaldby-jwpat7, yes, it is. For this context I assumed he meant it as keepgoing == 1. – goddland_16 Apr 28 '17 at 3:08
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! – Jameson1997 Apr 29 '17 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.