Gert Lauritsen / HCSR04

Dependents:   AdrianLysShow AdrianLysShow

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.h Source File

HCSR04.h

00001 //---------------------------------------------------------
00002 //Class for measure distance with the HCSR04 sensor
00003 /*
00004 Eksemple
00005 void distance(long Distance) {      
00006         printf("Distance: %d   \r",Distance);    
00007 }
00008 
00009 int main()
00010 {
00011     sensor= new HCSR04(p5,p6,distance); 
00012     printf("Test af sensor\r\n");
00013     while(1) {
00014        sensor->Trigger();
00015        wait(0.1); 
00016 
00017     }
00018 }
00019 Datasheet: http://www.micropik.com/PDF/HCSR04.pdf
00020 */
00021 #include "mbed.h"
00022 typedef void (*callback_type)(long); 
00023  
00024 class HCSR04 {
00025   public:
00026     HCSR04(PinName t, PinName e,callback_type _callback);
00027     void Trigger(); //starts the trigger
00028     
00029     private:
00030         DigitalOut trig;
00031         InterruptIn echo;
00032         Timer timer;
00033         Timeout *pulsWidth;
00034         callback_type callback;
00035         long dist_cm;
00036         
00037         void Trigoff(); //Turnoff the trigger
00038         void DistResult();
00039         void StartTimer(); 
00040 };