![](/media/cache/profiles/e1f5448f160187bcb4c4acaacfb3456c.50x50_q85.jpg)
The program of Musical Instrument in Microgravity ~ Combined All Experiment Video (85% Speed) ~
Project introduction slide) https://www.slideshare.net/secret/EFyMJ5f4U6oJbz
main.cpp@0:90918a0a0969, 2021-03-12 (annotated)
- Committer:
- AkiraK
- Date:
- Fri Mar 12 02:49:56 2021 +0000
- Revision:
- 0:90918a0a0969
This program make a sound with the change of magnetic field.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkiraK | 0:90918a0a0969 | 1 | #define mC 2610.626 |
AkiraK | 0:90918a0a0969 | 2 | #define mD 2930.665 |
AkiraK | 0:90918a0a0969 | 3 | #define mE 3290.628 |
AkiraK | 0:90918a0a0969 | 4 | #define mF 3490.228 |
AkiraK | 0:90918a0a0969 | 5 | #define mG 3910.995 |
AkiraK | 0:90918a0a0969 | 6 | #define mA 440.000 |
AkiraK | 0:90918a0a0969 | 7 | #define mB 493.883 |
AkiraK | 0:90918a0a0969 | 8 | // viollin: 391.995~4186.009 |
AkiraK | 0:90918a0a0969 | 9 | // viola: 261.626~2093.005 |
AkiraK | 0:90918a0a0969 | 10 | // cello: 130.813~1046.502 |
AkiraK | 0:90918a0a0969 | 11 | // contrabass: 82.407~391.995 |
AkiraK | 0:90918a0a0969 | 12 | // hape: 61.735~6271.927 |
AkiraK | 0:90918a0a0969 | 13 | |
AkiraK | 0:90918a0a0969 | 14 | #include "mbed.h" |
AkiraK | 0:90918a0a0969 | 15 | #include "math.h" |
AkiraK | 0:90918a0a0969 | 16 | |
AkiraK | 0:90918a0a0969 | 17 | Ticker timer; |
AkiraK | 0:90918a0a0969 | 18 | AnalogOut sp1(p18); |
AkiraK | 0:90918a0a0969 | 19 | AnalogIn magSensors[] = {p15,p16,p17,p19,p20}; |
AkiraK | 0:90918a0a0969 | 20 | float ms[180]; |
AkiraK | 0:90918a0a0969 | 21 | float m1,m2,m3,m4,m5; |
AkiraK | 0:90918a0a0969 | 22 | float freq[3] = {0,0,0}; |
AkiraK | 0:90918a0a0969 | 23 | |
AkiraK | 0:90918a0a0969 | 24 | void sound_out(void) { |
AkiraK | 0:90918a0a0969 | 25 | // |
AkiraK | 0:90918a0a0969 | 26 | static float j1=0; |
AkiraK | 0:90918a0a0969 | 27 | static float j2=0; |
AkiraK | 0:90918a0a0969 | 28 | static float j3=0; |
AkiraK | 0:90918a0a0969 | 29 | static float j4=0; |
AkiraK | 0:90918a0a0969 | 30 | static float j5=0; |
AkiraK | 0:90918a0a0969 | 31 | j1=j1+m1; |
AkiraK | 0:90918a0a0969 | 32 | j2=j2+m2; |
AkiraK | 0:90918a0a0969 | 33 | j3=j3+m3; |
AkiraK | 0:90918a0a0969 | 34 | j4=j4+m4; |
AkiraK | 0:90918a0a0969 | 35 | j5=j5+m5; |
AkiraK | 0:90918a0a0969 | 36 | if (j1>180)j1=j1-180; |
AkiraK | 0:90918a0a0969 | 37 | if (j2>180)j2=j2-180; |
AkiraK | 0:90918a0a0969 | 38 | if (j3>180)j3=j3-180; |
AkiraK | 0:90918a0a0969 | 39 | if (j4>180)j4=j4-180; |
AkiraK | 0:90918a0a0969 | 40 | if (j5>180)j5=j5-180; |
AkiraK | 0:90918a0a0969 | 41 | sp1.write((ms[(int)j1]+ms[(int)j2]+ms[(int)j3]+ms[(int)j4]+ms[(int)j5])/5.0); |
AkiraK | 0:90918a0a0969 | 42 | } |
AkiraK | 0:90918a0a0969 | 43 | |
AkiraK | 0:90918a0a0969 | 44 | int main() { |
AkiraK | 0:90918a0a0969 | 45 | int i; |
AkiraK | 0:90918a0a0969 | 46 | //setting sincurv |
AkiraK | 0:90918a0a0969 | 47 | for (i=0;i<180;i++) { |
AkiraK | 0:90918a0a0969 | 48 | ms[i]=sin(2*3.1415*(float)i/180.0)/2.0+0.5; |
AkiraK | 0:90918a0a0969 | 49 | } |
AkiraK | 0:90918a0a0969 | 50 | timer.attach_us(&sound_out,100); //10kHz |
AkiraK | 0:90918a0a0969 | 51 | while(true){ |
AkiraK | 0:90918a0a0969 | 52 | // printf("magSensors: %f, %f, %f\n", magSensors[0]*3.3 * 1054 + 20, magSensors[1]*3.3, magSensors[2]*3.3); |
AkiraK | 0:90918a0a0969 | 53 | |
AkiraK | 0:90918a0a0969 | 54 | // m1 = (magSensors[0]*3.3 * 1149.7 + 391.995) *2*180/10000; //606 violin |
AkiraK | 0:90918a0a0969 | 55 | // m2 = (magSensors[1]*3.3 * 554.96 + 261.626) *2*180/10000; //606 2000 viola |
AkiraK | 0:90918a0a0969 | 56 | // m3 = (magSensors[2]*3.3 * 227.48 + 130.813) *2*180/10000; // cello |
AkiraK | 0:90918a0a0969 | 57 | // m4 = (magSensors[3]*3.3 * 93.815 + 82.407) *2*180/10000; //606 2000 |
AkiraK | 0:90918a0a0969 | 58 | // m5 = (magSensors[4]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 59 | // |
AkiraK | 0:90918a0a0969 | 60 | m1 = (magSensors[0]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 61 | m2 = (magSensors[1]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 62 | m3 = (magSensors[2]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 63 | m4 = (magSensors[3]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 64 | m5 = (magSensors[4]*3.3 * 1881.876 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 65 | |
AkiraK | 0:90918a0a0969 | 66 | // m1 = (sqrt(magSensors[0]*3.3) * 3418.598 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 67 | // m2 = (sqrt(magSensors[1]*3.3) * 3418.598 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 68 | // m3 = (sqrt(magSensors[2]*3.3) * 3418.598 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 69 | // m4 = (sqrt(magSensors[3]*3.3) * 3418.598 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 70 | // m5 = (sqrt(magSensors[4]*3.3) * 3418.598 + 61.735) *2*180/10000; // |
AkiraK | 0:90918a0a0969 | 71 | // printf("%f,%f,%f,%f,%f\n",m1,m2,m3,m4,m5); |
AkiraK | 0:90918a0a0969 | 72 | |
AkiraK | 0:90918a0a0969 | 73 | // Error avoid |
AkiraK | 0:90918a0a0969 | 74 | |
AkiraK | 0:90918a0a0969 | 75 | //for(int i = 0; i < 3; i++) { |
AkiraK | 0:90918a0a0969 | 76 | // m[i] = magSensors[i]*3.3*6054+20; |
AkiraK | 0:90918a0a0969 | 77 | // } |
AkiraK | 0:90918a0a0969 | 78 | // |
AkiraK | 0:90918a0a0969 | 79 | wait(0.00001); |
AkiraK | 0:90918a0a0969 | 80 | } |
AkiraK | 0:90918a0a0969 | 81 | ////ceg |
AkiraK | 0:90918a0a0969 | 82 | // m1=mC*2*180/10000; |
AkiraK | 0:90918a0a0969 | 83 | // m2=mE*2*180/10000; |
AkiraK | 0:90918a0a0969 | 84 | // m3=mG*2*180/10000; |
AkiraK | 0:90918a0a0969 | 85 | // wait(1.0f); |
AkiraK | 0:90918a0a0969 | 86 | // //cfa |
AkiraK | 0:90918a0a0969 | 87 | // m1=mC*2*180/10000; |
AkiraK | 0:90918a0a0969 | 88 | // m2=mF*2*180/10000; |
AkiraK | 0:90918a0a0969 | 89 | // m3=mA*2*180/10000; |
AkiraK | 0:90918a0a0969 | 90 | // wait(1.0f); |
AkiraK | 0:90918a0a0969 | 91 | // //ceg |
AkiraK | 0:90918a0a0969 | 92 | // m1=mC*2*180/10000; |
AkiraK | 0:90918a0a0969 | 93 | // m2=mE*2*180/10000; |
AkiraK | 0:90918a0a0969 | 94 | // m3=mG*2*180/10000; |
AkiraK | 0:90918a0a0969 | 95 | // wait(1.0f); |
AkiraK | 0:90918a0a0969 | 96 | // //bdg |
AkiraK | 0:90918a0a0969 | 97 | // m1=mB*180/10000; |
AkiraK | 0:90918a0a0969 | 98 | // m2=mD*2*180/10000; |
AkiraK | 0:90918a0a0969 | 99 | // m3=mG*2*180/10000; |
AkiraK | 0:90918a0a0969 | 100 | // wait(1.0f); |
AkiraK | 0:90918a0a0969 | 101 | // //ceg |
AkiraK | 0:90918a0a0969 | 102 | // m1=mC*2*180/10000; |
AkiraK | 0:90918a0a0969 | 103 | // m2=mE*2*180/10000; |
AkiraK | 0:90918a0a0969 | 104 | // m3=mG*2*180/10000; |
AkiraK | 0:90918a0a0969 | 105 | // wait(1.0f); |
AkiraK | 0:90918a0a0969 | 106 | // timer.detach(); |
AkiraK | 0:90918a0a0969 | 107 | // sp1.write(0.0f); |
AkiraK | 0:90918a0a0969 | 108 | // while (1); |
AkiraK | 0:90918a0a0969 | 109 | } |
AkiraK | 0:90918a0a0969 | 110 | |
AkiraK | 0:90918a0a0969 | 111 | //Ticker timer; |
AkiraK | 0:90918a0a0969 | 112 | //AnalogOut sp1(p18); //define analog out pin |
AkiraK | 0:90918a0a0969 | 113 | // |
AkiraK | 0:90918a0a0969 | 114 | //float ms[180]; |
AkiraK | 0:90918a0a0969 | 115 | //float m1; |
AkiraK | 0:90918a0a0969 | 116 | //int counter = 0; |
AkiraK | 0:90918a0a0969 | 117 | // |
AkiraK | 0:90918a0a0969 | 118 | //void sound_out(void) { |
AkiraK | 0:90918a0a0969 | 119 | // static float j = 0; |
AkiraK | 0:90918a0a0969 | 120 | // j = j + m1; |
AkiraK | 0:90918a0a0969 | 121 | // if(j > 180) j = j - 180; |
AkiraK | 0:90918a0a0969 | 122 | // sp1.write(ms[(int)j]); |
AkiraK | 0:90918a0a0969 | 123 | // counter++; |
AkiraK | 0:90918a0a0969 | 124 | // if(counter > 500000) { |
AkiraK | 0:90918a0a0969 | 125 | // timer.detach(); |
AkiraK | 0:90918a0a0969 | 126 | // } |
AkiraK | 0:90918a0a0969 | 127 | //} |
AkiraK | 0:90918a0a0969 | 128 | // |
AkiraK | 0:90918a0a0969 | 129 | //int main() { |
AkiraK | 0:90918a0a0969 | 130 | // float mm[] = {mC,mD,mE,mF,mG,mA,mB}; //sound scale |
AkiraK | 0:90918a0a0969 | 131 | // //setting sinecurv |
AkiraK | 0:90918a0a0969 | 132 | // for(int i = 0; i < 180; i++) { |
AkiraK | 0:90918a0a0969 | 133 | // ms[i] = sin(2*3.1415*(float)i/180.0)/2.0+0.5; |
AkiraK | 0:90918a0a0969 | 134 | // } |
AkiraK | 0:90918a0a0969 | 135 | // |
AkiraK | 0:90918a0a0969 | 136 | // for(int i = 0; i < sizeof(mm); i++) { |
AkiraK | 0:90918a0a0969 | 137 | // m1 = mm[i]*180/100000; |
AkiraK | 0:90918a0a0969 | 138 | // timer.attach_us(&sound_out,10); |
AkiraK | 0:90918a0a0969 | 139 | // counter = 0; |
AkiraK | 0:90918a0a0969 | 140 | // wait(0.5f); |
AkiraK | 0:90918a0a0969 | 141 | // } |
AkiraK | 0:90918a0a0969 | 142 | // |
AkiraK | 0:90918a0a0969 | 143 | // sp1.write(0.0f); |
AkiraK | 0:90918a0a0969 | 144 | // while(true); |
AkiraK | 0:90918a0a0969 | 145 | //} |
AkiraK | 0:90918a0a0969 | 146 | // |
AkiraK | 0:90918a0a0969 | 147 | // |
AkiraK | 0:90918a0a0969 | 148 | //DigitalOut sp1(p5); |
AkiraK | 0:90918a0a0969 | 149 | //Ticker timer; //Initialize timer interrupt |
AkiraK | 0:90918a0a0969 | 150 | // |
AkiraK | 0:90918a0a0969 | 151 | //int oto = 0; |
AkiraK | 0:90918a0a0969 | 152 | // |
AkiraK | 0:90918a0a0969 | 153 | //void tick(void) |
AkiraK | 0:90918a0a0969 | 154 | //{ |
AkiraK | 0:90918a0a0969 | 155 | // sp1.write(oto); //Digital Out |
AkiraK | 0:90918a0a0969 | 156 | // oto=!oto; //switch flag of digital out |
AkiraK | 0:90918a0a0969 | 157 | //} |
AkiraK | 0:90918a0a0969 | 158 | // |
AkiraK | 0:90918a0a0969 | 159 | //int main() |
AkiraK | 0:90918a0a0969 | 160 | //{ |
AkiraK | 0:90918a0a0969 | 161 | // float mm [] = {mC,mD,mE,mF,mG,mA,mB}; //sound scale |
AkiraK | 0:90918a0a0969 | 162 | // for(int i = 0; i < sizeof(mm); i++) { |
AkiraK | 0:90918a0a0969 | 163 | // timer.attach(&tick,1.0/mm[i]/2.0); |
AkiraK | 0:90918a0a0969 | 164 | // wait(0.5f); |
AkiraK | 0:90918a0a0969 | 165 | // } |
AkiraK | 0:90918a0a0969 | 166 | // timer.detach(); |
AkiraK | 0:90918a0a0969 | 167 | // while(true); |
AkiraK | 0:90918a0a0969 | 168 | //} |