Conner Awald / Mbed 2 deprecated StepperMotorSongMidi

Dependencies:   mbed mbed-rtos

Committer:
cawald18
Date:
Mon Apr 26 00:17:01 2021 +0000
Revision:
2:7c59ebc9dc23
Parent:
1:58fce6cd1c51
Base2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cawald18 0:6cc2c1f459f1 1 #include "mbed.h"
cawald18 0:6cc2c1f459f1 2 #include "rtos.h"
cawald18 0:6cc2c1f459f1 3 #include "notes.h"
cawald18 2:7c59ebc9dc23 4 #include "math.h"
cawald18 2:7c59ebc9dc23 5 #include "wii.h"
cawald18 0:6cc2c1f459f1 6
cawald18 0:6cc2c1f459f1 7 Serial pc(USBTX, USBRX); // tx, rx
cawald18 0:6cc2c1f459f1 8 class Flipper {
cawald18 0:6cc2c1f459f1 9 public:
cawald18 0:6cc2c1f459f1 10 Flipper(PinName pin) : _pin(pin) {
cawald18 0:6cc2c1f459f1 11 _pin = 0;
cawald18 0:6cc2c1f459f1 12 }
cawald18 0:6cc2c1f459f1 13 void flip() {
cawald18 0:6cc2c1f459f1 14 _pin = !_pin;
cawald18 0:6cc2c1f459f1 15 }
cawald18 0:6cc2c1f459f1 16 private:
cawald18 0:6cc2c1f459f1 17 DigitalOut _pin;
cawald18 0:6cc2c1f459f1 18 };
cawald18 0:6cc2c1f459f1 19
cawald18 0:6cc2c1f459f1 20 DigitalOut led1(LED1);
cawald18 2:7c59ebc9dc23 21 Flipper f1(p21);
cawald18 2:7c59ebc9dc23 22 Flipper f2(p22);
cawald18 2:7c59ebc9dc23 23 Flipper f3(p23);
cawald18 0:6cc2c1f459f1 24 Ticker t1;
cawald18 2:7c59ebc9dc23 25 Ticker t2;
cawald18 2:7c59ebc9dc23 26 Ticker t3;
cawald18 2:7c59ebc9dc23 27
cawald18 2:7c59ebc9dc23 28 Mutex motor1lock;
cawald18 2:7c59ebc9dc23 29 Mutex motor2lock;
cawald18 2:7c59ebc9dc23 30 Mutex motor3lock;
cawald18 2:7c59ebc9dc23 31 Timer motor1timer;
cawald18 2:7c59ebc9dc23 32
cawald18 2:7c59ebc9dc23 33 /*
cawald18 2:7c59ebc9dc23 34 Takes the midi number in from the array and then converts it to a frequency in hz;
cawald18 2:7c59ebc9dc23 35 */
cawald18 2:7c59ebc9dc23 36 float midi2freq(float midi){
cawald18 2:7c59ebc9dc23 37 return 440.0*powf(2.0, (midi-69)/12);
cawald18 2:7c59ebc9dc23 38 }
cawald18 2:7c59ebc9dc23 39
cawald18 2:7c59ebc9dc23 40 /*
cawald18 2:7c59ebc9dc23 41 Stops playback for motor1 and unlocks it to allow a new note to be set
cawald18 2:7c59ebc9dc23 42 */
cawald18 2:7c59ebc9dc23 43 void stopMotor1(){
cawald18 2:7c59ebc9dc23 44 motor1lock.unlock();
cawald18 2:7c59ebc9dc23 45 }
cawald18 0:6cc2c1f459f1 46 int main() {
aurora_aeterna 1:58fce6cd1c51 47 //parse length here
cawald18 2:7c59ebc9dc23 48 int length = (int)( sizeof(noteswii) / sizeof(noteswii[0]));
aurora_aeterna 1:58fce6cd1c51 49 // copy above for more lines of notes
aurora_aeterna 1:58fce6cd1c51 50
aurora_aeterna 1:58fce6cd1c51 51 //parsing of notes, needs to partse
aurora_aeterna 1:58fce6cd1c51 52
aurora_aeterna 1:58fce6cd1c51 53 //parsing of durs
aurora_aeterna 1:58fce6cd1c51 54
cawald18 2:7c59ebc9dc23 55
cawald18 2:7c59ebc9dc23 56
cawald18 0:6cc2c1f459f1 57 pc.printf("Hello World!\n");
cawald18 2:7c59ebc9dc23 58 motor1timer.start();
cawald18 0:6cc2c1f459f1 59 // spin in a main loop. flipper will interrupt it to call flip
cawald18 0:6cc2c1f459f1 60 while(1) {
cawald18 2:7c59ebc9dc23 61 for(int i = 0; i < length;) {
cawald18 2:7c59ebc9dc23 62 //This sees if we ever have an unlocked motor lock,
cawald18 2:7c59ebc9dc23 63 //in which case the motor will be done playing and
cawald18 2:7c59ebc9dc23 64 //we will need to set it to silent
cawald18 2:7c59ebc9dc23 65 if(motor1lock.trylock()){
cawald18 2:7c59ebc9dc23 66 motor1lock.unlock(); //Make the motor available for note commands
cawald18 2:7c59ebc9dc23 67 t1.detach(); //Stop the ticker on the motor
cawald18 2:7c59ebc9dc23 68 }
cawald18 2:7c59ebc9dc23 69 if(motor2lock.trylock()){
cawald18 2:7c59ebc9dc23 70 motor2lock.unlock();
cawald18 2:7c59ebc9dc23 71 t2.detach();
cawald18 2:7c59ebc9dc23 72 }
cawald18 2:7c59ebc9dc23 73 if(motor3lock.trylock()){
cawald18 2:7c59ebc9dc23 74 motor3lock.unlock();
cawald18 2:7c59ebc9dc23 75 t3.detach();
cawald18 2:7c59ebc9dc23 76 }
cawald18 2:7c59ebc9dc23 77
cawald18 2:7c59ebc9dc23 78 /*
cawald18 2:7c59ebc9dc23 79 IF THIS IS TRUE it means we have a note that just came into
cawald18 2:7c59ebc9dc23 80 being played and we should move through the indexed list
cawald18 2:7c59ebc9dc23 81 at the very end
cawald18 2:7c59ebc9dc23 82 */
cawald18 2:7c59ebc9dc23 83 if(notesstart[i]<motor1timer.read()){
cawald18 2:7c59ebc9dc23 84 if(motor1lock.trylock_for((notesstop[i]-notesstart[i]*1000)) { //This means we were succesfully able to lock the motor
cawald18 2:7c59ebc9dc23 85 t1.attach(&f1, &Flipper::flip, 1.0/midi2freq(noteswii[i]));
cawald18 2:7c59ebc9dc23 86 }
cawald18 2:7c59ebc9dc23 87
cawald18 2:7c59ebc9dc23 88
cawald18 2:7c59ebc9dc23 89
cawald18 2:7c59ebc9dc23 90 i++;//THIS NEEDS TO BE THE LAST THING
cawald18 2:7c59ebc9dc23 91 }
cawald18 2:7c59ebc9dc23 92 else {
cawald18 2:7c59ebc9dc23 93 t1.attach(&f1, &Flipper::flip, 1.0/notes1[i]); }
cawald18 2:7c59ebc9dc23 94 pc.printf("%f\n",notes1[i]);
cawald18 2:7c59ebc9dc23 95 pc.printf("%f\n",durs1[i]);
cawald18 2:7c59ebc9dc23 96 wait(durs1[i]);
cawald18 2:7c59ebc9dc23 97 pc.printf("The timer is at %f\n\n", motor1timer.read());
cawald18 0:6cc2c1f459f1 98 }
cawald18 0:6cc2c1f459f1 99
cawald18 0:6cc2c1f459f1 100 }
cawald18 0:6cc2c1f459f1 101 }