Vincent Hervieux / Mbed OS Nucleo_radar

Dependencies:   HC_SR04_Ultrasonic_Library PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers radar.cpp Source File

radar.cpp

00001 #include "radar.h"
00002 #include "client.h"
00003 
00004 /* local function declarations */
00005 void dist(int distance);
00006 
00007 /* variable(s) definition(s) */
00008 int volatile position=RANGE_NONE;
00009 ultrasonic mu(PB_8, PB_9, .1, 1, &dist);
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut led3(LED3);
00013 // Global flip count variable
00014 int volatile flipcount=0;
00015 
00016 DigitalOut led4(PC_6); // external LED or buzzer
00017 //PinDetect pb(PC_13);
00018 Ticker flipper;
00019 
00020 /* function(s) definition(s) */
00021 void dist(int distance)
00022 {
00023     //put code here to execute when the distance has changed
00024     printf("Distance %d mm\r\n", distance);
00025     send_data2(distance);
00026     if (distance < 300) position = RANGE_CLOSE;
00027     else if (distance < 600) position = RANGE_MID;
00028     else if (distance < 1000) position = RANGE_FAR;
00029     else position = RANGE_NONE;
00030     
00031     led3 = (position >= RANGE_CLOSE);
00032     led2 = (position >= RANGE_MID);
00033     led1 = (position >= RANGE_FAR);
00034 }
00035 
00036 
00037 void flip() {
00038     if(RANGE_NONE == position) {
00039         led4 = 0;
00040         return;
00041     }
00042     flipcount++;
00043     flipcount = flipcount % (RANGE_MAX - position);
00044     if(flipcount == 0) {
00045         led4 = !led4;
00046     }
00047 }
00048 
00049 // run a radar forever
00050 void radar() {
00051     //start measuring the distance
00052     mu.startUpdates();
00053     // the address of the function to be attached (flip) and the interval (2 seconds)
00054     flipper.attach(&flip, 0.2); 
00055  
00056     while(1)
00057     {
00058         //Do something else here
00059         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
00060                                 //the class checks if dist needs to be called.
00061     }
00062 }