mbedbidos / Mbed 2 deprecated ultrasonido_2

Dependencies:   mbed

Committer:
jiuk
Date:
Mon Nov 19 02:02:10 2018 +0000
Revision:
1:a656f3ed73e5
Parent:
0:c9881f6aef19
ultrasonido

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:c9881f6aef19 1 #include "mbed.h"
jiuk 0:c9881f6aef19 2
jiuk 0:c9881f6aef19 3 DigitalOut trigger(PC_12);
jiuk 0:c9881f6aef19 4 DigitalOut myled(LED1); //monitor trigger
jiuk 0:c9881f6aef19 5 DigitalOut myled2(LED2); //monitor echo
jiuk 0:c9881f6aef19 6 DigitalIn echo(PC_10);
jiuk 0:c9881f6aef19 7 int distance = 0;
jiuk 0:c9881f6aef19 8 int correction = 0;
jiuk 0:c9881f6aef19 9 Timer sonar;
jiuk 0:c9881f6aef19 10
jiuk 0:c9881f6aef19 11 int main()
jiuk 0:c9881f6aef19 12 {
jiuk 1:a656f3ed73e5 13 while(1)
jiuk 1:a656f3ed73e5 14 {
jiuk 0:c9881f6aef19 15 sonar.reset();
jiuk 0:c9881f6aef19 16 // measure actual software polling timer delays
jiuk 0:c9881f6aef19 17 // delay used later in time correction
jiuk 0:c9881f6aef19 18 // start timer
jiuk 0:c9881f6aef19 19 sonar.start();
jiuk 0:c9881f6aef19 20 // min software polling delay to read echo pin
jiuk 0:c9881f6aef19 21 while (echo==2) {};
jiuk 0:c9881f6aef19 22 myled2 = 0;
jiuk 0:c9881f6aef19 23 // stop timer
jiuk 0:c9881f6aef19 24 sonar.stop();
jiuk 0:c9881f6aef19 25 // read timer
jiuk 0:c9881f6aef19 26 correction = sonar.read_us();
jiuk 0:c9881f6aef19 27 printf("Approximate software overhead timer delay is %d uS\n\r",correction);
jiuk 0:c9881f6aef19 28
jiuk 0:c9881f6aef19 29 //Loop to read Sonar distance values, scale, and print
jiuk 0:c9881f6aef19 30 while(1) {
jiuk 0:c9881f6aef19 31 // trigger sonar to send a ping
jiuk 0:c9881f6aef19 32 trigger = 1;
jiuk 0:c9881f6aef19 33 myled = 1;
jiuk 0:c9881f6aef19 34 myled2 = 0;
jiuk 0:c9881f6aef19 35 sonar.reset();
jiuk 0:c9881f6aef19 36 wait_us(10.0);
jiuk 0:c9881f6aef19 37 trigger = 0;
jiuk 0:c9881f6aef19 38 myled = 0;
jiuk 0:c9881f6aef19 39 //wait for echo high
jiuk 0:c9881f6aef19 40 while (echo==0) {};
jiuk 0:c9881f6aef19 41 myled2=echo;
jiuk 0:c9881f6aef19 42 //echo high, so start timer
jiuk 0:c9881f6aef19 43 sonar.start();
jiuk 0:c9881f6aef19 44 //wait for echo low
jiuk 0:c9881f6aef19 45 while (echo==1) {};
jiuk 0:c9881f6aef19 46 //stop timer and read value
jiuk 0:c9881f6aef19 47 sonar.stop();
jiuk 0:c9881f6aef19 48 //subtract software overhead timer delay and scale to cm
jiuk 0:c9881f6aef19 49 distance = (sonar.read_us()-correction)/58.0;
jiuk 0:c9881f6aef19 50 myled2 = 0;
jiuk 0:c9881f6aef19 51 printf(" %d cm \n\r",distance);
jiuk 0:c9881f6aef19 52 //wait so that any echo(s) return before sending another ping
jiuk 0:c9881f6aef19 53 wait(0.2);
jiuk 1:a656f3ed73e5 54 }
jiuk 0:c9881f6aef19 55 }
jiuk 0:c9881f6aef19 56 }