Simple Arduino L.E.D Police Lights

 by dave1324

Step 3: Writing The Code.


The code for this project is very, very basic



// Project 1 - Police Lights by Dave1324

int ledDelay = 50; // delay by 50ms
int redPin = 9;
int bluePin = 11;


void setup() {
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}

void loop() {

digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms

digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms

digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms

digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms

digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms

digitalWrite(redPin, :LOW); // turn the red light off
delay(ledDelay); // wait 50 ms

delay(100); // delay midpoint by 100ms

digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms

digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms

digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms

digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms

digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms

digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms


}



Now upload to your arduino and thats it ! :)


 
Remove these adsRemove these ads by Signing Up
KegRaider says: Sep 14, 2012. 8:19 PM
I found a slight error in the code. Pretty easy to fix, but a total n00b may not know where it is....

[quoted code]
digitalWrite(redPin, :LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
delay(100); // delay midpoint by 100ms
[/quoted code]

Basically, take out the : from before the LOW command. ;)
Other than that, my young boy loves the lights, thanks.

-KegRaider
Crimson13 says: Dec 23, 2010. 4:43 PM
Great instrucable. I'm working on an emergency led lighting system, so this comes in handy.

One thing I would suggest for a smoother pattern, is to add another delay of 100ms at the end of your loop.

Right now it loops from blue-off to red-on without a delay, but the opposite has the 100ms delay at the midpoint. Adding that same midpoint delay at the end will make everything balance out.
Wally_Z in reply to Crimson13May 10, 2012. 5:01 AM
I have been researching flash patterns to use with my Arduino, and I have found that this works fairly good:

-Red flashes 3 times (50ms on, 50ms off)
-Blue flashes 3 times (50ms on, 50ms off)
-Alternating flashes of each (back and forth)
Wally_Z in reply to Crimson13May 10, 2012. 5:01 AM
I have been researching flash patterns to use with my Arduino, and I have found that this works fairly good:

-Red flashes 3 times (50ms on, 50ms off)
-Blue flashes 3 times (50ms on, 50ms off)
-Alternating flashes of each (back and forth)
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!