Home Security Lamp / Mbed 2 deprecated HoSAL

Dependencies:   CameraC328 HCSR04 SDFileSystem WIZnetInterface mbed

Fork of HoSAL by pi bae

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rev_Hcsr04.cpp Source File

rev_Hcsr04.cpp

00001 /*
00002 # coded by revival / uasonice (at) gmail.com
00003 # DATE: 2015/08/10 / Mon Aug 10 21:21:36 KST 2015
00004 #
00005 # DESCRIPTION:
00006 #       copied from Ultrasonic by Ryu
00007 #       
00008 */
00009 
00010 #include "mbed.h"
00011 #include <stdio.h>
00012 #include <string.h>
00013 
00014 #define DEBUG_TYPE 1
00015 #define P_ uart.printf
00016 #include "rev_config.h"
00017 
00018 #include "rev_Hcsr04.h"
00019 
00020 
00021 #if defined(USE_MEASURE_DISTANCE)
00022 long avg_dist = 0; 
00023 uint8_t index_dist = 0; 
00024 long sum_dist[3];
00025 
00026 int get_distance(HCSR04 *pH)
00027 {
00028 
00029     while (true) {
00030         if( index_dist < 3){ //ryuhs74@20150810     
00031             sum_dist[index_dist] = pH->distance();;
00032             //DM_fLN("sum_dist[%d] = %d", index_dist, sum_dist[index_dist]);
00033             index_dist ++;
00034         } else {
00035             avg_dist = 0;
00036             index_dist = 0;
00037 
00038             for(int i =0; i<3;i++){
00039                 avg_dist += sum_dist[i];
00040             }
00041 
00042             avg_dist /= 3;
00043             DM_fLN("avg: %d", avg_dist);
00044             return avg_dist;
00045         }
00046 #if 1
00047         wait_ms(10);
00048 #else
00049         break;
00050 #endif
00051     }
00052     return 8192;
00053 }
00054 #endif // defined(USE_MEASURE_DISTANCE)
00055