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: mbed wave_player Servo mbed-rtos Motor SDFileSystem
Revision 12:3f127a8c0ed6, committed 2020-04-24
- Comitter:
- tejpatel
- Date:
- Fri Apr 24 14:15:23 2020 +0000
- Parent:
- 11:0309bef74ba8
- Commit message:
- Code for implementing a Bluetooth controlled Ping Pong Ball tank launcher.
Changed in this revision
diff -r 0309bef74ba8 -r 3f127a8c0ed6 Motor.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.lib Fri Apr 24 14:15:23 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/simon/code/Motor/#f265e441bcd9
diff -r 0309bef74ba8 -r 3f127a8c0ed6 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Fri Apr 24 14:15:23 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/neilt6/code/SDFileSystem/#e4d2567200db
diff -r 0309bef74ba8 -r 3f127a8c0ed6 Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Fri Apr 24 14:15:23 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/simon/code/Servo/#36b69a7ced07
diff -r 0309bef74ba8 -r 3f127a8c0ed6 SongPlayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Fri Apr 24 14:15:23 2020 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class SongPlayer +{ +public: + SongPlayer(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the constructor + } +// class method to play a note based on PwmOut class + void PlaySong(float frequency[], float duration[], float volume=1.0) { + vol = volume; + notecount = 0; + _pin.period(1.0/frequency[notecount]); + _pin = volume/2.0; + noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); + // setup timer to interrupt for next note to play + frequencyptr = frequency; + durationptr = duration; + //returns after first note starts to play + } + void nextnote(); +private: + Timeout noteduration; + PwmOut _pin; + int notecount; + float vol; + float * frequencyptr; + float * durationptr; +}; +//Interrupt Routine to play next note +void SongPlayer::nextnote() +{ + _pin = 0.0; + notecount++; //setup next note in song + if (durationptr[notecount]!=0.0) { + _pin.period(1.0/frequencyptr[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); + _pin = vol/2.0; + } else + _pin = 0.0; //turn off on last note +} \ No newline at end of file
diff -r 0309bef74ba8 -r 3f127a8c0ed6 main.cpp --- a/main.cpp Wed Feb 15 14:04:02 2017 -0600 +++ b/main.cpp Fri Apr 24 14:15:23 2020 +0000 @@ -1,22 +1,151 @@ #include "mbed.h" #include "rtos.h" - -DigitalOut led1(LED1); -DigitalOut led2(LED2); -Thread thread; - -void led2_thread() { - while (true) { - led2 = !led2; - Thread::wait(1000); +#include "SDFileSystem.h" +#include "wave_player.h" +#include "Servo.h" +#include "Motor.h" + +Servo launcher(p21); // Digital Out +Servo leftMotor(p22); // pwm +Servo rightMotor(p23); // pwm +DigitalOut solenoid(p9); //Solenoid output control bit + +SDFileSystem sd(p11, p12, p13, p8, "sd"); // the pinout on the mbed Cool Components workshop board. +Serial bluetooth(p28,p27); // bluetooth control of the robot + +Thread thread2; // thread for playing sound +Thread thread3; // thread for bluetooth messages +Mutex solenoidCtrl; +AnalogOut DACout(p18); +wave_player mySpeaker(&DACout); + +int stopSound = 1; + +void acceptMessage() { + char buffer = 0; + char bnum, bhit; + while (1) { + // Function to take in bluetooth messages and set global variables. + if (bluetooth.readable()) { + buffer = bluetooth.getc(); + if (buffer == '!') { + buffer = bluetooth.getc(); + if(buffer == 'B') { + // Check button + bnum = bluetooth.getc(); //button number + bhit = bluetooth.getc(); //1=hit, 0=release + if (bluetooth.getc()==char(~('!' + 'B' + bnum + bhit))) { + //checksum OK? + switch(bnum) { + case '1': { + if (bhit=='1') { + //turn on launcher motor. + launcher = 0.58; + }else{ + //turn off launcher. + launcher = 0.5; + } + break; + } + case '2': { + if (bhit=='1') { + //enable sound. + stopSound = 0; + //let a ping pong ball through. + solenoidCtrl.lock(); + solenoid = 1; //retract the solenoid + wait(0.05); + solenoid = 0; //extend the solenoid + solenoidCtrl.unlock(); + } + break; + } + case '8': { + if (bhit=='1') { + //back + rightMotor = 0.0; + leftMotor = 0.0; + }else{ + rightMotor = 0.5; + leftMotor = 0.5; + } + break; + } + case '7': { + if (bhit=='1') { + //forward + rightMotor = 1.0; + leftMotor = 1.0; + }else{ + rightMotor = 0.5; + leftMotor = 0.5; + } + break; + } + case '6': { + if (bhit=='1') { + //turn right + rightMotor = 0.0; + leftMotor = 1.0; + }else{ + rightMotor = 0.5; + leftMotor = 0.5; + } + break; + } + case '5': { + if (bhit=='1') { + //turn left + rightMotor = 1.0; + leftMotor = 0.0; + }else{ + rightMotor = 0.5; + leftMotor = 0.5; + } + break; + } + } + } + } + } + }else{ + solenoidCtrl.lock(); + solenoid = 0; //extend the solenoid + solenoidCtrl.unlock(); + } } } + +void sound() { + while (1) + { + // Start song and return once playing starts + if (stopSound) { + Thread::wait(100); + continue; + } + FILE *wave_file; + wave_file=fopen("/sd/wavfiles/boom.wav","r"); + mySpeaker.play(wave_file); + fclose(wave_file); + stopSound = 1; + Thread::wait(500); + } + } int main() { - thread.start(led2_thread); - + //Initialize motors and solenoid + rightMotor = 0.5; + leftMotor = 0.5; + solenoid = 0; + //Initialize ESC and turn off launcher + launcher = 0.5; + wait(5); + launcher = 0.5; + Thread::wait(2); + thread2.start(sound); + thread3.start(acceptMessage); while (true) { - led1 = !led1; Thread::wait(500); } }
diff -r 0309bef74ba8 -r 3f127a8c0ed6 mbed.bld --- a/mbed.bld Wed Feb 15 14:04:02 2017 -0600 +++ b/mbed.bld Fri Apr 24 14:15:23 2020 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/0ab6a29f35bf \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/0ab6a29f35bf \ No newline at end of file
diff -r 0309bef74ba8 -r 3f127a8c0ed6 wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Fri Apr 24 14:15:23 2020 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/sravet/code/wave_player/#62c18ade9a60