Advertisement
franciscominajas

ArduinoP1RGB

Sep 22nd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int pinLED = 13;
  2. int pinLedR = 11;  // pin Rojo del led RGB
  3. int pinLedV = 10;  // pin Verde del led RGB
  4. int pinLedA = 9;   // pin Azul del led RGB
  5. float valor;
  6. String cadena;
  7. void setup()
  8. {
  9.    Serial.begin(9600);
  10.    pinMode(pinLedR, OUTPUT);    // pone el pinLedR como output
  11.    pinMode(pinLedV, OUTPUT);    // pone el pinLedV como output
  12.    pinMode(pinLedA, OUTPUT);    // pone el pinLedA como output
  13. }
  14.  
  15. void loop()
  16. {
  17.   String red="";
  18.   String green="";
  19.   String blue="";
  20.  
  21.   int rojo=0;
  22.   int verde =0;
  23.   int azul =0;
  24.    if (Serial.available()>0)
  25.    {
  26.       while(Serial.available())
  27.       {
  28.         char c =  Serial.read();
  29.         if((c!='\r') && (c!='\n'))
  30.         {
  31.             cadena+=c;
  32.         }
  33.         delay(20);
  34.       }
  35.       if(cadena.charAt(0)=='r')
  36.       {
  37.         for(int i=1:i<cadena.length();i++)
  38.         {
  39.           red+=cadena.charAt(i);
  40.         }
  41.       }
  42.       else if(cadena.charAt(0)=='v')
  43.       {
  44.         for(int i=1:i<cadena.length();i++)
  45.         {
  46.           green+=cadena.charAt(i);
  47.         }
  48.       }
  49.       else if(cadena.charAt(0)==='a')
  50.       {
  51.         for(int i=1:i<cadena.length();i++)
  52.         {
  53.           blue+=cadena.charAt(i);
  54.         }
  55.       }
  56.       rojo = red.toInt();
  57.       verde = green.toInt();
  58.       azul = blue.toInt();
  59.  
  60.       color(rojo, verde, azul);     // apagado
  61.       delay(10);
  62.    }
  63. }
  64.  
  65. // funcion para generar colores
  66. void color (int rojo, int verde, int azul)
  67. {
  68.   analogWrite(pinLedR, rojo);
  69.   analogWrite(pinLedV, verde);
  70.   analogWrite(pinLedA, azul);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement