Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. const int numLedRGB = 3;
  2. int ledRGB[numLedRGB] = {9,10,11};  //Pin LED RGB
  3.  
  4. int leerDato;
  5. int ind = 0;
  6. boolean iluminamosLED = false; //variable para comprobar si se ilimuna el LEDRGB
  7.  
  8. void setup()
  9. {  
  10.   Serial.begin(9600); //comunicacion serial a 9600bps
  11.   for(int i = 0; i < numLedRGB; i++)
  12.   {
  13.     pinMode(ledRGB[i], OUTPUT); //se establece el pin digital de salida
  14.   }
  15. }
  16.  
  17. void loop(){  
  18.   if(Serial.available() > 0){  
  19.     //leemos valor recibido desde processing
  20.     leerDato = Serial.read();
  21.     //se comprueba si se ilumina el LEDRGB
  22.     if (iluminamosLED)
  23.     {
  24.       //se ilumina el led correspondiene al RGB
  25.       colorRGB(ind, leerDato);
  26.       iluminamosLED = false; //cambiamos el valor para leer que color
  27.     }
  28.     else
  29.     {      
  30.       //selecciona el color que se iluminara rojo,verde o azul
  31.       ind = buscarColor(leerDato);
  32.     }
  33.   }
  34. }
  35.  
  36. int buscarColor(int dato)
  37. {  
  38.   switch (dato)
  39.   {    
  40.     //R ascii
  41.     case 82:
  42.       iluminamosLED = true;
  43.       return 0;
  44.       break;
  45.     //G ascii
  46.     case 71:
  47.       iluminamosLED = true;
  48.       return 1;
  49.       break;
  50.     //B ascii  
  51.     case 66:    
  52.       iluminamosLED = true;
  53.       return 2;
  54.       break;
  55.   }
  56.   return -1;
  57. }
  58.  
  59. void colorRGB(int ind, int valColor)
  60. {
  61.     //PWM del color ind
  62.     //ind = 0, color rojo
  63.     //ind = 1, color verde
  64.     //ind = 2, color azul
  65.     analogWrite(ledRGB[ind], 255-valColor);    
  66. }