vehicle detection

Dependencies:   mbed HCSR04 DHT

main.cpp

Committer:
saranyachitta
Date:
2018-03-29
Revision:
2:74cfa9f8251d
Parent:
1:8711db3189d0
Child:
3:d520a6ecf90c

File content as of revision 2:74cfa9f8251d:

#include "mbed.h"
#include "HCSR04.h"

 Serial pc(USBTX,USBRX);
 Serial device(PTC17,NC);     // Create a serial connection to pc through the mbed USB cable

 void dist(int distance)
{
    //put code here to execute when the distance has changed
    printf("Distance %d mm\r\n", distance);
}

HCSR04 sensor(D8,D9);
DigitalOut Green(LED2);

int main()
{
    pc.baud(115200);
device.baud(115200);
   wait_ms(4000);

int echoVal = sensor.echo_duration();
   Green = 1;
   
    while(1){
    int a = sensor.echo_duration();
    if (a - echoVal > 75 || echoVal - a > 75){
        Green=0;
        wait(.2);
        Green=1;
        wait(.2);
    }
        if(a > 1 and a <= 170)
         {
         device.printf("1"); 
         pc.printf("Object Detected: Value = 0 \r\n");
         wait(0.5);
    }
    else
       {
             device.printf("0");
               pc.printf("No object detected: Value = 1 \r\n");  
               wait(0.5);
       }
    wait_ms(1000);
   }
   }