Tarea encender un led variar su intensidad con slider, utilizando appinventor.

Dependencies:   mbed

main.cpp

Committer:
caapalacioto
Date:
2017-11-26
Revision:
0:224b47265962
Child:
1:802d25cbc918

File content as of revision 0:224b47265962:

#include "mbed.h"
#include "string.h"

Serial device(PTE0, PTE1);  // tx, rx
Serial pc(USBTX,USBRX);  // pc
PwmOut rojo(LED1);
PwmOut verde(LED2);
PwmOut azul(LED3);
int periodo;
char buffer[10], led[0], d[0];// TAMAÑO DEL BUFER
Timer t;   //VALOR DEL TIEMPO
int i = 0, j;
int c=0;
float r;


int readBuffer(char *buffer,int count)   //esta funcion lee un bufer de datos
{
    int i=0; 
    t.start();    //CUENTA EL TIEMPO DE CONEXION E INICIA
    while(1) {
        while (device.readable()) {
            char c = device.getc();
            if (c == '\r' || c == '\n') c = '$';//si se envia fin de linea o de caracter inserta $
            buffer[i++] = c;//mete al bufer el caracter leido
            if(i > count)break;//sale del loop si ya detecto terminacion
        }
        if(i > count)break;
        if(t.read() > 1) {  //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE
            t.stop();
            t.reset();
            break;
        }
    }
     return 0;
}
 
void cleanBuffer(char *buffer, int count)  //esta funcion limpia el bufer
{
    for(int i=0; i < count; i++) {
        buffer[i] = '\0';
    }
}



int main()
{
    device.baud(9600);
    device.format(8,Serial::None,1); 
    pc.baud(9600);   // asigno baudios y configuro puerto serie de la PC
     pc.format(8,Serial::None,1); 
    periodo=90;  // Periodo del pulso
    
// apago los led
    verde=1;    
    rojo=1;
    azul=1;
    
 //  Asignamos el periodo del pulso a los leds
    azul.period_ms(periodo);
    rojo.period_ms(periodo);
    verde.period_ms(periodo);
          verde= 0;
          wait(1.5);
          verde=1;
    while (1) {

       if (device.readable()) {
          readBuffer(buffer,10);
          wait(0.1);
          sscanf(buffer,"%c %s",d,led); //la informacion llega con una letra que indica el color y un numero que indica el valor
          r=atof(d);
          if (led[0]=='R') //rojo
          {
          rojo=(1-r*0.01);
            pc.putc(r);
          }
          if (led[0]=='A') //amarillo
          {
          azul=(1-r*0.01);
            pc.putc(r);
          }
          if (led[0]=='V') //verde
          {
          verde=(1-r*0.01);
            pc.putc(r);
          }
          }
        cleanBuffer(buffer,1);  // limpiamos buffer
    }
}