-1

I have just started my first project. I got through with 2 periodically blinking LEDs. Now I want one LED (blue) to be lit constantly and the other (red) to be blinking as before. Can anyone help me with this?

This is my code for blinking LEDs.

int red_led = 4; int blue_led = 3; void setup() { pinMode(red_led, OUTPUT); pinMode(blue_led, OUTPUT); } void loop() { digitalWrite(blue_led, LOW); digitalWrite(red_led, HIGH); delay(1000); digitalWrite(blue_led, HIGH); digitalWrite(red_led, LOW); delay(1000); } 
3
  • So you do not want one of your LEDs to turn off? The answer will depend on how your LEDs are wired. Commented Dec 3, 2015 at 23:44
  • Why are you even setting the blue LED low then? Commented Dec 4, 2015 at 7:50
  • What had you tried before posting the question? Did you at all understand the code you posted above? Commented Dec 14, 2015 at 17:16

2 Answers 2

2

The wonderful thing about the Arduino is that it hides a lot of the programming "noise" and lets you concentrate on the code that is actually doing the work. Part of the joy of programming is starting to "own" the code and being able to make it do what you want.

The answer here isn't too hard, and I'm sure that many could answer your question – but I think you might enjoy the experience more (and you would earn more respect) if you make a stab at it yourself first. Or if you already have, tell us what you've tried and what happened when you tried it. That will help us help you to see not only the steps necessary, but the concepts that will enable you to branch off on your own.

Do you know what is causing the LEDs to blink in your current code?

2
  • 1
    The LEDs get forward-biased with positive supply at anode and ground at cathode: that makes them getting lit. In the code, the infinite loop forces the consecutive LEDs to glow for 1000ms and then going to "off" state for 1000ms. Commented Dec 3, 2015 at 23:45
  • 1
    So, if you move the blue_led control out of the infinite loop() and into setup() you get what you want. Commented Dec 4, 2015 at 3:32
1

Remember that the setup() function runs once, and then the loop() function runs forever. So to keep the blue LED light, simply turn it on, but never off. The code for the red LED remains unchanged.

const int red_led = 4; const int blue_led = 3; void setup() { pinMode(red_led, OUTPUT); pinMode(blue_led, OUTPUT); digitalWrite(blue_led, HIGH); // LED On } void loop() { digitalWrite(red_led, HIGH); // LED On delay(1000); digitalWrite(red_led, LOW); // LED Off delay(1000); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.