The IoTBot is a WiFi-enabled rover built from the Shadow Robot kit. It is controlled from a web interface running on the Adafruit Huzzah ESP8266 WiFi module and implements a pair of Hall-effect sensors on the motors to keep the wheels spinning at the same speed. Additionally, it uses a Sharp IR sensor to detect walls in front of the robot and prevent it from crashing.
Dependencies: mbed-dev mbed-rtos
Fork of huzzah_helloWorld by
SongPlayer.h@2:2d87957b577b, 2016-11-02 (annotated)
- Committer:
- tanmaybangalore
- Date:
- Wed Nov 02 17:15:46 2016 +0000
- Revision:
- 2:2d87957b577b
Commit for code checkoff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tanmaybangalore | 2:2d87957b577b | 1 | #include "mbed.h" |
tanmaybangalore | 2:2d87957b577b | 2 | // new class to play a note on Speaker based on PwmOut class |
tanmaybangalore | 2:2d87957b577b | 3 | class SongPlayer |
tanmaybangalore | 2:2d87957b577b | 4 | { |
tanmaybangalore | 2:2d87957b577b | 5 | public: |
tanmaybangalore | 2:2d87957b577b | 6 | SongPlayer(PinName pin) : _pin(pin) { |
tanmaybangalore | 2:2d87957b577b | 7 | // _pin(pin) means pass pin to the constructor |
tanmaybangalore | 2:2d87957b577b | 8 | } |
tanmaybangalore | 2:2d87957b577b | 9 | // class method to play a note based on PwmOut class |
tanmaybangalore | 2:2d87957b577b | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
tanmaybangalore | 2:2d87957b577b | 11 | vol = volume; |
tanmaybangalore | 2:2d87957b577b | 12 | notecount = 0; |
tanmaybangalore | 2:2d87957b577b | 13 | _pin.period(1.0/frequency[notecount]); |
tanmaybangalore | 2:2d87957b577b | 14 | _pin = volume/2.0; |
tanmaybangalore | 2:2d87957b577b | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
tanmaybangalore | 2:2d87957b577b | 16 | // setup timer to interrupt for next note to play |
tanmaybangalore | 2:2d87957b577b | 17 | frequencyptr = frequency; |
tanmaybangalore | 2:2d87957b577b | 18 | durationptr = duration; |
tanmaybangalore | 2:2d87957b577b | 19 | //returns after first note starts to play |
tanmaybangalore | 2:2d87957b577b | 20 | } |
tanmaybangalore | 2:2d87957b577b | 21 | void nextnote(); |
tanmaybangalore | 2:2d87957b577b | 22 | private: |
tanmaybangalore | 2:2d87957b577b | 23 | Timeout noteduration; |
tanmaybangalore | 2:2d87957b577b | 24 | PwmOut _pin; |
tanmaybangalore | 2:2d87957b577b | 25 | int notecount; |
tanmaybangalore | 2:2d87957b577b | 26 | float vol; |
tanmaybangalore | 2:2d87957b577b | 27 | float * frequencyptr; |
tanmaybangalore | 2:2d87957b577b | 28 | float * durationptr; |
tanmaybangalore | 2:2d87957b577b | 29 | }; |
tanmaybangalore | 2:2d87957b577b | 30 | //Interrupt Routine to play next note |
tanmaybangalore | 2:2d87957b577b | 31 | void SongPlayer::nextnote() |
tanmaybangalore | 2:2d87957b577b | 32 | { |
tanmaybangalore | 2:2d87957b577b | 33 | _pin = 0.0; |
tanmaybangalore | 2:2d87957b577b | 34 | notecount++; //setup next note in song |
tanmaybangalore | 2:2d87957b577b | 35 | if (durationptr[notecount]!=0.0) { |
tanmaybangalore | 2:2d87957b577b | 36 | _pin.period(1.0/frequencyptr[notecount]); |
tanmaybangalore | 2:2d87957b577b | 37 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
tanmaybangalore | 2:2d87957b577b | 38 | _pin = vol/2.0; |
tanmaybangalore | 2:2d87957b577b | 39 | } else |
tanmaybangalore | 2:2d87957b577b | 40 | _pin = 0.0; //turn off on last note |
tanmaybangalore | 2:2d87957b577b | 41 | } |