Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HC_SR04_Ultrasonic_Library mbed-rtos mbed-src mbed HCSR04
Fork of Nucleo_UltrasonicHelloWorld by
main.cpp
00001 #include "mbed.h" 00002 00003 //#define TEST_ULTRASONIC 00004 #ifdef TEST_ULTRASONIC 00005 #include "ultrasonic.h" 00006 #else 00007 #include "hcsr04.h" 00008 #endif 00009 00010 00011 //#define USE_THREAD 00012 #ifdef USE_THREAD 00013 #include "rtos.h" 00014 #endif 00015 00016 #define CHECK_DISTANCE 1000 00017 00018 int sum_dist[3] = {0,}; //ryuhs74@20150712 - 00019 int avg_dist = 0; //ryuhs74@20150712 - 00020 int index_dist = 0; //ryuhs74@20150712 - 00021 00022 Serial pc(USBTX, USBRX); // tx, rx 00023 00024 #ifdef USE_THREAD 00025 int isRun_thread = 0; 00026 Thread *pThread = NULL; 00027 00028 void camera_thread( void const* args) 00029 { 00030 isRun_thread = 1; 00031 00032 while(isRun_thread){ 00033 //ryuhs74@20150713 START - TEST code 00034 for(int i = 0; i<100;i++){ 00035 wait(1); 00036 } 00037 break; 00038 //ryuhs74@20150713 - TEST code END 00039 } 00040 00041 if( pThread != NULL ){ 00042 pThread->terminate(); 00043 pThread = NULL; 00044 } 00045 isRun_thread = 0; 00046 return; 00047 } 00048 #endif 00049 00050 void dist(int distance) 00051 { 00052 //put code here to happen when the distance is changed 00053 pc.printf("Distance changed to %d mm\r\n", distance); 00054 } 00055 00056 00057 #ifdef TEST_ULTRASONIC 00058 ultrasonic mu(D8, D9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 00059 //have updates every .1 seconds and a timeout after 1 00060 //second, and call dist when the distance changes 00061 #else 00062 HCSR04 sensor(D12, D11); 00063 #endif 00064 00065 int main() 00066 { 00067 #ifdef TEST_ULTRASONIC 00068 pc.printf("Start ----> TEST_ULTRASONIC\n"); 00069 #else 00070 pc.printf("Start ----> HCSR04\n"); 00071 #endif 00072 00073 #ifdef TEST_ULTRASONIC 00074 mu.startUpdates();//start mesuring the distance 00075 #endif 00076 00077 while(1) { 00078 00079 pc.printf("Before mu.checkDistance();\n"); 00080 //Do something else here 00081 #ifdef TEST_ULTRASONIC 00082 mu.checkDistance(); //call checkDistance() as much as possible, as this is where 00083 #else 00084 long distance = sensor.distance(); 00085 #endif 00086 //the class checks if dist needs to be called. 00087 pc.printf("After mu.checkDistance();\n"); 00088 00089 //ryuhs74@20150712 START - 00090 if( index_dist < 3){ 00091 pc.printf("Before mu.getCurrentDistance();\n"); 00092 #ifdef TEST_ULTRASONIC 00093 sum_dist[index_dist] = mu.getCurrentDistance(); 00094 #else 00095 sum_dist[index_dist] = distance; 00096 #endif 00097 pc.printf("sum_dist[index_dist(%d)] = %d\n", index_dist, sum_dist[index_dist]); 00098 index_dist ++; 00099 } else { 00100 pc.printf("Before index_dist( %d ) ", index_dist); 00101 index_dist = 0; 00102 pc.printf("After index_dist( %d )\n", index_dist); 00103 00104 for(int i =0; i<3;i++){ 00105 avg_dist += sum_dist[i]; 00106 } 00107 00108 //avg_dist /= avg_dist; 00109 00110 if( avg_dist <= CHECK_DISTANCE ){ 00111 #ifdef USE_THREAD 00112 if( isRun_thread == 0 ){ 00113 Thread thread(camera_thread); 00114 00115 if( pThread == NULL) 00116 pThread = (Thread*)&thread; 00117 00118 } 00119 #else 00120 #endif 00121 00122 } 00123 } 00124 pc.printf("wait(1)\n"); 00125 //ryuhs74@20150712 - END 00126 wait(1.0); //ryuhs7474@20150713 - 초당 한번씩 딜레이를 준다 00127 } 00128 }
Generated on Thu Jul 14 2022 20:01:46 by
1.7.2
