Sound : LED = 1:1 param "1" never go upper 15, under 0

Dependencies:   C12832 mbed-rtos mbed

Fork of app-board-Speaker by Ryohei Funakoshi

Committer:
max_0630
Date:
Sat Dec 10 11:23:42 2016 +0000
Revision:
4:91da6632d547
Parent:
3:b1115b34e95b
Sound & LED light -> 1:1 ; parameter "i" never go upper 15, under 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
funa0825 3:b1115b34e95b 1
funa0825 3:b1115b34e95b 2
funa0825 3:b1115b34e95b 3 #define mC 261.626
funa0825 3:b1115b34e95b 4 #define mD 293.665
funa0825 3:b1115b34e95b 5 #define mE 329.628
funa0825 3:b1115b34e95b 6 #define mF 349.228
funa0825 3:b1115b34e95b 7 #define mG 391.995
funa0825 3:b1115b34e95b 8 #define mA 440.000
funa0825 3:b1115b34e95b 9 #define mB 493.883
funa0825 3:b1115b34e95b 10 #define THRESH 80
max_0630 4:91da6632d547 11 #define COUNT_MAX 15
funa0825 3:b1115b34e95b 12 #include "C12832.h"
chris 0:f86c572491c3 13 #include "mbed.h"
funa0825 3:b1115b34e95b 14 #include "rtos.h"
chris 0:f86c572491c3 15
funa0825 3:b1115b34e95b 16 C12832 lcd(p5, p7, p6, p8, p11);
chris 1:0a08d26b011a 17 DigitalIn fire(p14);
funa0825 3:b1115b34e95b 18 //DigitalIn up(p15);
funa0825 3:b1115b34e95b 19 //DigitalIn down(p12);
funa0825 3:b1115b34e95b 20 PwmOut sp1(p26);
funa0825 3:b1115b34e95b 21 InterruptIn myup(p15);
funa0825 3:b1115b34e95b 22 InterruptIn mydown(p12);
funa0825 3:b1115b34e95b 23 PwmOut led1(p23);
funa0825 3:b1115b34e95b 24 PwmOut led2(p24);
funa0825 3:b1115b34e95b 25 PwmOut led3(p25);
funa0825 3:b1115b34e95b 26 Semaphore three_slots(1);
funa0825 3:b1115b34e95b 27 Timer for_chat;
chris 0:f86c572491c3 28
funa0825 3:b1115b34e95b 29 int i=0;
max_0630 4:91da6632d547 30 int count = 0;
max_0630 4:91da6632d547 31
funa0825 3:b1115b34e95b 32 float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4};
funa0825 3:b1115b34e95b 33 void intRise(void)
funa0825 3:b1115b34e95b 34 {
funa0825 3:b1115b34e95b 35 if(for_chat.read_ms()>THRESH);
funa0825 3:b1115b34e95b 36 {
max_0630 4:91da6632d547 37 if(i<=13){
funa0825 3:b1115b34e95b 38 i++;
max_0630 4:91da6632d547 39 count ++;
max_0630 4:91da6632d547 40 }
funa0825 3:b1115b34e95b 41 sp1.period(1.0/mm[i]);
funa0825 3:b1115b34e95b 42 sp1.write(0.5f);
funa0825 3:b1115b34e95b 43 for_chat.reset();
funa0825 3:b1115b34e95b 44 }
funa0825 3:b1115b34e95b 45 }
funa0825 3:b1115b34e95b 46
funa0825 3:b1115b34e95b 47 void intFall(void)
funa0825 3:b1115b34e95b 48 {
funa0825 3:b1115b34e95b 49 if(for_chat.read_ms()>THRESH);
funa0825 3:b1115b34e95b 50 {
max_0630 4:91da6632d547 51 if(i >=1){
funa0825 3:b1115b34e95b 52 i--;
max_0630 4:91da6632d547 53 count --;
max_0630 4:91da6632d547 54 }
funa0825 3:b1115b34e95b 55 sp1.period(1.0/mm[i]);
funa0825 3:b1115b34e95b 56 sp1.write(0.5f);
funa0825 3:b1115b34e95b 57 for_chat.reset();
funa0825 3:b1115b34e95b 58 }
funa0825 3:b1115b34e95b 59 }
funa0825 3:b1115b34e95b 60
funa0825 3:b1115b34e95b 61
funa0825 3:b1115b34e95b 62 void r_thread(void const *argument)
funa0825 3:b1115b34e95b 63 {
funa0825 3:b1115b34e95b 64 float r = 0;
funa0825 3:b1115b34e95b 65 while (true)
funa0825 3:b1115b34e95b 66 {
funa0825 3:b1115b34e95b 67 r=0;
funa0825 3:b1115b34e95b 68 while(r<=1)
funa0825 3:b1115b34e95b 69 {
funa0825 3:b1115b34e95b 70 three_slots.wait();
max_0630 4:91da6632d547 71 switch(count){
max_0630 4:91da6632d547 72 case 0:r = 0.0;break;
max_0630 4:91da6632d547 73 case 1:r=0.07;break;
max_0630 4:91da6632d547 74 case 2:r=0.13;break;
max_0630 4:91da6632d547 75 case 3:r=0.20;break;
max_0630 4:91da6632d547 76 case 4:r=0.27;break;
max_0630 4:91da6632d547 77 case 5:r=0.34;break;
max_0630 4:91da6632d547 78 case 6:r=0.41;break;
max_0630 4:91da6632d547 79 case 7:r=0.48;break;
max_0630 4:91da6632d547 80 case 8:r=0.55;break;
max_0630 4:91da6632d547 81 case 9:r=0.62;break;
max_0630 4:91da6632d547 82 case 10:r=0.69;break;
max_0630 4:91da6632d547 83 case 11:r=0.76;break;
max_0630 4:91da6632d547 84 case 12:r=0.83;break;
max_0630 4:91da6632d547 85 case 13:r=0.90;break;
max_0630 4:91da6632d547 86 case 14:r=0.97;break;
max_0630 4:91da6632d547 87 }
max_0630 4:91da6632d547 88 //r = (float)(count%COUNT_MAX)/COUNT_MAX;
funa0825 3:b1115b34e95b 89 led1 = r;
max_0630 4:91da6632d547 90 //r += 0.01;
funa0825 3:b1115b34e95b 91 lcd.locate(0,16);
funa0825 3:b1115b34e95b 92 lcd.printf("R: %3.2f",r);
funa0825 3:b1115b34e95b 93 three_slots.release();
funa0825 3:b1115b34e95b 94 Thread::wait(100);
funa0825 3:b1115b34e95b 95
funa0825 3:b1115b34e95b 96 }
funa0825 3:b1115b34e95b 97 while(r>0)
funa0825 3:b1115b34e95b 98 {
funa0825 3:b1115b34e95b 99 three_slots.wait();
max_0630 4:91da6632d547 100 switch(count){
max_0630 4:91da6632d547 101 case 0:r = 0.0;break;
max_0630 4:91da6632d547 102 case 1:r=0.07;break;
max_0630 4:91da6632d547 103 case 2:r=0.13;break;
max_0630 4:91da6632d547 104 case 3:r=0.20;break;
max_0630 4:91da6632d547 105 case 4:r=0.27;break;
max_0630 4:91da6632d547 106 case 5:r=0.34;break;
max_0630 4:91da6632d547 107 case 6:r=0.41;break;
max_0630 4:91da6632d547 108 case 7:r=0.48;break;
max_0630 4:91da6632d547 109 case 8:r=0.55;break;
max_0630 4:91da6632d547 110 case 9:r=0.62;break;
max_0630 4:91da6632d547 111 case 10:r=0.69;break;
max_0630 4:91da6632d547 112 case 11:r=0.76;break;
max_0630 4:91da6632d547 113 case 12:r=0.83;break;
max_0630 4:91da6632d547 114 case 13:r=0.90;break;
max_0630 4:91da6632d547 115 case 14:r=0.97;break;
max_0630 4:91da6632d547 116 }
max_0630 4:91da6632d547 117 //r = (float)(count%COUNT_MAX)/COUNT_MAX;
funa0825 3:b1115b34e95b 118 led1 = r;
max_0630 4:91da6632d547 119 //r -= 0.01;
funa0825 3:b1115b34e95b 120 lcd.locate(0,16);
funa0825 3:b1115b34e95b 121 lcd.printf("R: %3.2f",r);
funa0825 3:b1115b34e95b 122 three_slots.release();
funa0825 3:b1115b34e95b 123 Thread::wait(100);
funa0825 3:b1115b34e95b 124
funa0825 3:b1115b34e95b 125 }
funa0825 3:b1115b34e95b 126 }
funa0825 3:b1115b34e95b 127 }
funa0825 3:b1115b34e95b 128 void g_thread(void const *argument)
chris 0:f86c572491c3 129 {
funa0825 3:b1115b34e95b 130 float g = 0;
funa0825 3:b1115b34e95b 131 while (true)
funa0825 3:b1115b34e95b 132 {
funa0825 3:b1115b34e95b 133 g=0;
funa0825 3:b1115b34e95b 134 while(g<=1)
funa0825 3:b1115b34e95b 135 {
funa0825 3:b1115b34e95b 136 three_slots.wait();
max_0630 4:91da6632d547 137 switch(count){
max_0630 4:91da6632d547 138 case 0:g = 0.50;break;
max_0630 4:91da6632d547 139 case 1:g = 0.50;break;
max_0630 4:91da6632d547 140 case 2:g=0.63;break;
max_0630 4:91da6632d547 141 case 3:g=0.63;break;
max_0630 4:91da6632d547 142 case 4:g=0.77;break;
max_0630 4:91da6632d547 143 case 5:g=0.77;break;
max_0630 4:91da6632d547 144 case 6:g=0.90;break;
max_0630 4:91da6632d547 145 case 7:g=0.90;break;
max_0630 4:91da6632d547 146 case 8:g=0.47;break;
max_0630 4:91da6632d547 147 case 9:g=0.47;break;
max_0630 4:91da6632d547 148 case 10:g=0.33;break;
max_0630 4:91da6632d547 149 case 11:g=0.33;break;
max_0630 4:91da6632d547 150 case 12:g=0.20;break;
max_0630 4:91da6632d547 151 case 13:g=0.20;break;
max_0630 4:91da6632d547 152 case 14:g=0.07;break;
max_0630 4:91da6632d547 153 }
funa0825 3:b1115b34e95b 154 led2 = g;
max_0630 4:91da6632d547 155 //g += 0.01;
funa0825 3:b1115b34e95b 156 lcd.locate(40,16);
funa0825 3:b1115b34e95b 157 lcd.printf("G: %3.2f",g);
funa0825 3:b1115b34e95b 158 three_slots.release();
funa0825 3:b1115b34e95b 159 Thread::wait(200);
funa0825 3:b1115b34e95b 160 }
funa0825 3:b1115b34e95b 161 while(g>0)
funa0825 3:b1115b34e95b 162 {
funa0825 3:b1115b34e95b 163 three_slots.wait();
max_0630 4:91da6632d547 164 switch(count){
max_0630 4:91da6632d547 165 case 0:g = 0.50;break;
max_0630 4:91da6632d547 166 case 1:g = 0.50;break;
max_0630 4:91da6632d547 167 case 2:g=0.63;break;
max_0630 4:91da6632d547 168 case 3:g=0.63;break;
max_0630 4:91da6632d547 169 case 4:g=0.77;break;
max_0630 4:91da6632d547 170 case 5:g=0.77;break;
max_0630 4:91da6632d547 171 case 6:g=0.90;break;
max_0630 4:91da6632d547 172 case 7:g=0.90;break;
max_0630 4:91da6632d547 173 case 8:g=0.47;break;
max_0630 4:91da6632d547 174 case 9:g=0.47;break;
max_0630 4:91da6632d547 175 case 10:g=0.33;break;
max_0630 4:91da6632d547 176 case 11:g=0.33;break;
max_0630 4:91da6632d547 177 case 12:g=0.20;break;
max_0630 4:91da6632d547 178 case 13:g=0.20;break;
max_0630 4:91da6632d547 179 case 14:g=0.07;break;
max_0630 4:91da6632d547 180 }
funa0825 3:b1115b34e95b 181 led2 = g;
max_0630 4:91da6632d547 182 //g -= 0.01;
funa0825 3:b1115b34e95b 183 lcd.locate(40,16);
funa0825 3:b1115b34e95b 184 lcd.printf("G: %3.2f",g);
funa0825 3:b1115b34e95b 185 three_slots.release();
funa0825 3:b1115b34e95b 186 Thread::wait(200);
funa0825 3:b1115b34e95b 187 }
funa0825 3:b1115b34e95b 188 }
funa0825 3:b1115b34e95b 189 }
funa0825 3:b1115b34e95b 190 void b_thread(void const *argument)
funa0825 3:b1115b34e95b 191 {
funa0825 3:b1115b34e95b 192 float b = 0;
funa0825 3:b1115b34e95b 193 while (true)
funa0825 3:b1115b34e95b 194 {
funa0825 3:b1115b34e95b 195 b=0;
funa0825 3:b1115b34e95b 196 while(b<=1)
funa0825 3:b1115b34e95b 197 {
funa0825 3:b1115b34e95b 198 three_slots.wait();
max_0630 4:91da6632d547 199 switch(count){
max_0630 4:91da6632d547 200 case 0:b = 1.00;break;
max_0630 4:91da6632d547 201 case 1:b = 1.00;break;
max_0630 4:91da6632d547 202 case 2:b=1.00;break;
max_0630 4:91da6632d547 203 case 3:b=0.80;break;
max_0630 4:91da6632d547 204 case 4:b=0.80;break;
max_0630 4:91da6632d547 205 case 5:b=0.80;break;
max_0630 4:91da6632d547 206 case 6:b=0.60;break;
max_0630 4:91da6632d547 207 case 7:b=0.60;break;
max_0630 4:91da6632d547 208 case 8:b=0.60;break;
max_0630 4:91da6632d547 209 case 9:b=0.40;break;
max_0630 4:91da6632d547 210 case 10:b=0.40;break;
max_0630 4:91da6632d547 211 case 11:b=0.40;break;
max_0630 4:91da6632d547 212 case 12:b=0.20;break;
max_0630 4:91da6632d547 213 case 13:b=0.20;break;
max_0630 4:91da6632d547 214 case 14:b=0.20;break;
max_0630 4:91da6632d547 215 }
funa0825 3:b1115b34e95b 216 led3 = b;
max_0630 4:91da6632d547 217 //b += 0.01;
funa0825 3:b1115b34e95b 218 lcd.locate(80,16);
funa0825 3:b1115b34e95b 219 lcd.printf("B: %3.2f",b);
funa0825 3:b1115b34e95b 220 three_slots.release();
funa0825 3:b1115b34e95b 221 Thread::wait(300);
chris 2:cfd337edff08 222 }
funa0825 3:b1115b34e95b 223 while(b>0)
funa0825 3:b1115b34e95b 224 {
funa0825 3:b1115b34e95b 225 three_slots.wait();
max_0630 4:91da6632d547 226 switch(count){
max_0630 4:91da6632d547 227 case 0:b = 1.00;break;
max_0630 4:91da6632d547 228 case 1:b = 1.00;break;
max_0630 4:91da6632d547 229 case 2:b=1.00;break;
max_0630 4:91da6632d547 230 case 3:b=0.80;break;
max_0630 4:91da6632d547 231 case 4:b=0.80;break;
max_0630 4:91da6632d547 232 case 5:b=0.80;break;
max_0630 4:91da6632d547 233 case 6:b=0.60;break;
max_0630 4:91da6632d547 234 case 7:b=0.60;break;
max_0630 4:91da6632d547 235 case 8:b=0.60;break;
max_0630 4:91da6632d547 236 case 9:b=0.40;break;
max_0630 4:91da6632d547 237 case 10:b=0.40;break;
max_0630 4:91da6632d547 238 case 11:b=0.40;break;
max_0630 4:91da6632d547 239 case 12:b=0.20;break;
max_0630 4:91da6632d547 240 case 13:b=0.20;break;
max_0630 4:91da6632d547 241 case 14:b=0.20;break;
max_0630 4:91da6632d547 242 }
funa0825 3:b1115b34e95b 243 led3 = b;
max_0630 4:91da6632d547 244 //b -= 0.01;
funa0825 3:b1115b34e95b 245 lcd.locate(80,16);
funa0825 3:b1115b34e95b 246 lcd.printf("B: %3.2f",b);
funa0825 3:b1115b34e95b 247 three_slots.release();
funa0825 3:b1115b34e95b 248 Thread::wait(300);
funa0825 3:b1115b34e95b 249 }
funa0825 3:b1115b34e95b 250 }
funa0825 3:b1115b34e95b 251 }
funa0825 3:b1115b34e95b 252 void count_thread(void const *argument)
funa0825 3:b1115b34e95b 253 {
funa0825 3:b1115b34e95b 254 while (true)
funa0825 3:b1115b34e95b 255 {
funa0825 3:b1115b34e95b 256 three_slots.wait();
funa0825 3:b1115b34e95b 257 lcd.locate(0,3);
max_0630 4:91da6632d547 258 lcd.printf("Counting : %d ",i);
funa0825 3:b1115b34e95b 259 three_slots.release();
funa0825 3:b1115b34e95b 260 Thread::wait(100);
chris 0:f86c572491c3 261 }
funa0825 3:b1115b34e95b 262 }
funa0825 3:b1115b34e95b 263 int main()
funa0825 3:b1115b34e95b 264 {
funa0825 3:b1115b34e95b 265 wait(0.01);
funa0825 3:b1115b34e95b 266 for_chat.start();
funa0825 3:b1115b34e95b 267 myup.fall(&intRise);
funa0825 3:b1115b34e95b 268 mydown.fall(&intFall);
funa0825 3:b1115b34e95b 269 Thread thread1(r_thread,(void *)"Th 1");
funa0825 3:b1115b34e95b 270 Thread thread2(g_thread,(void *)"Th 2");
funa0825 3:b1115b34e95b 271 Thread thread3(b_thread,(void *)"Th 3");
funa0825 3:b1115b34e95b 272 Thread thread4(count_thread,(void *)"Th 4");
funa0825 3:b1115b34e95b 273
funa0825 3:b1115b34e95b 274 lcd.cls();
funa0825 3:b1115b34e95b 275 wait(0.01);
funa0825 3:b1115b34e95b 276 while(1)
funa0825 3:b1115b34e95b 277 {
funa0825 3:b1115b34e95b 278 //lcd.cls();
funa0825 3:b1115b34e95b 279
funa0825 3:b1115b34e95b 280 }
funa0825 3:b1115b34e95b 281 }
funa0825 3:b1115b34e95b 282