vehicle detection

Dependencies:   mbed HCSR04 DHT

Committer:
khp007
Date:
Tue Mar 20 03:32:56 2018 +0000
Revision:
0:a804a58b647b
Child:
1:8711db3189d0
hcsr04 for pill dispenser, detects motion

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
khp007 0:a804a58b647b 4 Serial pc(USBTX,USBRX); // Create a serial connection to pc through the mbed USB cable
khp007 0:a804a58b647b 5
khp007 0:a804a58b647b 6 void dist(int distance)
khp007 0:a804a58b647b 7 {
khp007 0:a804a58b647b 8 //put code here to execute when the distance has changed
khp007 0:a804a58b647b 9 printf("Distance %d mm\r\n", distance);
khp007 0:a804a58b647b 10 }
khp007 0:a804a58b647b 11
khp007 0:a804a58b647b 12 HCSR04 sensor(D8,D9);
khp007 0:a804a58b647b 13 DigitalOut Green(LED2);
khp007 0:a804a58b647b 14
khp007 0:a804a58b647b 15 int main()
khp007 0:a804a58b647b 16 {
khp007 0:a804a58b647b 17 wait_ms(4000);
khp007 0:a804a58b647b 18
khp007 0:a804a58b647b 19 int echoVal = sensor.echo_duration();
khp007 0:a804a58b647b 20 Green = 1;
khp007 0:a804a58b647b 21
khp007 0:a804a58b647b 22 while(1){
khp007 0:a804a58b647b 23 int a = sensor.echo_duration();
khp007 0:a804a58b647b 24 if (a - echoVal > 75 || echoVal - a > 75){
khp007 0:a804a58b647b 25 Green=0;
khp007 0:a804a58b647b 26 wait(.2);
khp007 0:a804a58b647b 27 Green=1;
khp007 0:a804a58b647b 28 wait(.2);
khp007 0:a804a58b647b 29 }
khp007 0:a804a58b647b 30 pc.printf("%d \n\r",a);
khp007 0:a804a58b647b 31 wait_ms(50);
khp007 0:a804a58b647b 32 }
khp007 0:a804a58b647b 33 }
khp007 0:a804a58b647b 34