Lab5 Speeding Detector

Submitted by wendy.xue on Mon, 03/04/2013 - 22:36

 

Description

The speeding detector is useful to train people not to go over the speed limit while driving. Imaging the FSR as the gas paddle of your car. As you press it, the speed of the car goes up. When it goes over the speed limit, the red and blue LED (imaging the police car light) will start flashing and the Piezo speaker will ring a two-tone siren. The faster you drive, the faster the siren will sound and the faster the police car lights will flash. If you ease your gas paddle, everything will return to the peaceful driving. However, if you continue to speed and eventually go over the speed limit by 15 miles, you will see animated images of police officer stopping you and writing you a ticket. 

Video is at:

http://www.youtube.com/watch?v=pj8l_mEywHo

http://www.youtube.com/watch?v=93jQLK6JiFY

Components Used

1- Arduino Uno
1- Breadboard
1- Force sensitive resistor
1- Piezo speaker
2- 220kΩ Resistors
2- LEDs
various wires
 

Code

Arduino code:

 

/********************
Ask user for a speed limit via keyboard input
If the pressure reading from FSR is greater than the speed limit, 
turn on the blue and red LEDs. The LED should flash alternatively 
until the FSR reading becomes smaller than the speed limit. At the
same time when the LED are flashing, play the siren.
 note frequency period PW (timeHigh)
 * c        261 Hz        3830 1915
 * d        294 Hz        3400 1700
 * e        329 Hz        3038 1519
 * f        349 Hz        2864 1432
 * g        392 Hz        2550 1275
 * a        440 Hz        2272 1136
 * b        493 Hz        2028 1014
 * C        523 Hz        1912 956
 *************************************************/
const int blueLedPin = 9;
const int redLedPin = 13;
const int speakerPin = 7;
const int fsrPin = A0;
const int fsrThreshold = 5; // minimum reading of the sensors
 
int DEBUG = 0;
 
int speedLimit = 150;
int fsrReading = 0;
int musicFreq = 0;
int incomingByte; 
int prevFsrReading = fsrReading;
 
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
  pinMode(blueLedPin, OUTPUT);
  pinMode(redLedPin, OUTPUT);
  pinMode(speakerPin, OUTPUT);
}
 
void loop() {
  //A0 reading represent the amount of pressure on gas paddle, thus speed of a car
  fsrReading = analogRead(A0);
  
  //convert the pressure to speed between 10miles to 200 miles
  int curSpeed = map(fsrReading, 0, 1023, 10, 200);
  //convert the speed to led blinking delay from 1000 ms to 10 ms
  int blinkDelay = map(curSpeed, 10,200,1000, 10); 
  
  //if user is speeding
  if (curSpeed > speedLimit)
  {
    //turn on the police lights
      digitalWrite(blueLedPin, HIGH);
      delay(blinkDelay);
      digitalWrite(blueLedPin, LOW);
      delay(blinkDelay);
      digitalWrite(blueLedPin, HIGH);
      delay(blinkDelay);
      tone(speakerPin, 622,blinkDelay);
      digitalWrite(blueLedPin, LOW);
      delay(blinkDelay);
  
      digitalWrite(redLedPin, HIGH);
      delay(blinkDelay);
      digitalWrite(redLedPin, LOW);
      delay(blinkDelay);
      digitalWrite(redLedPin, HIGH);
      delay(blinkDelay);
      tone(speakerPin, 523, blinkDelay);
      digitalWrite(redLedPin, LOW);
      delay(blinkDelay);
  }
 
  Serial.println(curSpeed);
  noTone(speakerPin);
//  if(DEBUG && fsrReading - prevFsrReading >fsrThreshold)
//  {
//    Serial.print("speedLimit: ");
//    Serial.println(speedLimit);
//    Serial.print("fsr reading: ");
//    Serial.println(fsrReading);
//    Serial.print("current speed: ");
//    Serial.println(curSpeed);
//    Serial.println();
//  }
//  prevFsrReading = fsrReading;
  // wait a bit for the analog-to-digital converter 
  // to stabilize after the last reading:
  delay(10);
}

Processing code:

 

import processing.serial.*;
 
Serial port;
 
int numFrames = 4;
float speed = 0;
float speedLimit = 150;
PImage[] imgs = new PImage[numFrames]; 
PImage backgroundImg;
int frame = 0;
 
void setup()
{
  size(640,480);
  background(255); //white background
  frameRate(5);
  
  port = new Serial(this, Serial.list()[0], 9600);
  port.bufferUntil('\n');
  imgs[0] = loadImage("policecar.jpg");
  imgs[1] = loadImage("ticket.jpg");
  imgs[2] = loadImage("ticketLarge.jpg");
  imgs[3] = loadImage("ticketXlarge.jpg");
  backgroundImg = loadImage("roadtrip.jpg");
}
 
void draw()
{
  if (speed - speedLimit>15)
  {
    background(255);
    frame = (frame+1) % numFrames;
    int x = width/2 - imgs[frame].width/2;
    int y = height/2 - imgs[frame].height/2;
    image(imgs[frame], x, y);
  }
  else
  {
    image(backgroundImg,0,0);
  }
}
 
void serialEvent (Serial port)
{
  String input = port.readStringUntil('\n');
  
  if(input != null)
  {
    input = trim(input); //removing trailing whitespace
    speed = float(input); 
   }   
 }
 
 
 
 
0
Your rating: None
Drupal theme by Kiwi Themes.