A robot rover with distance sensing and audiovisual effects

Dependencies:   4DGL-uLCD-SE Motordriver PID mbed

Fork of PIDRover by Aaron Berk

Committer:
Szilard
Date:
Wed Mar 16 17:37:23 2016 +0000
Revision:
6:9dc165a89453
Parent:
4:48f440b9a787
published version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Szilard 4:48f440b9a787 1 #include "mbed.h"
Szilard 4:48f440b9a787 2 // new class to play a note on Speaker based on PwmOut class
Szilard 4:48f440b9a787 3 class SongPlayer
Szilard 4:48f440b9a787 4 {
Szilard 4:48f440b9a787 5 public:
Szilard 4:48f440b9a787 6 SongPlayer(PinName pin) : _pin(pin) {
Szilard 4:48f440b9a787 7 // _pin(pin) means pass pin to the constructor
Szilard 4:48f440b9a787 8 }
Szilard 4:48f440b9a787 9 // class method to play a note based on PwmOut class
Szilard 4:48f440b9a787 10 void PlaySong(float frequency[], float duration[], float volume=1.0) {
Szilard 4:48f440b9a787 11 vol = volume;
Szilard 4:48f440b9a787 12 notecount = 0;
Szilard 4:48f440b9a787 13 _pin.period(1.0/frequency[notecount]);
Szilard 4:48f440b9a787 14 _pin = volume/2.0;
Szilard 4:48f440b9a787 15 noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
Szilard 4:48f440b9a787 16 // setup timer to interrupt for next note to play
Szilard 4:48f440b9a787 17 frequencyptr = frequency;
Szilard 4:48f440b9a787 18 durationptr = duration;
Szilard 4:48f440b9a787 19 //returns after first note starts to play
Szilard 4:48f440b9a787 20 }
Szilard 4:48f440b9a787 21 void nextnote();
Szilard 4:48f440b9a787 22 private:
Szilard 4:48f440b9a787 23 Timeout noteduration;
Szilard 4:48f440b9a787 24 PwmOut _pin;
Szilard 4:48f440b9a787 25 int notecount;
Szilard 4:48f440b9a787 26 float vol;
Szilard 4:48f440b9a787 27 float * frequencyptr;
Szilard 4:48f440b9a787 28 float * durationptr;
Szilard 4:48f440b9a787 29 };
Szilard 4:48f440b9a787 30 //Interrupt Routine to play next note
Szilard 4:48f440b9a787 31 void SongPlayer::nextnote()
Szilard 4:48f440b9a787 32 {
Szilard 4:48f440b9a787 33 _pin = 0.0;
Szilard 4:48f440b9a787 34 notecount++; //setup next note in song
Szilard 4:48f440b9a787 35 if (durationptr[notecount]!=0.0) {
Szilard 4:48f440b9a787 36 _pin.period(1.0/frequencyptr[notecount]);
Szilard 4:48f440b9a787 37 noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
Szilard 4:48f440b9a787 38 _pin = vol/2.0;
Szilard 4:48f440b9a787 39 } else
Szilard 4:48f440b9a787 40 _pin = 0.0; //turn off on last note
Szilard 4:48f440b9a787 41 }