Instructables

Arduino Police Lights (ATMega 2650 Version) **New Flash Patterns!**

Step 2: The Code

Picture of The Code
Now that you have it all hooked up, you will want to paste the code into your Arduino IDE. You can change this code any way you want to make it look more like police lights, or even like an oversized Nightrider if you use all red LED's. So feel free to modify it, you don't have to say that I wrote the code, just mention me for giving you the idea or the base to branch your project off of.

Patterns:
-Flash one side 3 times
-Flash the other side 3 times
-Flash one side one time, then the other side one time, (alternating happening 7 times)
-Flash one side twice, then the other side twice (happens 6 times)

Feel free to mess with the timings, or how many cycles it does of each to your liking.

The code:

/*
This sketch will flash 8 LED's in certain patterns. The "for" statement really helped cut down on the code (It cut the size in half).
This project is meant for the blue lights I will be using in my vehicle as a volunteer firefighter, so the variables are named as such.
*/

//Declaring the front lights
const int GrillTopLeft = 5;
const int GrillTopRight = 6;
const int GrillBottomLeft = 7;
const int GrillBottomRight = 8;

//For the lights in by the rear-view mirror, just use the outputs for the REAR LED's so it looks awesome, and you don't have to use 4 more outputs.

//Declaring the rear lights
const int TaillightLeft = 9;
const int ReverselightLeft = 10;
const int ReverselightRight = 11;
const int TaillightRight = 12;

void setup() {
   //Declares the front LED's as output
  pinMode(GrillTopLeft, OUTPUT);
  pinMode(GrillTopRight, OUTPUT);
  pinMode(GrillBottomLeft, OUTPUT);
  pinMode(GrillBottomRight, OUTPUT);

  //Declares the rear LED's as outputs
  pinMode(TaillightLeft, OUTPUT);
  pinMode(ReverselightLeft, OUTPUT);
  pinMode(ReverselightRight, OUTPUT);
  pinMode(TaillightRight, OUTPUT);
}   

void loop() {
  
  for(int x=0; x < 2; x++){  //Nested for structure. It makes the whole light flashing loop cycle 3 times.
  for(int x=0; x < 4; x++){  //First LED's flash 3x
  digitalWrite(GrillTopLeft, HIGH);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, HIGH);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, HIGH);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, HIGH);
  digitalWrite(TaillightRight, LOW);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(50);
}
 
  //Makes the other LED's flash 3x
  for(int x=0; x < 4; x++){
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, HIGH);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, HIGH);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, HIGH);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, HIGH);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(50);
  }
  }
  //Beginning the alternating flashes
  for(int x=0; x < 8; x++){
  digitalWrite(GrillTopLeft, HIGH);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, HIGH);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, HIGH);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, HIGH);
  digitalWrite(TaillightRight, LOW);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, HIGH);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, HIGH);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, HIGH);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, HIGH);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(50);
  }
 
  for(int x=0; x < 5; x++){
   
  for(int x=0; x < 2; x++){
  digitalWrite(GrillTopLeft, HIGH);
  digitalWrite(GrillTopRight, HIGH);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, HIGH);
  digitalWrite(TaillightRight, HIGH);
  delay(50);
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(50);
}
  for(int x=0; x < 2; x++){
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, HIGH);
  digitalWrite(GrillBottomRight, HIGH);
  digitalWrite(TaillightLeft, HIGH);
  digitalWrite(ReverselightLeft, HIGH);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(75); //Changed from 50ms because it was too fast, and hard to see.
  digitalWrite(GrillTopLeft, LOW);
  digitalWrite(GrillTopRight, LOW);
  digitalWrite(GrillBottomLeft, LOW);
  digitalWrite(GrillBottomRight, LOW);
  digitalWrite(TaillightLeft, LOW);
  digitalWrite(ReverselightLeft, LOW);
  digitalWrite(ReverselightRight, LOW);
  digitalWrite(TaillightRight, LOW);
  delay(75); //Changed from 50ms as well.
  }
  }
}
 
Remove these adsRemove these ads by Signing Up
SayntCigol2 years ago
/*
This is a 16 LED police light sketch to be used with the MEGA.
Hook up each line of LED's to a breadboard properly and it'll
look amazing!

This Code is much easier to read / modify if desires change.
*/

void setup() {
// Give all LED-pins OUTPUT status
// Pin 23-37 is Red, 39-53 is Blue
for(int i=23; i<54; i+=2) {
pinMode(i, OUTPUT);
int RedLEDs[] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37};
int BlueLEDs[] = {39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 52, 52, 53};
}
}

void loop() {
// Flash the LEDs in series of 6, alternating the color 3 times
for(int i=0; i<3; i++) {
flashLED(6, BlueLEDs);
flashLED(6, RedLEDs);
}

// Flash the LEDs in series of 2, alternating the color 5 times
for(int i=0; i<5; i++) {
flashLED(2, BlueLEDs);
flashLED(2, RedLEDs);
}
}

void flashLED(int times, int LEDPins[15]) {
for(int i=0; i for(int pin=0; pin<15; pin++) {
digitalWrite(LEDPins[pin], HIGH);
}
delay(50);
for(int pin=0; pin<15; pin++) {
digitalWrite(LEDPins[pin], LOW);
}
}
}

}
RHE1232 years ago
Interesting animation! Your code is quite long (as you say yourself) and I think this can be shortened a lot. So this is what I did. I was not able to actually test it (I don't have a mega available at the moment), but it should have the same output as your code. It can be even shorter, but than the readability would be worse. The code:
/*
This is a 16 LED police light sketch to be used with the MEGA.
Hook up each line of LED's to a breadboard properly and it'll
look amazing!
*/

void setup() {
  // Give all LED-pins OUTPUT status
  // Pin 23-37 is Red, 39-53 is Blue
  for(int i=23; i<54; i+=2) {
    pinMode(i, OUTPUT);
  }
}  

void loop() {
  // Flash the LEDs in series of 6, alternating the color 3 times
  for(int i=0; i<3; i++) {
    flashBlue(6);
    flashRed(6);
  }

  // Flash the LEDs in series of 2, alternating the color 5 times
  for(int i=0; i<5; i++) {
    flashBlue(2);
    flashRed(2);
  }
}

void flashBlue(int times) {
  for(int i=0; i<times; i++) {
    digitalWrite(37, HIGH);
    digitalWrite(35, HIGH);
    digitalWrite(33, HIGH);
    digitalWrite(31, HIGH);
    digitalWrite(29, HIGH);
    digitalWrite(27, HIGH);
    digitalWrite(25, HIGH);
    digitalWrite(23, HIGH);
   
    delay(50);
  }
}

void flashRed(int times) {
  for(int i=0; i<times; i++) {
    digitalWrite(53, HIGH);
    digitalWrite(51, HIGH);
    digitalWrite(49, HIGH);
    digitalWrite(47, HIGH);
    digitalWrite(45, HIGH);
    digitalWrite(43, HIGH);
    digitalWrite(41, HIGH);
    digitalWrite(39, HIGH);
   
    delay(50);
  }
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!