Gokul Shrinivas
Published © GPL3+

Vehicle Violation Detector

Automatically detects the vehicles crossing the stop line during red signal and generates penalty - cloud based on no. of records.

AdvancedShowcase (no instructions)4,467
Vehicle Violation Detector

Things used in this project

Story

Read more

Schematics

Model of signal post

Two ultrasonic TX RX are capable of covering the whole signal post as per the standards

Present Scenario at signal zones

Code

Detection code

C/C++
#include <NewPing.h>
#define  c     3830    // 261 Hz 
#define  d     3400    // 294 Hz 
#define  e     3038    // 329 Hz 
#define  f     2864    // 349 Hz 
#define  g     2550    // 392 Hz 
#define  a     2272    // 440 Hz 
#define  b     2028    // 493 Hz 
#define  C     1912    // 523 Hz 
#define  R     0
int speakerOut = 9;
int DEBUG = 1;
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 400 
int output;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 
int melody[] = {  C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };
int beats[]  = { 16, 16, 16,  8,  8,  16, 32, 16, 16, 16, 8, 8 }; 
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration  = 0;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Output
{
  
  long OnTime;     // milliseconds of on-time

  unsigned long previousMillis;   // will store last time LED was updated

  public:
  Output(long on)
  {
    
  OnTime = on;
  
  previousMillis = 0;
  }

  void Update()
  {
   unsigned long currentMillis = millis();
     
    if((currentMillis - previousMillis >= OnTime))
    {
      previousMillis = currentMillis;  // Remember the time

Serial.print("Ping: ");
unsigned int uS = sonar.ping();
output = uS / US_ROUNDTRIP_CM;
Serial.print(output);
Serial.println("cm");

    }   
  }
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
void playTone() {
  long elapsed_time = 0;
  if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 
    //  played less long than 'duration', pulse speaker HIGH and LOW
    while (elapsed_time < duration) {

      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(tone_ / 2);

      // DOWN
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone_ / 2);

      // Keep track of how long we pulsed
      elapsed_time += (tone_);
    } 
  }
  else { // Rest beat; loop times delay
    for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
      delayMicroseconds(duration);  
    }                                
  }                                 
}
  ///////////////////////////////////////////////////////////////////////////////////////////////////////


class Buzzer
{
 
  long OnTime1;     // milliseconds of on-time

  unsigned long previousMillis1;   // will store last time LED was updated

public: 
  Buzzer(long off)
  {
    
  OnTime1 = off;
  previousMillis1 = 0;
  } 
  void Update()
  {
    unsigned long currentMillis1 = millis();
     
    if((currentMillis1 - previousMillis1 >= OnTime1))
    {
      previousMillis1 = currentMillis1;  // Remember the time

      if(output > 0)
{
  Serial.println("im done");

   for (int i=0; i<MAX_COUNT; i++) {
    tone_ = melody[i];
    beat = beats[i];

    duration = beat * tempo; // Set up timing

    playTone(); 
    // A pause between notes...
    delayMicroseconds(pause);
}
}
}
}
};

Output out(100);
Buzzer buzzer(101);

///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
   pinMode(speakerOut, OUTPUT);
Serial.begin(9600); 
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
delay(50); 
out.Update();
buzzer.Update();
}

Uploading Code

C/C++
Merge this with Detection code and burn it in arduino for complete project
#include <SoftwareSerial.h>
#include <stdlib.h>



// replace with your channel's thingspeak API key
String apiKey = "ZP1PZO62773HXWVU";

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // RX, TX

// this runs once
void setup() {
// initialize the digital pin as an output.
Serial.begin(9600);
// enable software serial
ser.begin(9600);

// reset ESP8266
ser.println("AT+RST");
connectWiFi;
}
// the loop
void loop() {

Serial.println(output);

// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);

if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}

// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(output);
getStr += "\r\n\r\n";

// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);

if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}

// thingspeak needs 15 sec delay between updates
delay(16000);
}
boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    ser.println("OK");
    return true;
  }else{
    return false;
  }
}

Credits

Gokul Shrinivas

Gokul Shrinivas

3 projects • 8 followers

Comments