Archive for January, 2010

Arduino Police Lights

Difficulty Level = 4 [What’s this?]

This is an amusing project inspired by flashing blue and red lights on police cars, ambulances, etc.  This is a perf board Arduino with 5 blue and 5 red LEDs, and the Arduino code lights them up in a pattern similar to police lights.

First, the Arduino built on a perf board.  It’s not hard to build your own Arduino.  I use a real Arduino board to upload code to the ATMega328 chip, then just move it to the project board.

Perfboard Arduino with blue and red LEDs for police lights mini-project

Now here it is in action — I really think if you don’t know what you’re looking at, it looks realistic in the dark.

Here’s the code that makes it work. Notice that some of the LEDs are controlled using PWM; the second and fourth LED in each of the blue and red groups. I would invite people to build something similar and tweak this to get the most realistic effect possible!

#define NUM_OFF 3
#define DELAY 50
#define PWM_MIN 10
#define PWM_MAX 128

int blue[5];
int red[5];

void setup()
{
  blue[0] = 19;
  blue[1] = 5;
  blue[2] = 18;
  blue[3] = 6;
  blue[4] = 17;
  red[0] = 13;
  red[1] = 11;
  red[2] = 12;
  red[3] = 10;
  red[4] = 9;

  for(int i=0;i<5;i++) {
    pinMode(blue[i], OUTPUT);
    pinMode(red[i], OUTPUT);
  }
  randomSeed(analogRead(0));
}

void loop()
{
  allOn();
  analogWrite(blue[1], random(PWM_MIN, PWM_MAX));
  analogWrite(blue[3], random(PWM_MIN, PWM_MAX));
  analogWrite(red[1], random(PWM_MIN, PWM_MAX));
  analogWrite(red[3], random(PWM_MIN, PWM_MAX));


  for(int i=0;i<NUM_OFF;i++) {
    digitalWrite(blue[random(5)], LOW);
    digitalWrite(red[random(5)], LOW);
  }
  delay(DELAY);
}

void allOn() {
  for(int i=0;i<5;i++) {
    digitalWrite(blue[i], HIGH);
    digitalWrite(red[i], HIGH);
  }
}

Published by Michael, on January 8th, 2010 at 10:03 pm. Filed under: Arduino,Level 4. | 3 Comments |