Pages: [1]   Go Down
Author Topic: LED Help  (Read 607 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

U of A, Tucson, AZ
Offline Offline
Full Member
***
Karma: 0
Posts: 164
Hello World
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}

}  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make that fourteen seconds that it waits.
Logged

U of A, Tucson, AZ
Offline Offline
Full Member
***
Karma: 0
Posts: 164
Hello World
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley

The extra {  } pairs you have around each block are unneeded.  You only need a single pair of { } for the entire loop function.
« Last Edit: April 16, 2010, 10:50:05 pm by estranged » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

U of A, Tucson, AZ
Offline Offline
Full Member
***
Karma: 0
Posts: 164
Hello World
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Pages: [1]   Go Up
Jump to: