Guest User

Untitled

a guest
Jan 18th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. int r,g,b;
  2. char colors[3];
  3. String str;
  4. boolean can_change = true;
  5.  
  6. int redPin = 9;
  7. int greenPin = 10;
  8. int bluePin = 11;
  9.  
  10. void setup() {
  11. Serial.begin(115200);
  12. }
  13.  
  14. void loop() {
  15. str = Serial.readString();
  16.  
  17. r = getValue(str, ':', (int)0).toInt();
  18. g = getValue(str, ':', (int)1).toInt();
  19. b = getValue(str, ':', (int)2).toInt();
  20. setColor(r,g,b);
  21.  
  22. Serial.println("can_send");
  23.  
  24. }
  25.  
  26. void setColor(int red, int green, int blue)
  27. {
  28. #ifdef COMMON_ANODE
  29. red = 255 - red;
  30. green = 255 - green;
  31. blue = 255 - blue;
  32. #endif
  33. analogWrite(redPin, red);
  34. analogWrite(greenPin, green);
  35. analogWrite(bluePin, blue);
  36. }
  37.  
  38.  
  39. String getValue(String data, char separator, int index)
  40. {
  41. int found = 0;
  42. int strIndex[] = {0, -1};
  43. int maxIndex = data.length()-1;
  44.  
  45. for(int i=0; i<=maxIndex && found<=index; i++){
  46. if(data.charAt(i)==separator || i==maxIndex){
  47. found++;
  48. strIndex[0] = strIndex[1]+1;
  49. strIndex[1] = (i == maxIndex) ? i+1 : i;
  50. }
  51. }
  52.  
  53. return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  54. }
Advertisement
Add Comment
Please, Sign In to add comment