Proof of concept for distance and temperature monitoring

Dependencies:   mbed mbedConnectorInterface mbedEndpointNetwork

Committer:
Luminoscity
Date:
Fri May 01 23:21:36 2015 +0000
Revision:
0:cb422b231ea5
Child:
7:939fdc8df95b
Demo Temp and Distance Program;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Luminoscity 0:cb422b231ea5 1 /* Copyright (c) 2013 Prabhu Desai
Luminoscity 0:cb422b231ea5 2 * pdtechworld@gmail.com
Luminoscity 0:cb422b231ea5 3 *
Luminoscity 0:cb422b231ea5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Luminoscity 0:cb422b231ea5 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Luminoscity 0:cb422b231ea5 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Luminoscity 0:cb422b231ea5 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Luminoscity 0:cb422b231ea5 8 * furnished to do so, subject to the following conditions:
Luminoscity 0:cb422b231ea5 9 *
Luminoscity 0:cb422b231ea5 10 * The above copyright notice and this permission notice shall be included in all copies or
Luminoscity 0:cb422b231ea5 11 * substantial portions of the Software.
Luminoscity 0:cb422b231ea5 12 *
Luminoscity 0:cb422b231ea5 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Luminoscity 0:cb422b231ea5 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Luminoscity 0:cb422b231ea5 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Luminoscity 0:cb422b231ea5 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Luminoscity 0:cb422b231ea5 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Luminoscity 0:cb422b231ea5 18 */
Luminoscity 0:cb422b231ea5 19
Luminoscity 0:cb422b231ea5 20
Luminoscity 0:cb422b231ea5 21 #include "hcsr04.h"
Luminoscity 0:cb422b231ea5 22
Luminoscity 0:cb422b231ea5 23
Luminoscity 0:cb422b231ea5 24 HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
Luminoscity 0:cb422b231ea5 25 trigger(TrigPin), echo(EchoPin)
Luminoscity 0:cb422b231ea5 26 {
Luminoscity 0:cb422b231ea5 27 pulsetime.stop();
Luminoscity 0:cb422b231ea5 28 pulsetime.reset();
Luminoscity 0:cb422b231ea5 29 echo.rise(this,&HCSR04::isr_rise);
Luminoscity 0:cb422b231ea5 30 echo.fall(this,&HCSR04::isr_fall);
Luminoscity 0:cb422b231ea5 31 trigger=0;
Luminoscity 0:cb422b231ea5 32 }
Luminoscity 0:cb422b231ea5 33
Luminoscity 0:cb422b231ea5 34 HCSR04::~HCSR04()
Luminoscity 0:cb422b231ea5 35 {
Luminoscity 0:cb422b231ea5 36 }
Luminoscity 0:cb422b231ea5 37
Luminoscity 0:cb422b231ea5 38 void HCSR04::isr_rise(void)
Luminoscity 0:cb422b231ea5 39 {
Luminoscity 0:cb422b231ea5 40 pulsetime.start();
Luminoscity 0:cb422b231ea5 41 }
Luminoscity 0:cb422b231ea5 42 void HCSR04::start(void)
Luminoscity 0:cb422b231ea5 43 {
Luminoscity 0:cb422b231ea5 44 trigger=1;
Luminoscity 0:cb422b231ea5 45 wait_us(10);
Luminoscity 0:cb422b231ea5 46 trigger=0;
Luminoscity 0:cb422b231ea5 47 }
Luminoscity 0:cb422b231ea5 48
Luminoscity 0:cb422b231ea5 49 void HCSR04::isr_fall(void)
Luminoscity 0:cb422b231ea5 50 {
Luminoscity 0:cb422b231ea5 51 pulsetime.stop();
Luminoscity 0:cb422b231ea5 52 pulsedur = pulsetime.read_us();
Luminoscity 0:cb422b231ea5 53 distance= (pulsedur*343)/20000;
Luminoscity 0:cb422b231ea5 54 pulsetime.reset();
Luminoscity 0:cb422b231ea5 55 }
Luminoscity 0:cb422b231ea5 56
Luminoscity 0:cb422b231ea5 57 void HCSR04::rise (void (*fptr)(void))
Luminoscity 0:cb422b231ea5 58 {
Luminoscity 0:cb422b231ea5 59 echo.rise(fptr);
Luminoscity 0:cb422b231ea5 60 }
Luminoscity 0:cb422b231ea5 61 void HCSR04::fall (void (*fptr)(void))
Luminoscity 0:cb422b231ea5 62 {
Luminoscity 0:cb422b231ea5 63 echo.fall(fptr);
Luminoscity 0:cb422b231ea5 64 }
Luminoscity 0:cb422b231ea5 65
Luminoscity 0:cb422b231ea5 66 unsigned int HCSR04::get_dist_cm()
Luminoscity 0:cb422b231ea5 67 {
Luminoscity 0:cb422b231ea5 68 return distance;
Luminoscity 0:cb422b231ea5 69 }
Luminoscity 0:cb422b231ea5 70 unsigned int HCSR04::get_pulse_us()
Luminoscity 0:cb422b231ea5 71 {
Luminoscity 0:cb422b231ea5 72 return pulsedur;
Luminoscity 0:cb422b231ea5 73 }
Luminoscity 0:cb422b231ea5 74
Luminoscity 0:cb422b231ea5 75
Luminoscity 0:cb422b231ea5 76
Luminoscity 0:cb422b231ea5 77 /*******************************************************
Luminoscity 0:cb422b231ea5 78 Here is a sample code usage
Luminoscity 0:cb422b231ea5 79 *********************************************************
Luminoscity 0:cb422b231ea5 80 #include "hcsr04.h"
Luminoscity 0:cb422b231ea5 81 HCSR04 usensor(p25,p6);
Luminoscity 0:cb422b231ea5 82 int main()
Luminoscity 0:cb422b231ea5 83 {
Luminoscity 0:cb422b231ea5 84 unsigned char count=0;
Luminoscity 0:cb422b231ea5 85 while(1) {
Luminoscity 0:cb422b231ea5 86 usensor.start();
Luminoscity 0:cb422b231ea5 87 wait_ms(500);
Luminoscity 0:cb422b231ea5 88 dist=usensor.get_dist_cm();
Luminoscity 0:cb422b231ea5 89 lcd.cls();
Luminoscity 0:cb422b231ea5 90 lcd.locate(0,0);
Luminoscity 0:cb422b231ea5 91 lcd.printf("cm:%ld",dist );
Luminoscity 0:cb422b231ea5 92
Luminoscity 0:cb422b231ea5 93 count++;
Luminoscity 0:cb422b231ea5 94 lcd.locate(0,1);
Luminoscity 0:cb422b231ea5 95 lcd.printf("Distance =%d",count);
Luminoscity 0:cb422b231ea5 96
Luminoscity 0:cb422b231ea5 97 }
Luminoscity 0:cb422b231ea5 98 */