Simple code for multiple sensor test using K64f

Dependencies:   mbed MQ2 FXOS8700Q DHT TextLCD ESP8266

Committer:
abhishek_gupta
Date:
Thu Mar 25 10:44:23 2021 +0000
Revision:
5:a981584597fb
This code represents the use of multiple sensors in a simple way in order to create a small scale smart home system which runs on its own

Who changed what in which revision?

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