
Modulo Bluetooh controla PWM de un LED
Revision 1:ccce94774ade, committed 2019-07-11
- Comitter:
- junmorenodi
- Date:
- Thu Jul 11 13:31:33 2019 +0000
- Parent:
- 0:9f953d3871c9
- Commit message:
- HC05 Corregido;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Jul 07 17:37:54 2019 +0000 +++ b/main.cpp Thu Jul 11 13:31:33 2019 +0000 @@ -1,23 +1,41 @@ #include "mbed.h" +#include "vector" +#include "string" +#include "list" +#include "math.h" + Serial pc(USBTX, USBRX); Serial blue(PG_14,PG_9); DigitalOut led2(LED2); DigitalOut led3(LED3); -PwmOut ledPWM(PA_3); +PwmOut ledPWM(D13); +float power(float base,int potencia); +using namespace std; - - - +float power(float base,int potencia){ + float resultado=0; + if(potencia>1){ + for(int i=1;i<potencia;i++){ + resultado=resultado+base*base; + } + }else if(potencia==1){ + resultado=base; + }else{ + resultado=1; + } + return resultado; +} + int main() { blue.baud(9600); pc.baud(9600); pc.printf("Start \r\n"); + int i=0,cont=0; float data_sent=0; float unid=0; float dec=0; float cent=0; - char ok='0'; float new_data=0; float period=0.001; float duty=1.0f; @@ -42,50 +60,49 @@ }*/ while(1){ - - for (int i=0; i<3;i++) - { - - data_sent=blue.getc(); - data_sent=data_sent-'0'; - - if (i==0){ - cent=data_sent*100; - } - if (i==1){ - dec=data_sent*10; + unid=0; + dec=0; + cent=0; + for ( i=0; i<3;i++){ + data_sent=blue.getc(); + if(data_sent==97){ + break; + }else{ + data_sent=data_sent-'0'; + if (i==0){ + cent=data_sent; + } + if (i==1){ + dec=data_sent; + } + if (i==2){ + unid=data_sent; + } + } + cont=i; } - if (i==2){ - unid=data_sent; - } + wait(1); + cent=cent*power(10,cont); + dec=dec*power(10,cont-1); + unid=unid*power(10,cont-2); + pc.printf("Cont %.1i \r\n", cont); + pc.printf("Centenas %.1f \r\n", cent); + pc.printf("Decenas %.1f \r\n", dec); + pc.printf("Unidades %.1f \r\n", unid); + new_data=unid+dec+cent; - - } - wait(1); - - new_data=unid+dec+cent; - - if (new_data==100.0){ - led2=1; - led3=0; + if (new_data==100.0){ + led2=1; + led3=0; } - else { - led3=1; - led2=0; + else{ + led3=1; + led2=0; } - - duty=new_data/100; - - ledPWM.period(period); - ledPWM.write(duty); - + duty=new_data/100; + ledPWM.period(period); + ledPWM.write(duty); pc.printf("Valor Data_Sent: %.1f \r\n", new_data); pc.printf("PWM Duty Cycle: %.2f %\r\n", duty*100); - } - - - - - - -} + } +} \ No newline at end of file