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 ads by
Signing Up

































Visit Our Store »
Go Pro Today »




[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
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.
-Red flashes 3 times (50ms on, 50ms off)
-Blue flashes 3 times (50ms on, 50ms off)
-Alternating flashes of each (back and forth)
-Red flashes 3 times (50ms on, 50ms off)
-Blue flashes 3 times (50ms on, 50ms off)
-Alternating flashes of each (back and forth)