code line placement question

Why does the B1red (LED) not go LOW unless it is placed in the 2nd line of the void loop? Commenting it out and using the low command the 3rd line up from the bottom of the void loop does not turn off the LED. But the b1yellow, b11green function as they should.

I added the commented out line as a troubleshoot aid.

int b1red = 12;
int b1yellow = 11;
int b11green = 10;

void setup()
{
pinMode(b1red, OUTPUT);
pinMode(b1yellow, OUTPUT);
pinMode(b11green, OUTPUT);

}

void loop()
{
digitalWrite(b1red, HIGH);
delay(1000);
//digitalWrite(b1red, LOW);
digitalWrite(b11green, HIGH);
delay(1000);
digitalWrite(b1yellow, HIGH);
delay(1000);
digitalWrite(b1red, LOW);
digitalWrite(b11green, LOW);
digitalWrite(b1yellow, LOW);
}

Try:

void loop()
{
  digitalWrite(b1red, HIGH);  
  delay(1000);
  digitalWrite(b11green, HIGH);
  delay(1000);
  digitalWrite(b1yellow, HIGH);
  delay(1000);
  digitalWrite(b1red, LOW);
  digitalWrite(b11green, LOW);
  digitalWrite(b1yellow, LOW);
  delay(1000)
}

Without the final delay the leds will never appear to be going low even though they are for a very short time. You need to appreciate the speed of the loop function, it's much faster then your eye can follow

Lefty

The loop() function is called repeated. When you turn the red LED off via that call three lines up from the end of loop() there is no delay between that, and the red LED being turned back on in the first line of loop() next time it is called. So, although the LED does turn off it is turned back on almost immediately, so you never get to see it go off.

If you added a delay at the end of loop(), you'd have seen the red LED go off.

ETA: dammit, too slow again.

Thanks for the help boy do I feel foolish not adding the delay line to the code.
I am new to the ARDUINO had the UNO for about a week and been having fun with it.

Thanks Again

Dan

dfargo5133:
Thanks for the help boy do I feel foolish not adding the delay line to the code.
I am new to the ARDUINO had the UNO for about a week and been having fun with it.

Thanks Again

Dan

Cool, but beware our own PaulS will probably only allow you another week or two for using the delay function before he unleashes a fury that can burn out leds. As he then tells you to study the example sketch blinkwithoutdelay so you can learn to use a method to time events without blocking all code execution until a delay times out. It, delay(), really can be a restrictive command as you try to do more complex and useful things in your sketches.

Lefty

Lefty,

If delay fits the need is there anything actually wrong with using it for a givin project? Can it damage something (LED burnout)
I know it stopps the loop until the delay completes.

I am going to be building a project for a railroad mesum that will cycle an old railroad block signal from green to red to yellow and then back to green any time the arduino is powered up.

I did look at blink with outdelay but need to study it more.

If you don't mind not doing anything else there is nothing wrong with delay. As for the LEDs, make sure you have resistors in series with them.

Can it damage something (LED burnout)

I think Lefty meant that Paul’s fury can burn out LEDs, not the use of the delay().

Oh yes, well in that case put a resistor between yourself and PaulS. :stuck_out_tongue:

That might work, just be sure it's of high enough ohms. These folks build some rather larger ohm resistors:

Lefty