Go Down

Topic: LED Help (Read 647 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.

Go Up