Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. String firstValue, secondValue, thirdValue, myString, inString="";
  2. int r,g,b,inChar;
  3. void setup()
  4. {
  5.   pinMode(9, OUTPUT);
  6.   pinMode(10, OUTPUT);
  7.   pinMode(11, OUTPUT);
  8.   // Open serial communications and wait for port to open:
  9.   Serial.begin(9600);
  10. }
  11. void loop()
  12. {
  13.   if (Serial.available()>0)
  14.   {
  15.     inChar = Serial.read();
  16.     changeValues(inChar);
  17.   }
  18. }
  19.  
  20. void changeValues(int X)
  21. {
  22.   // convert the incoming byte to a char and add it to the string:
  23.     inString += (char)X;
  24.     int commaIndex = inString.indexOf('-');
  25.     //  Search for the next comma just after the first
  26.     int secondCommaIndex = inString.indexOf('-', commaIndex + 1);
  27.     firstValue = inString.substring(0, commaIndex);
  28.     secondValue = inString.substring(commaIndex + 1, secondCommaIndex);
  29.     thirdValue = inString.substring(secondCommaIndex + 1); // To the end of the string
  30.     r = firstValue.toInt();
  31.     g = secondValue.toInt();
  32.     b = thirdValue.toInt();
  33.     analogWrite(9,r);
  34.     analogWrite(10,g);
  35.     analogWrite(11,b);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement