Control LED RGB with serial port to example using is send of serial port this string "255#255#255" this string is equal => Red = 255; Green = 255; Blue = 255;

Dependencies:   mbed

main.cpp

Committer:
jhon309
Date:
2015-06-25
Revision:
0:1f60ca10b221

File content as of revision 0:1f60ca10b221:

#include "mbed.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

PwmOut pwmRed(D9);//conector PWM/D9
PwmOut pwmGreen(D10);//conector PWM/D10
PwmOut pwmBlue(D11);//conector PWM/D11
Serial pc(USBTX, USBRX);

float const variation = 0.0039215686274509803921568627451;
float Red = 0;
float Green = 0;
float Blue = 0;

int main()
{    
    while (1) 
    {
        if(pc.readable()) 
        {
            char buffer[128];        
            pc.gets(buffer, 20); 
            Red = atof(strtok(buffer, "#"))* variation;
            Green = atof(strtok(NULL, "#")) * variation;
            Blue = atof(strtok(NULL, "#")) * variation;
            printf("%f %f %f \n", Red, Green, Blue); 
        }        
        pwmRed.write(Red);     
        pwmGreen.write(Green);  
        pwmBlue.write(Blue);  
        pwmRed.period_ms(1);
        pwmGreen.period_ms(1);
        pwmBlue.period_ms(1);  
    }
}