temperature and distance

Fork of TrashSensors by Corey Ford

Committer:
Luminoscity
Date:
Thu May 28 23:11:33 2015 +0000
Revision:
1:9cd59726cc5e
Parent:
0:3f335fc50cef
Calibration at 5V;

Who changed what in which revision?

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