Vehicle Detection

Dependencies:   mbed HCSR04 DHT

Committer:
saranyachitta
Date:
Thu Mar 29 18:40:24 2018 +0000
Revision:
2:74cfa9f8251d
Parent:
1:8711db3189d0
Child:
3:d520a6ecf90c
Obstacle avoidance using ultrasonic sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khp007 0:a804a58b647b 1 #include "mbed.h"
khp007 0:a804a58b647b 2 #include "HCSR04.h"
khp007 0:a804a58b647b 3
saranyachitta 2:74cfa9f8251d 4 Serial pc(USBTX,USBRX);
saranyachitta 2:74cfa9f8251d 5 Serial device(PTC17,NC); // Create a serial connection to pc through the mbed USB cable
khp007 0:a804a58b647b 6
khp007 0:a804a58b647b 7 void dist(int distance)
khp007 0:a804a58b647b 8 {
khp007 0:a804a58b647b 9 //put code here to execute when the distance has changed
khp007 0:a804a58b647b 10 printf("Distance %d mm\r\n", distance);
khp007 0:a804a58b647b 11 }
khp007 0:a804a58b647b 12
khp007 0:a804a58b647b 13 HCSR04 sensor(D8,D9);
khp007 0:a804a58b647b 14 DigitalOut Green(LED2);
khp007 0:a804a58b647b 15
khp007 0:a804a58b647b 16 int main()
khp007 0:a804a58b647b 17 {
saranyachitta 2:74cfa9f8251d 18 pc.baud(115200);
saranyachitta 2:74cfa9f8251d 19 device.baud(115200);
khp007 0:a804a58b647b 20 wait_ms(4000);
khp007 0:a804a58b647b 21
khp007 0:a804a58b647b 22 int echoVal = sensor.echo_duration();
khp007 0:a804a58b647b 23 Green = 1;
khp007 0:a804a58b647b 24
khp007 0:a804a58b647b 25 while(1){
khp007 0:a804a58b647b 26 int a = sensor.echo_duration();
khp007 0:a804a58b647b 27 if (a - echoVal > 75 || echoVal - a > 75){
khp007 0:a804a58b647b 28 Green=0;
khp007 0:a804a58b647b 29 wait(.2);
khp007 0:a804a58b647b 30 Green=1;
khp007 0:a804a58b647b 31 wait(.2);
khp007 0:a804a58b647b 32 }
saranyachitta 2:74cfa9f8251d 33 if(a > 1 and a <= 170)
saranyachitta 2:74cfa9f8251d 34 {
saranyachitta 2:74cfa9f8251d 35 device.printf("1");
saranyachitta 2:74cfa9f8251d 36 pc.printf("Object Detected: Value = 0 \r\n");
saranyachitta 2:74cfa9f8251d 37 wait(0.5);
khp007 0:a804a58b647b 38 }
saranyachitta 2:74cfa9f8251d 39 else
saranyachitta 2:74cfa9f8251d 40 {
saranyachitta 2:74cfa9f8251d 41 device.printf("0");
saranyachitta 2:74cfa9f8251d 42 pc.printf("No object detected: Value = 1 \r\n");
saranyachitta 2:74cfa9f8251d 43 wait(0.5);
saranyachitta 2:74cfa9f8251d 44 }
saranyachitta 2:74cfa9f8251d 45 wait_ms(1000);
saranyachitta 2:74cfa9f8251d 46 }
saranyachitta 2:74cfa9f8251d 47 }