Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE Servo HC_SR04_Ultrasonic_Library mbed-rtos mbed
Revision 1:21d56f84fe51, committed 2018-05-01
- Comitter:
- davidscque
- Date:
- Tue May 01 04:54:34 2018 +0000
- Parent:
- 0:0e43a69efacd
- Commit message:
- final edit;
Changed in this revision
diff -r 0e43a69efacd -r 21d56f84fe51 Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Tue May 01 04:54:34 2018 +0000 @@ -0,0 +1,52 @@ +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 0e43a69efacd -r 21d56f84fe51 WavPlayer.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WavPlayer.lib Tue May 01 04:54:34 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/users/p07gbar/code/WavPlayer/#a7380cfc1987
diff -r 0e43a69efacd -r 21d56f84fe51 lab3part1_4180.lib --- a/lab3part1_4180.lib Mon Apr 23 17:55:06 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
diff -r 0e43a69efacd -r 21d56f84fe51 main.cpp --- a/main.cpp Mon Apr 23 17:55:06 2018 +0000 +++ b/main.cpp Tue May 01 04:54:34 2018 +0000 @@ -1,10 +1,14 @@ -// Hello World to sweep a servo through its full range - #include "mbed.h" #include "Servo.h" #include "ultrasonic.h" #include "rtos.h" #include "uLCD_4DGL.h" +#include "Speaker.h" +//#include "PinDetect.h" + +//#include <mpr121.h> + + float dist2; Mutex mut; void dist(int distance) @@ -17,88 +21,149 @@ } Thread thread; Thread thread2; +Thread thread3; +Thread thread4; DigitalOut led(p8); ultrasonic mu(p6, p7, .1, 1, &dist); Servo myservo(p21); PwmOut myled(LED1); -AnalogIn mypotentiometer(p20); +//AnalogIn mypotentiometer(p20); +Speaker speaker(p18); RawSerial blue(p9,p10); uLCD_4DGL uLCD(p13, p14, p15); +DigitalOut LEDR(p26); //DigitalOuts for LEDs +DigitalOut LEDG(p29); +DigitalOut LEDB(p30); + +DigitalIn pb3(p19); + +DigitalOut LED(LED1); +DigitalIn pb1(p23); //locked +DigitalIn pb2(p24); //unlocked + +Serial pc(USBTX,USBRX); +InterruptIn interrupt(p25); +I2C i2c(p28, p27); +AnalogIn photocell(p20); +//Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); + + float i =0.0; float j=0.0; + +void LEDT(){ + while(1){ + + + + + mut.lock(); + if(photocell<0.010) + { + mut.unlock(); + Thread::wait(100000); + Thread::wait(100000); + Thread::wait(100000); + mut.lock(); + if(photocell<0.010) + i=0; + } //end if + + + mut.unlock(); + + + //Thread::yield(); + + }//end while + + + + } + + + + + + + + + + void LCD() { while(1) { - if (j==1) + mut.lock(); + if(i==0){ + + uLCD.locate(7,9); + uLCD.printf("Locked "); + } + else + { + uLCD.locate(7,9); + uLCD.printf("Unlocked"); + + } + mut.unlock(); + + + + + if (j==0) { mut.lock(); - uLCD.cls(); + + uLCD.line(0, 0, 127, 0, RED); + uLCD.line(127, 0, 127, 127, RED); + uLCD.line(127, 127, 0, 127, RED); + uLCD.line(0, 127, 0, 0, RED); uLCD.color(RED); - uLCD.locate(2,1); - uLCD.text_width(3); - uLCD.text_height(3); - uLCD.printf("UNLOCKED"); + uLCD.locate(7,8); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.printf("CLOSED "); mut.unlock(); - } - if(j==0) + }//end if + + if(j==1) { mut.lock(); - uLCD.cls(); + + uLCD.line(0, 0, 127, 0, GREEN); + uLCD.line(127, 0, 127, 127, GREEN); + uLCD.line(127, 127, 0, 127, GREEN); + uLCD.line(0, 127, 0, 0, GREEN); uLCD.color(GREEN); - uLCD.locate(2,1); - uLCD.text_width(3); - uLCD.text_height(3); - uLCD.printf("LOCKED"); - mut.unlock(); + uLCD.locate(7,8); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.printf("OPEN "); + + + mut.unlock(); } - - } - - - -} + Thread::wait(2); + + }//end while +}//end LCD void bluetooth(){ char bnum=0; - while(1){ - - /* - if (blue.getc()=='!') { - if (blue.getc()=='B') - { - bnum = blue.getc(); - // if ((bnum>='1')&&(bnum<='4'))//is a number button 1..4 - // myled[bnum-'1']=blue.getc()-'0'; //turn on/off that num LED - - - switch (bnum){ - case '1': - i=1.0; - j=1; - break; - case '2': - i=0.0; - j=0; - break; - } - - - } + + + + while(1) + { + blue.printf("enter password\n"); - } - - */ - - //mut.lock(); - blue.printf("enter password\n"); - - if (blue.getc()=='!') + if (blue.getc()=='!') { if (blue.getc()=='!') { @@ -108,7 +173,7 @@ { i=!i; blue.printf("password is correct\n"); - + } @@ -120,22 +185,152 @@ }//end while loop } + + void Pwspeaker() + { + + while(1) + { + + while(i==0&&j==1) + { + speaker.PlayNote(969.0, 0.5, 1.0); + speaker.PlayNote(800.0, 0.5, 1.0); + + } + + + + } + + } int main() { uLCD.baudrate(3000000); + pb1.mode(PullUp); + pb2.mode(PullUp); + pb3.mode(PullUp); mu.startUpdates();//start measuring the distance - thread.start(bluetooth); + +thread3.start(Pwspeaker); + thread.start(bluetooth); thread2.start(LCD); + thread4.start(LEDT); + while(1){ - //myled = mypotentiometer; - // myservo = mypotentiometer; + + + LEDG =!i; + LEDB = 0; + LEDR= i; + + if(pb3==0){ + + mut.lock(); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("10"); + + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("9"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("8"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("7"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("6"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("5"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("4"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("3"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("2"); + wait(1); + uLCD.cls(); + uLCD.color(RED); + uLCD.locate(3,3); + uLCD.text_width(3); + uLCD.text_height(3); + uLCD.printf("1"); + wait(1); + uLCD.cls(); + + i=0; + mut.unlock(); - // wait(1.0); + } + + + + + + + if (pb1==0){//Locked + + i = 0; + + } + + if (pb2==0){//Unlocked + i = 1; + + } + if (j==1){//close + LEDR =0; + LEDG = 0; + LEDB =1; + } + + + - // wait(1.0); - // printf("%c \n",mypotentiometer); myservo=i; - // led=j; + mut.lock(); mu.checkDistance(); mut.unlock(); @@ -144,63 +339,15 @@ { led=1; j=1; - // myservo =1.0; + } else { led=0; j=0; - //myservo = 0.0; + } - - /* - - - if (j==1) - { - mut.lock(); - //uLCD.cls(); - - - uLCD.color(BLACK); - //uLCD.locate(2,1); - uLCD.text_width(2); - uLCD.text_height(2); - uLCD.printf("CLOSED"); - - uLCD.color(RED); - //uLCD.locate(2,1); - uLCD.text_width(2); - uLCD.text_height(2); - uLCD.printf("OPEN"); - mut.unlock(); - } - if(j==0) - { - - mut.lock(); - //uLCD.cls(); - - uLCD.color(BLACK); - //uLCD.locate(2,1); - uLCD.text_width(2); - uLCD.text_height(2); - uLCD.printf("OPEN"); - - uLCD.color(GREEN); - // uLCD.locate(2,1); - uLCD.text_width(2); - uLCD.text_height(2); - uLCD.printf("CLOSED"); - mut.unlock(); - } - - - */ - - - - + }//end while -}//end main +}//end main \ No newline at end of file
diff -r 0e43a69efacd -r 21d56f84fe51 ultrasonic.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ultrasonic.cpp Tue May 01 04:54:34 2018 +0000 @@ -0,0 +1,86 @@ + #include "ultrasonic.h" + + ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout):_trig(trigPin), _echo(echoPin) + { + _updateSpeed = updateSpeed; + _timeout = timeout; + } + + ultrasonic::ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)) + :_trig(trigPin), _echo(echoPin) + { + _onUpdateMethod=onUpdate; + _updateSpeed = updateSpeed; + _timeout = timeout; + _t.start (); + } + void ultrasonic::_startT() + { + if(_t.read()>600) + { + _t.reset (); + } + start = _t.read_us (); + } + + void ultrasonic::_updateDist() + { + end = _t.read_us (); + done = 1; + _distance = (end - start)/6; + _tout.detach(); + _tout.attach(this,&ultrasonic::_startTrig, _updateSpeed); + } + void ultrasonic::_startTrig(void) + { + _tout.detach(); + _trig=1; + wait_us(10); + done = 0; + _echo.rise(this,&ultrasonic::_startT); + _echo.fall(this,&ultrasonic::_updateDist); + _echo.enable_irq (); + _tout.attach(this,&ultrasonic::_startTrig,_timeout); + _trig=0; + } + + int ultrasonic::getCurrentDistance(void) + { + return _distance; + } + void ultrasonic::pauseUpdates(void) + { + _tout.detach(); + _echo.rise(NULL); + _echo.fall(NULL); + } + void ultrasonic::startUpdates(void) + { + _startTrig(); + } + void ultrasonic::attachOnUpdate(void method(int)) + { + _onUpdateMethod = method; + } + void ultrasonic::changeUpdateSpeed(float updateSpeed) + { + _updateSpeed = updateSpeed; + } + float ultrasonic::getUpdateSpeed() + { + return _updateSpeed; + } + int ultrasonic::isUpdated(void) + { + //printf("%d", done); + d=done; + done = 0; + return d; + } + void ultrasonic::checkDistance(void) + { + if(isUpdated()) + { + (*_onUpdateMethod)(_distance); + } + }
diff -r 0e43a69efacd -r 21d56f84fe51 ultrasonic.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ultrasonic.h Tue May 01 04:54:34 2018 +0000 @@ -0,0 +1,47 @@ +#ifndef MBED_ULTRASONIC_H +#define MBED_ULTRASONIC_H + +#include "mbed.h" + +class ultrasonic +{ + public: + /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/ + ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout); + /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/ + ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)); + /** returns the last measured distance**/ + int getCurrentDistance(void); + /**pauses measuring the distance**/ + void pauseUpdates(void); + /**starts mesuring the distance**/ + void startUpdates(void); + /**attachs the method to be called when the distances changes**/ + void attachOnUpdate(void method(int)); + /**changes the speed at which updates are made**/ + void changeUpdateSpeed(float updateSpeed); + /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ + int isUpdated(void); + /**gets the speed at which updates are made**/ + float getUpdateSpeed(void); + /**call this as often as possible in your code, eg. at the end of a while(1) loop, + and it will check whether the method you have attached needs to be called**/ + void checkDistance(void); + private: + DigitalOut _trig; + InterruptIn _echo; + Timer _t; + Timeout _tout; + int _distance; + float _updateSpeed; + int start; + int end; + volatile int done; + void (*_onUpdateMethod)(int); + void _startT(void); + void _updateDist(void); + void _startTrig(void); + float _timeout; + int d; +}; +#endif \ No newline at end of file