Child Monitor system
Dependencies: HC_SR04_Ultrasonic_proximity_sensor mbed
Fork of ultrasonic_sensor_trial by
main.cpp@0:59d1b681f30e, 2017-12-06 (annotated)
- Committer:
- Niranjan_ravi
- Date:
- Wed Dec 06 19:37:58 2017 +0000
- Revision:
- 0:59d1b681f30e
ruu
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Niranjan_ravi | 0:59d1b681f30e | 1 | #include "mbed.h" |
Niranjan_ravi | 0:59d1b681f30e | 2 | DigitalOut trigger(PTC3); |
Niranjan_ravi | 0:59d1b681f30e | 3 | DigitalOut myled(LED1); //monitor trigger |
Niranjan_ravi | 0:59d1b681f30e | 4 | DigitalOut myled2(LED2); //monitor echo |
Niranjan_ravi | 0:59d1b681f30e | 5 | DigitalIn echo(PTC2); |
Niranjan_ravi | 0:59d1b681f30e | 6 | int distance = 0; |
Niranjan_ravi | 0:59d1b681f30e | 7 | int correction = 0; |
Niranjan_ravi | 0:59d1b681f30e | 8 | Timer sonar; |
Niranjan_ravi | 0:59d1b681f30e | 9 | Serial pc(USBTX, USBRX); |
Niranjan_ravi | 0:59d1b681f30e | 10 | Serial xbee(PTC17, PTC16); |
Niranjan_ravi | 0:59d1b681f30e | 11 | char bufchar; |
Niranjan_ravi | 0:59d1b681f30e | 12 | |
Niranjan_ravi | 0:59d1b681f30e | 13 | int main() |
Niranjan_ravi | 0:59d1b681f30e | 14 | { |
Niranjan_ravi | 0:59d1b681f30e | 15 | sonar.reset(); |
Niranjan_ravi | 0:59d1b681f30e | 16 | // measure actual software polling timer delays |
Niranjan_ravi | 0:59d1b681f30e | 17 | // delay used later in time correction |
Niranjan_ravi | 0:59d1b681f30e | 18 | // start timer |
Niranjan_ravi | 0:59d1b681f30e | 19 | sonar.start(); |
Niranjan_ravi | 0:59d1b681f30e | 20 | xbee.baud(9600); |
Niranjan_ravi | 0:59d1b681f30e | 21 | // min software polling delay to read echo pin |
Niranjan_ravi | 0:59d1b681f30e | 22 | while (echo==2) {}; |
Niranjan_ravi | 0:59d1b681f30e | 23 | myled2 = 0; |
Niranjan_ravi | 0:59d1b681f30e | 24 | // stop timer |
Niranjan_ravi | 0:59d1b681f30e | 25 | sonar.stop(); |
Niranjan_ravi | 0:59d1b681f30e | 26 | // read timer |
Niranjan_ravi | 0:59d1b681f30e | 27 | correction = sonar.read_us(); |
Niranjan_ravi | 0:59d1b681f30e | 28 | printf("Approximate software overhead timer delay is %d uS\n\r",correction); |
Niranjan_ravi | 0:59d1b681f30e | 29 | |
Niranjan_ravi | 0:59d1b681f30e | 30 | //Loop to read Sonar distance values, scale, and print |
Niranjan_ravi | 0:59d1b681f30e | 31 | while(1) { |
Niranjan_ravi | 0:59d1b681f30e | 32 | // trigger sonar to send a ping |
Niranjan_ravi | 0:59d1b681f30e | 33 | |
Niranjan_ravi | 0:59d1b681f30e | 34 | |
Niranjan_ravi | 0:59d1b681f30e | 35 | |
Niranjan_ravi | 0:59d1b681f30e | 36 | trigger = 1; |
Niranjan_ravi | 0:59d1b681f30e | 37 | myled = 1; |
Niranjan_ravi | 0:59d1b681f30e | 38 | myled2 = 0; |
Niranjan_ravi | 0:59d1b681f30e | 39 | sonar.reset(); |
Niranjan_ravi | 0:59d1b681f30e | 40 | wait_us(10.0); |
Niranjan_ravi | 0:59d1b681f30e | 41 | trigger = 0; |
Niranjan_ravi | 0:59d1b681f30e | 42 | myled = 0; |
Niranjan_ravi | 0:59d1b681f30e | 43 | //wait for echo high |
Niranjan_ravi | 0:59d1b681f30e | 44 | while (echo==0) {}; |
Niranjan_ravi | 0:59d1b681f30e | 45 | myled2=echo; |
Niranjan_ravi | 0:59d1b681f30e | 46 | //echo high, so start timer |
Niranjan_ravi | 0:59d1b681f30e | 47 | sonar.start(); |
Niranjan_ravi | 0:59d1b681f30e | 48 | //wait for echo low |
Niranjan_ravi | 0:59d1b681f30e | 49 | while (echo==1) {}; |
Niranjan_ravi | 0:59d1b681f30e | 50 | //stop timer and read value |
Niranjan_ravi | 0:59d1b681f30e | 51 | sonar.stop(); |
Niranjan_ravi | 0:59d1b681f30e | 52 | //subtract software overhead timer delay and scale to cm |
Niranjan_ravi | 0:59d1b681f30e | 53 | distance = (sonar.read_us()-correction)/58.0; |
Niranjan_ravi | 0:59d1b681f30e | 54 | myled2 = 0; |
Niranjan_ravi | 0:59d1b681f30e | 55 | if(distance<=300) |
Niranjan_ravi | 0:59d1b681f30e | 56 | { |
Niranjan_ravi | 0:59d1b681f30e | 57 | xbee.printf("%d \n\r", distance); |
Niranjan_ravi | 0:59d1b681f30e | 58 | printf(" The baby is at %d cm from the Proximity sensor \n\r", distance); |
Niranjan_ravi | 0:59d1b681f30e | 59 | } |
Niranjan_ravi | 0:59d1b681f30e | 60 | //wait so that any echo(s) return before sending another ping |
Niranjan_ravi | 0:59d1b681f30e | 61 | wait(0.15); |
Niranjan_ravi | 0:59d1b681f30e | 62 | } |
Niranjan_ravi | 0:59d1b681f30e | 63 | } |