Police Traffic Control | Arduino and ASP.NET project.
This project helps police to control traffic and catch the suspects easily.

code.ino

/*
 * Police Traffic Control | Arduino and ASP.NET project.
 * This project helps police to control traffic and catch the suspects easily.
 * 
 * author: khaled alam
 */

 
#include <Servo.h>

// pins
const int greenLed = 4;
const int redLed = 2;
const int buzzer = 5;
const int lightSensor = A0;
const int servoPin = 9;

//vars changeable
int lightValue;
int sensorValue;
int continueStopping = -1;
char incomingFromSerial;

//serial const signals
const char OPEN_GATE = '1';
const char CLOSE_GATE = '2';
const char POLICE_SAY_STOP = '3';
const char BUZZ_ON = '4';
const char BUZZ_OFF = '5';
const char GREEN_ON = '6';
const char GREEN_OFF = '7';
const char RED_ON = '8';
const char RED_OFF = '9';

Servo servo;

void setup()
{
  Serial.begin(9600);
  servo.attach(servoPin);
  pinMode(greenLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop()
{

  while(Serial.available() > 0)
  {
    //read serial come from ASP.NET
    incomingFromSerial = Serial.read();

    //if police ask driver to stop
    if (incomingFromSerial == POLICE_SAY_STOP)
    {
      continueStopping = 1;
      offCorrectLed();
      onWrongLed();
      openGate();
      detect(); 
    }

    // Do action based on serial signal send from ASP.NET
    // manual actions:
    if(incomingFromSerial == CLOSE_GATE)
    {
      continueStopping = -1;
      closeGate();
      offCorrectLed();
      onWrongLed();
    }
    else if (incomingFromSerial == OPEN_GATE)
    {
      continueStopping = -1;
      openGate();
      offWrongLed();
      onCorrectLed();
    }else if (incomingFromSerial == BUZZ_ON)
    {
      continueStopping = 1;
      turnOnBuzzer();
    }else if (incomingFromSerial == BUZZ_OFF)
    {
      continueStopping = -1;
      noTone(buzzer);
    }else if (incomingFromSerial == GREEN_ON)
    {
      digitalWrite(greenLed, HIGH);    
    }else if (incomingFromSerial == GREEN_OFF)
    {
      digitalWrite(greenLed, LOW);
    }else if (incomingFromSerial == RED_ON)
    {
      digitalWrite(redLed, HIGH);
    }else if (incomingFromSerial == RED_OFF)
    {
      digitalWrite(redLed, LOW);
    }
    delay(100); // wait 100 milliseconds (0.1 second)
    Serial.flush();
  }

}

/* 
 *  detect function use as a loop to check
 * if driver follow police order and stop or not 
 */
void detect()
{
  //while police ask driver to stop
  while(continueStopping == 1)
  {
    //check if driver run before police allow
    if ( pass() )
    {
      closeGate();
      turnOnBuzzer();
      Serial.println("Catch Now..");
      break;
    }
    onWrongLed();
  }
}

void onCorrectLed()
{
  digitalWrite(greenLed, HIGH);
}

void offCorrectLed()
{
  digitalWrite(greenLed, LOW);
}

void onWrongLed()
{
  digitalWrite(redLed, HIGH);
}

void offWrongLed()
{
  digitalWrite(redLed, LOW);
}

void turnOnBuzzer()
{
  while (continueStopping == 1)
  {
    //read from ASP.NET during police stop order
    incomingFromSerial = Serial.read();
    
    tone(buzzer, 1200); // << turn on buzzer
    
    // condition to stop buzzer
    if(incomingFromSerial == OPEN_GATE || 
        incomingFromSerial == CLOSE_GATE ||
        incomingFromSerial == POLICE_SAY_STOP ||
        incomingFromSerial == BUZZ_OFF)
    {
      if (incomingFromSerial != BUZZ_OFF)openGate();
      noTone(buzzer);
      continueStopping = -1;
    }
    delay(60);
  }
}

//check if any car cover light sensor or not
boolean pass()
{
  sensorValue = analogRead(lightSensor);
  lightValue = map(sensorValue, 0, 1023, 0, 255); 

  // increase 8 to increase sensitivity)
  if (lightValue <= 8)return true;
  return false;
}

void openGate()
{
  servo.write(0);
}

void closeGate()
{
  servo.write(90);
}

void debug()
{
  Serial.print("Light Value = ");
  Serial.println(lightValue);
}

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Police_Traffic_Control
{
    public partial class _Default : Page
    {
        public static SerialPort sp;

        // Signals
        const string OPEN_GATE       = "1";
        const string CLOSE_GATE      = "2";
        const string POLICE_SAY_STOP = "3";
        const string BUZZ_ON         = "4";
        const string BUZZ_OFF        = "5";
        const string GREEN_ON        = "6";
        const string GREEN_OFF       = "7";
        const string RED_ON          = "8";
        const string RED_OFF         = "9";

        //Images
        const string welcomeImg = "~/imgs/welcome.png";
        const string openImg    = "~/imgs/gateO.jpg";
        const string closeImg   = "~/imgs/gateC.jpg";
        // etc...


        protected void Page_Load(object sender, EventArgs e)
        {
            sp = new SerialPort();
            sp.PortName = "COM3"; // change port name(if change usb pos.)
            sp.BaudRate = 9600;
            sp.Parity = Parity.None;
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.ReadTimeout = 1000;
            StatusImage.ImageUrl = welcomeImg; // default welcome img
        }

        // Change images action examples
        protected void changeImage1(object sender, EventArgs e)
        {
            StatusImage.ImageUrl = openImg;
        }
        protected void changeImage2(object sender, EventArgs e)
        {
            StatusImage.ImageUrl = closeImg;
        }
        protected void changeImageProp(object sender, EventArgs e)
        {
            StatusImage.Width = 250;
            StatusImage.Height = 300;
            StatusImage.ImageAlign = ImageAlign.Bottom;
            //...
        }



        protected void stopNow(object sender, EventArgs e) //send command to uno to open gate and turn on passing detection
        {
            GateStatus.Text = "<b style='background-color:yellow;'> >>> </b>Police is Asking Driver To Stop!";

            // Change image ex. :
            StatusImage.ImageUrl      =     openImg; 
            // add alertImg instead of openImg ^ for example

            if(!sp.IsOpen)sp.Open();
            sp.Write(POLICE_SAY_STOP); sp.Close();
        }


        protected void openGate(object sender, EventArgs e) //send command to uno to open gate
        {
            GateStatus.Text = "<b style='background-color:green;'> >>> </b>Gate Opened!";
            if (!sp.IsOpen)sp.Open();
            sp.Write(OPEN_GATE); sp.Close();
        }


        protected void closeGate(object sender, EventArgs e) //send command to uno to close gate
        {
            GateStatus.Text = "<b style='background-color:red;'> >>> </b> Gate Closed!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(CLOSE_GATE); sp.Close();

        }

        protected void buzzOn(object sender, EventArgs e) //send command to uno to turn on buzz
        {
            GateStatus.Text = "<b style='background-color:brown;'> >>> </b> Buzz On!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(BUZZ_ON); sp.Close();
        }


        protected void buzzOff(object sender, EventArgs e) //send command to uno to turn off buzz
        {
            GateStatus.Text = "<b style='background-color:gray;'> >>> </b> Buzz Off!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(BUZZ_OFF); sp.Close();
        }


        protected void greenLedOn(object sender, EventArgs e) //send command to uno to turn on green led
        {
            GateStatus.Text = "<b style='background-color:green;'> >>> </b> Green Led On!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(GREEN_ON); sp.Close();
        }


        protected void greenLedOff(object sender, EventArgs e) //send command to uno to turn off green led
        {
            GateStatus.Text = "<b style='background-color:green;'> >>> </b> Green Led Off!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(GREEN_OFF); sp.Close();
        }


        protected void redLedOn(object sender, EventArgs e) //send command to uno to turn on red led
        {
            GateStatus.Text = "<b style='background-color:red;'> >>> </b> Red Led On!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(RED_ON); sp.Close();
        }


        protected void redLedOff(object sender, EventArgs e) //send command to uno to turn off red led
        {
            GateStatus.Text = "<b style='background-color:red;'> >>> </b> Red Led Off!";
            if (!sp.IsOpen) sp.Open();
            sp.Write(RED_OFF); sp.Close();
        }

    }
}
Facebook Comments
Last modified: July 20, 2019