Go Down

Topic: LED Help (Read 649 times) previous topic - next topic

Blaec

Ok, I'm a newbie, just to get that out of the way.
I have a Duemilanove, 4 white 5mm LEDs, and 4 220 Ohm 1/2 Watt Resistors. I wrote what I thought was some very simple, impossible to mess up code. However, when I wired one of the LEDs up, it ran through the code once, waited like five-seven seconds (when I had it to where it was only supposed to wait for 400 milliseconds), and started over. Why? How do I fix it?

estranged

Hard to say without seeing your sketch.  Go ahead and post it and we'll take a look.

Blaec

int ledPin1 =  12;    
int ledPin2 =  11;
int ledPin3 =  10;
int ledPin4 =  9;

void setup()  
{                
 pinMode(ledPin1, OUTPUT);    
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
}

void loop()                    
{

{
 digitalWrite(ledPin1, HIGH);
 delay(400);                  
 digitalWrite(ledPin1, LOW);  
 delay(200);
 digitalWrite(ledPin1, HIGH);
 delay(400);
 digitalWrite(ledPin1, LOW);
 delay(1000);
 digitalWrite(ledPin1, HIGH);
 delay(400);
 digitalWrite(ledPin1, LOW);
 delay(750);
 digitalWrite(ledPin1, HIGH);
 delay(400);
 digitalWrite(ledPin1, LOW);
 delay(300);
 digitalWrite(ledPin1, HIGH);
 delay(400);
 digitalWrite(ledPin1, LOW);
 delay(100);  
}
{
 digitalWrite(ledPin2, HIGH);
 delay(400);                  
 digitalWrite(ledPin2, LOW);  
 delay(200);
 digitalWrite(ledPin2, HIGH);
 delay(400);
 digitalWrite(ledPin2, LOW);
 delay(1000);
 digitalWrite(ledPin2, HIGH);
 delay(400);
 digitalWrite(ledPin2, LOW);
 delay(750);
 digitalWrite(ledPin2, HIGH);
 delay(400);
 digitalWrite(ledPin2, LOW);
 delay(300);
 digitalWrite(ledPin2, HIGH);
 delay(400);
 digitalWrite(ledPin2, LOW);
 delay(100);  
}
{
 delay(400);
 digitalWrite(ledPin3, HIGH);
 delay(400);
 digitalWrite(ledPin3, LOW);
 delay(1750);
 digitalWrite(ledPin3, HIGH);
 delay(500);
 digitalWrite(ledPin3, LOW);
 delay(650);
 digitalWrite(ledPin3, HIGH);
 delay(500);
 digitalWrite(ledPin3, LOW);
 delay(300);
 digitalWrite(ledPin3, HIGH);
 delay(450);
 digitalWrite(ledPin3, LOW);
 delay(200);
}  
{
 delay(400);
 digitalWrite(ledPin4, HIGH);
 delay(400);
 digitalWrite(ledPin4, LOW);
 delay(1750);
 digitalWrite(ledPin4, HIGH);
 delay(500);
 digitalWrite(ledPin4, LOW);
 delay(650);
 digitalWrite(ledPin4, HIGH);
 delay(500);
 digitalWrite(ledPin4, LOW);
 delay(300);
 digitalWrite(ledPin4, HIGH);
 delay(450);
 digitalWrite(ledPin4, LOW);
 delay(200);
}

}  

Blaec

Make that fourteen seconds that it waits.

estranged

#4
Apr 17, 2010, 05:48 am Last Edit: Apr 17, 2010, 05:50 am by estranged Reason: 1
If you only hooked up one of the LEDs, your reported behavior makes sense.  For example, if you wired up the ledPin1 LED, it would cycle through that first block of code, then continue on to execute the remaining three blocks, toggling the pins high and low, and waiting the specified times.  The fact that you don't have anything hooked up to the remaining pins just means you don't have anything to see, so you're just observing the delays.

Hook up the remaining 3 LEDs and it will probably make more sense.  :)

The extra {  } pairs you have around each block are unneeded.  You only need a single pair of { } for the entire loop function.

Blaec

Well, I want them all doing their blinking simultaneously. How do I do that?

estranged

Getting them to all blink in various patterns at the same time is a bit trickier.  You'll have to come up some timer variables to track how long each LED is supposed to be on, and how long it has been on.  Each cycle through the event loop, you'll check to see how long a particular LED has been on, and if it needs to be shut off at this point or not.  Similar idea for counting how long one has been off, and if its time to be turned on again.  

I wrote something conceptually similar, its a sketch that picks a random LED from a group of 32 periodically, then sets a random duration for that LED to fade on/fade off.  You can see the sketch here:

http://www.fischco.org/misc-stuff/2009/12/31/twinkling-stars-code.html

Look at the twinkle() function, that's where I'm tracking how long each LED has been on, and if its fading on or off.  Yours would be a bit simpler since you don't have to update the fade value each loop, just keep track of how long its supposed to be on/off and toggle it.

Sorry I don't have a nice simple answer for you, but that's about all I can think of so far.  Good luck!

Go Up