
Distance Sensor working without LCD
Dependencies: Speaker mbed 4DGL-uLCD-SE hcsr04
Revision 12:730a95b81992, committed 2020-12-16
- Comitter:
- fiachra
- Date:
- Wed Dec 16 17:20:46 2020 +0000
- Parent:
- 11:68194bd43e0b
- Commit message:
- lib;
Changed in this revision
diff -r 68194bd43e0b -r 730a95b81992 Speaker.h --- a/Speaker.h Wed Dec 16 15:05:27 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -class Speaker -{ -public: - Speaker(PinName pin) : _pin(pin) { -// _pin(pin) means pass pin to the Speaker Constructor -// precompute 32 sample points on one sine wave cycle -// used for continuous sine wave output later - for(int k=0; k<32; k++) { - Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0)); - // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg - } - - } -// class method to play a note based on AnalogOut class - void PlayNote(float frequency, float duration, float volume) { - // scale samples using current volume level arg - for(int k=0; k<32; k++) { - Analog_scaled_data[k] = Analog_out_data[k] * volume; - } - // reset to start of sample array - i=0; - // turn on timer interrupts to start sine wave output - Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0)); - // play note for specified time - wait(duration); - // turns off timer interrupts - Sample_Period.detach(); - // sets output to mid range - analog zero - this->_pin.write_u16(32768); - - } -private: -// sets up specified pin for analog using AnalogOut class - AnalogOut _pin; - // set up a timer to be used for sample rate interrupts - Ticker Sample_Period; - - //variables used by interrupt routine and PlayNote - volatile int i; - short unsigned Analog_out_data[32]; - short unsigned Analog_scaled_data[32]; - -// Interrupt routine -// used to output next analog sample whenever a timer interrupt occurs - void Sample_timer_interrupt(void) { - // send next analog sample out to D to A - this->_pin.write_u16(Analog_scaled_data[i]); - // increment pointer and wrap around back to 0 at 32 - i = (i+1) & 0x01F; - } -}; -
diff -r 68194bd43e0b -r 730a95b81992 Speaker.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.lib Wed Dec 16 17:20:46 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/fiachra/code/Speaker/#20c2337179bc
diff -r 68194bd43e0b -r 730a95b81992 hcsr04.cpp --- a/hcsr04.cpp Wed Dec 16 15:05:27 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* Copyright (c) 2013 Prabhu Desai - * pdtechworld@gmail.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -#include "hcsr04.h" - - -HCSR04::HCSR04(PinName TrigPin,PinName EchoPin): - trigger(TrigPin), echo(EchoPin) -{ - pulsetime.stop(); - pulsetime.reset(); - echo.rise(this,&HCSR04::isr_rise); - echo.fall(this,&HCSR04::isr_fall); - trigger=0; -} - -HCSR04::~HCSR04() -{ -} - -void HCSR04::isr_rise(void) -{ - pulsetime.start(); -} -void HCSR04::start(void) -{ - trigger=1; - wait_us(10); - trigger=0; -} - -void HCSR04::isr_fall(void) -{ - pulsetime.stop(); - pulsedur = pulsetime.read_us(); - distance= (pulsedur*343)/20000; - pulsetime.reset(); -} - -void HCSR04::rise (void (*fptr)(void)) -{ - echo.rise(fptr); -} -void HCSR04::fall (void (*fptr)(void)) -{ - echo.fall(fptr); -} - -unsigned int HCSR04::get_dist_cm() -{ - return distance; -} -unsigned int HCSR04::get_pulse_us() -{ - return pulsedur; -} - - - -/******************************************************* - Here is a sample code usage -********************************************************* -#include "hcsr04.h" -HCSR04 usensor(p25,p6); -int main() -{ - unsigned char count=0; - while(1) { - usensor.start(); - wait_ms(500); - dist=usensor.get_dist_cm(); - lcd.cls(); - lcd.locate(0,0); - lcd.printf("cm:%ld",dist ); - - count++; - lcd.locate(0,1); - lcd.printf("Distance =%d",count); - - } -*/ \ No newline at end of file
diff -r 68194bd43e0b -r 730a95b81992 hcsr04.h --- a/hcsr04.h Wed Dec 16 15:05:27 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* Copyright (c) 2013 Prabhu Desai - * pdtechworld@gmail.com - * - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * For more details on the sensor : - * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef MBED_HCSR04_H -#define MBED_HCSR04_H - -#include "mbed.h" - -/** HCSR04 Class(es) - */ - -class HCSR04 -{ -public: - /** Create a HCSR04 object connected to the specified pin - * @param pin i/o pin to connect to - */ - HCSR04(PinName TrigPin,PinName EchoPin); - ~HCSR04(); - - /** Return the distance from obstacle in cm - * @param distance in cms and returns -1, in case of failure - */ - unsigned int get_dist_cm(void); - /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver. - * @param pulse duration in microseconds. - */ - unsigned int get_pulse_us(void); - /** Generates the trigger pulse of 10us on the trigger PIN. - */ - void start(void ); - void isr_rise(void); - void isr_fall(void); - void fall (void (*fptr)(void)); - void rise (void (*fptr)(void)); - - - -private: - - Timer pulsetime; - DigitalOut trigger; - InterruptIn echo; - unsigned int pulsedur; - unsigned int distance; -}; - -#endif \ No newline at end of file
diff -r 68194bd43e0b -r 730a95b81992 hcsr04.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hcsr04.lib Wed Dec 16 17:20:46 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/fiachra/code/hcsr04/#68251ad692dd
diff -r 68194bd43e0b -r 730a95b81992 main.cpp --- a/main.cpp Wed Dec 16 15:05:27 2020 +0000 +++ b/main.cpp Wed Dec 16 17:20:46 2020 +0000 @@ -1,6 +1,6 @@ #include "mbed.h" #include "hcsr04.h" -#include "Speaker.h" +#include "Speaker.h" #include "uLCD_4DGL.h" HCSR04 distSens(p25,p6); @@ -8,19 +8,17 @@ Speaker mySpeaker(p18); uLCD_4DGL uLCD(p9,p10,p11); - unsigned int dist; - float averageDistance(); void beeping(); float i; int main() { - uLCD.text_width(1); // Normal size - uLCD.text_height(1); // Normal size - uLCD.locate(10, 12); // Move cursor - uLCD.color(BLUE); // Change text color + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.locate(10, 12); + uLCD.color(BLUE); uLCD.printf(" Reverse Sensor"); uLCD.rectangle(65, 117, 126, 127, 0x0000FF); while(1) { @@ -29,15 +27,14 @@ beeping(); uLCD.filled_rectangle(66, 118, 125, 127, 0x000000); pc.printf("cm:%d\n",dist ); - uLCD.text_width(1); // Normal size - uLCD.text_height(1); // Normal size - uLCD.locate(10, 15); // Move cursor - uLCD.color(WHITE); // Change text color + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.locate(10, 15); + uLCD.color(WHITE); uLCD.printf("cm:%1d\n",dist ); - } } -float averageDistance() /** Divide the running total by 10 to get the average distance to get more accurate readings on the sensor**/ +float averageDistance() { int a = distSens.get_dist_cm(); int b = distSens.get_dist_cm(); @@ -56,8 +53,8 @@ void beeping() { int distance = averageDistance(); - - if (distance > 2000) i=0.05; //sensor defaults to 2k+ when dist<~3cm + //sensor defaults to 2k+ when dist<~3cm + if (distance > 2000) i=0.05; if (distance < 150) i=0.6; if (distance < 100) i=0.55; if (distance < 90) i=0.5; @@ -70,6 +67,5 @@ if (distance < 20) i=0.15; if (distance < 10) i=0.1; if (distance < 5) i=0.05; - mySpeaker.PlayNote(600, i, 5.0); - + mySpeaker.PlayNote(600, i, 5.0); } \ No newline at end of file