Program using Touch Sensor and PWM function. This code set a value of brightness in RGB led using Touch Sensor like a slider. The begining of touch is used to select a color. The end of touch is used to select an effect.
Dependencies: MMA8451Q TSI mbed
Fork of FRDM_TSI by
main.cpp
00001 #include "mbed.h" 00002 #include "TSISensor.h" 00003 //#include "MMA8451Q.h" 00004 00005 //#define MMA8451_I2C_ADDRESS (0x1d<<1) 00006 00007 int main(void) 00008 { 00009 // MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); 00010 00011 PwmOut led1(LED_BLUE); 00012 PwmOut led2(LED_GREEN); 00013 PwmOut led3(LED_RED); 00014 00015 float tp = 0; 00016 //float tp_old = 0; 00017 float valor_ledR, valor_ledG, valor_ledB = 1.0; 00018 float valor_inicial, valor_final = 0.0; 00019 float offset = 0.0; 00020 float aux = 0.0; 00021 00022 unsigned char led_atual = 0; 00023 unsigned char ajustando = 0; 00024 unsigned char efeito = 0; 00025 unsigned char blink = 0; 00026 00027 TSISensor tsi; //inicializa rotinas do sensor de toque 00028 00029 led1 = 1.0; //desliga leds 00030 led2 = 1.0; 00031 led3 = 1.0; 00032 00033 valor_ledB = led1; 00034 valor_ledG = led2; 00035 valor_ledR = led3; 00036 00037 valor_inicial = 0.0; 00038 valor_final = 0.0; 00039 00040 efeito = 0; 00041 00042 while (1) 00043 { 00044 //tp = (tsi.readPercentage()>0)?tsi.readPercentage():tp; 00045 00046 tp = tsi.readDistance(); //take actual value of touch sensor 00047 00048 if (tp == 0) //there is no touch 00049 { 00050 00051 if(!ajustando) 00052 { 00053 valor_inicial = 0.0; 00054 valor_final = 0.0; 00055 } 00056 00057 ajustando = 0; //not adjusting 00058 00059 if(led_atual) //there is any color selected? 00060 { 00061 if(!efeito) 00062 { 00063 led3 = valor_ledR; //adjust red led 00064 led2 = valor_ledG; //adjust green led 00065 led1 = valor_ledB; //adjust blue led 00066 } 00067 } 00068 else 00069 { 00070 led3 = led2 = led1 = 1.0; //turn off all colors 00071 } 00072 } 00073 00074 if(efeito) 00075 { 00076 if(led_atual) 00077 { 00078 if(efeito == 1) 00079 { 00080 blink = ~blink; 00081 if(blink) 00082 { 00083 led3 = valor_ledR; //adjust red led 00084 led2 = valor_ledG; //adjust green led 00085 led1 = valor_ledB; //adjust blue led 00086 } 00087 else 00088 { 00089 led3 = 1.0; //adjust red led 00090 led2 = 1.0; //adjust green led 00091 led1 = 1.0; //adjust blue led 00092 } 00093 } 00094 if(efeito == 2) 00095 { 00096 offset = offset + 0.02; 00097 00098 if(offset > 1.0) 00099 { 00100 offset = 0.0; 00101 } 00102 00103 if(valor_ledB < 1.0) 00104 { 00105 led1 = offset; 00106 } 00107 if(valor_ledG < 1.0) 00108 { 00109 led2 = offset; 00110 } 00111 if(valor_ledR < 1.0) 00112 { 00113 led3 = offset; 00114 } 00115 } 00116 00117 if(efeito == 3) 00118 { 00119 offset = offset - 0.02; 00120 00121 if(offset < 0.0) 00122 { 00123 offset = 1.0; 00124 } 00125 00126 if(valor_ledB < 1.0) 00127 { 00128 led1 = offset; 00129 } 00130 if(valor_ledG < 1.0) 00131 { 00132 led2 = offset; 00133 } 00134 if(valor_ledR < 1.0) 00135 { 00136 led3 = offset; 00137 } 00138 } 00139 if(efeito == 4) 00140 { 00141 offset = offset - 0.01; 00142 00143 if(offset < 0.0) 00144 { 00145 offset = 1.0; 00146 } 00147 00148 led1 = offset; 00149 00150 aux = 1 - offset; 00151 if(aux < 0.0) 00152 { 00153 aux = 0.0; 00154 } 00155 led2 = aux; 00156 00157 aux = offset * 2; 00158 if(aux > 1.0) 00159 { 00160 aux = 0.0; 00161 } 00162 led3 = aux; 00163 00164 } 00165 } 00166 //wait(0.1); 00167 } 00168 00169 if((tp > 0) & (tp < 5)) //the colors are select at begin of touch sensor 00170 { 00171 if(!ajustando) //the adjust function is not activeted 00172 { 00173 led1 = 1.0; //turn off all colors 00174 led2 = 1.0; 00175 led3 = 1.0; 00176 00177 led_atual++; //increment actual color selector 00178 00179 if(led_atual > 7) 00180 { 00181 led_atual = 0; //turn off leds 00182 } 00183 00184 if(led_atual == 1) //selected color is blue 00185 { 00186 led1 = valor_ledB = 0.0; //turn on blue led 00187 led2 = valor_ledG = 1.0; 00188 led3 = valor_ledR = 1.0; 00189 } 00190 00191 if(led_atual == 2) //selected color is green 00192 { 00193 led1 = valor_ledB = 1.0; 00194 led2 = valor_ledG = 0.0; //turn on green led 00195 led3 = valor_ledR = 1.0; 00196 } 00197 00198 if(led_atual == 3) //selected color is red 00199 { 00200 led1 = valor_ledB = 1.0; 00201 led2 = valor_ledG = 1.0; 00202 led3 = valor_ledR = 0.0; //turn on red led 00203 } 00204 00205 if(led_atual == 4) 00206 { 00207 led1 = valor_ledB = 0.0; //turn on blue led and 00208 led2 = valor_ledG = 0.0; //turn on green led 00209 led3 = valor_ledR = 1.0; 00210 } 00211 00212 if(led_atual == 5) 00213 { 00214 led1 = valor_ledB = 0.0; //turn on blue led and 00215 led2 = valor_ledG = 1.0; 00216 led3 = valor_ledR = 0.0; //turn on red led 00217 } 00218 00219 if(led_atual == 6) 00220 { 00221 led1 = valor_ledB = 1.0; 00222 led2 = valor_ledG = 0.0; //turn on green led 00223 led3 = valor_ledR = 0.0; //turn on red led 00224 } 00225 00226 if(led_atual == 7) // select color is white 00227 { 00228 led1 = valor_ledB = 0.0; //turn on blue color 00229 led2 = valor_ledG = 0.0; //turn on green color 00230 led3 = valor_ledR = 0.0; //turn on red led 00231 } 00232 00233 wait(0.5); //show selected led to user 00234 } 00235 } 00236 00237 if (tp > 35) //superior side 00238 { 00239 if(!ajustando) 00240 { 00241 efeito++; 00242 if(efeito > 4) 00243 { 00244 efeito = 0; 00245 } 00246 } 00247 //wait(0.5); 00248 } 00249 00250 if((tp > 5)) //set the brightness of select color 00251 { 00252 if(!ajustando) 00253 { 00254 valor_inicial = tp; 00255 } 00256 00257 ajustando = 1; //adjusting is true 00258 00259 valor_final = tp; 00260 00261 //led1 = 1.0; //take off all colors 00262 //led2 = 1.0; 00263 //led3 = 1.0; 00264 00265 if(led_atual == 1) //selected color is blue 00266 { 00267 valor_ledB = valor_ledB + (valor_inicial - valor_final)/200; 00268 00269 if(valor_ledB < 0.0) 00270 { 00271 valor_ledB = 0.0; 00272 } 00273 00274 if(valor_ledB > 1.0) 00275 { 00276 valor_ledB = 1.0; 00277 } 00278 00279 //valor_ledB = (40.0 - tp)/40.0; 00280 00281 led1 = valor_ledB; //put new value on blue led 00282 led2 = valor_ledG = 1.0; //turn off 00283 led3 = valor_ledR = 1.0; //turn off 00284 } 00285 00286 if(led_atual == 2) //selected color is green 00287 { 00288 //valor_ledG = (40.0 - tp)/40.0; 00289 00290 valor_ledG = valor_ledG + (valor_inicial - valor_final)/200; 00291 00292 if(valor_ledG < 0.0) 00293 { 00294 valor_ledG = 0.0; 00295 } 00296 00297 if(valor_ledG > 1.0) 00298 { 00299 valor_ledG = 1.0; 00300 } 00301 00302 led1 = valor_ledB = 1.0; //turn off 00303 led2 = valor_ledG; //put new value on green led 00304 led3 = valor_ledR = 1.0; //turn off 00305 } 00306 00307 if(led_atual == 3) //selected color is red 00308 { 00309 //valor_ledR = (40.0 - tp)/40.0; 00310 00311 valor_ledR = valor_ledR + (valor_inicial - valor_final)/200; 00312 00313 if(valor_ledR < 0.0) 00314 { 00315 valor_ledR = 0.0; 00316 } 00317 00318 if(valor_ledR > 1.0) 00319 { 00320 valor_ledR = 1.0; 00321 } 00322 00323 led1 = valor_ledB = 1.0; //turn off 00324 led2 = valor_ledG = 1.0; //turn off 00325 led3 = valor_ledR; //put new value on red led 00326 } 00327 00328 if(led_atual == 4) 00329 { 00330 //valor_ledB = (40.0 - tp)/40.0; 00331 00332 valor_ledB = valor_ledB + (valor_inicial - valor_final)/200; 00333 00334 if(valor_ledB < 0.0) 00335 { 00336 valor_ledB = 0.0; 00337 } 00338 00339 if(valor_ledB > 1.0) 00340 { 00341 valor_ledB = 1.0; 00342 } 00343 00344 led1 = valor_ledB; //put new value on blue led 00345 led2 = valor_ledG = valor_ledB; //put new value on green led 00346 led3 = valor_ledR = 1.0; //turn off 00347 } 00348 00349 if(led_atual == 5) 00350 { 00351 //valor_ledB = (40.0 - tp)/40.0; 00352 00353 valor_ledB = valor_ledB + (valor_inicial - valor_final)/200; 00354 00355 if(valor_ledB < 0.0) 00356 { 00357 valor_ledB = 0.0; 00358 } 00359 00360 if(valor_ledB > 1.0) 00361 { 00362 valor_ledB = 1.0; 00363 } 00364 00365 led1 = valor_ledB; //put new value on blue led 00366 led2 = valor_ledG = 1.0; //turn off 00367 led3 = valor_ledR = valor_ledB; //put new value on red led 00368 } 00369 00370 if(led_atual == 6) 00371 { 00372 //valor_ledG = (40.0 - tp)/40.0; 00373 00374 valor_ledG = valor_ledG + (valor_inicial - valor_final)/200; 00375 00376 if(valor_ledG < 0.0) 00377 { 00378 valor_ledG = 0.0; 00379 } 00380 00381 if(valor_ledG > 1.0) 00382 { 00383 valor_ledG = 1.0; 00384 } 00385 00386 led1 = valor_ledB = 1.0; //turn off 00387 led2 = valor_ledG; //put new value on green led 00388 led3 = valor_ledR = valor_ledG; //put new value on red led 00389 } 00390 00391 if(led_atual == 7) // select color is white 00392 { 00393 //valor_ledB = (40.0 - tp)/40.0; 00394 00395 valor_ledB = valor_ledB + (valor_inicial - valor_final)/200; 00396 00397 if(valor_ledB < 0.0) 00398 { 00399 valor_ledB = 0.0; 00400 } 00401 00402 if(valor_ledB > 1.0) 00403 { 00404 valor_ledB = 1.0; 00405 } 00406 00407 led1 = valor_ledB; //put new value on blue led 00408 led2 = valor_ledG = valor_ledB; //put new value on green led 00409 led3 = valor_ledR = valor_ledB; //put new value on red led 00410 } 00411 } 00412 00413 //led1 = (tsi.readPercentage()==0)?(1-abs(acc.getAccX())):((tp>0.00 && tp <= 0.33)?(1-(tp-0.00)*3):1); 00414 //led2 = (tsi.readPercentage()==0)?(1-abs(acc.getAccY())):((tp>=0.34 && tp <= 0.66)?(1-(tp-0.33)*3):1); 00415 //led3 = (tsi.readPercentage()==0)?(1-abs(acc.getAccZ())):((tp>=0.67 && tp <= 0.99)?(1-(tp-0.66)*3):1); 00416 00417 wait(0.1); 00418 } 00419 }
Generated on Tue Jul 19 2022 04:55:14 by 1.7.2