Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int numLedRGB = 3;
- int ledRGB[numLedRGB] = {9,10,11}; //Pin LED RGB
- int leerDato;
- int ind = 0;
- boolean iluminamosLED = false; //variable para comprobar si se ilimuna el LEDRGB
- void setup()
- {
- Serial.begin(9600); //comunicacion serial a 9600bps
- for(int i = 0; i < numLedRGB; i++)
- {
- pinMode(ledRGB[i], OUTPUT); //se establece el pin digital de salida
- }
- }
- void loop(){
- if(Serial.available() > 0){
- //leemos valor recibido desde processing
- leerDato = Serial.read();
- //se comprueba si se ilumina el LEDRGB
- if (iluminamosLED)
- {
- //se ilumina el led correspondiene al RGB
- colorRGB(ind, leerDato);
- iluminamosLED = false; //cambiamos el valor para leer que color
- }
- else
- {
- //selecciona el color que se iluminara rojo,verde o azul
- ind = buscarColor(leerDato);
- }
- }
- }
- int buscarColor(int dato)
- {
- switch (dato)
- {
- //R ascii
- case 82:
- iluminamosLED = true;
- return 0;
- break;
- //G ascii
- case 71:
- iluminamosLED = true;
- return 1;
- break;
- //B ascii
- case 66:
- iluminamosLED = true;
- return 2;
- break;
- }
- return -1;
- }
- void colorRGB(int ind, int valColor)
- {
- //PWM del color ind
- //ind = 0, color rojo
- //ind = 1, color verde
- //ind = 2, color azul
- analogWrite(ledRGB[ind], 255-valColor);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement