Advertisement
6677

Untitled

Mar 27th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1.  
  2.  
  3. // pins for the LEDs:
  4. const int redPin = 3;
  5. const int greenPin = 5;
  6. const int bluePin = 6;
  7.  
  8. void setup() {
  9. // initialize serial:
  10. Serial.begin(9600);
  11.  
  12.  
  13. while (!Serial) {//No idea why but the leonardo needs this apparently
  14.  
  15. }
  16. // make the pins outputs:
  17. pinMode(redPin, OUTPUT);
  18. pinMode(greenPin, OUTPUT);
  19. pinMode(bluePin, OUTPUT);
  20.  
  21. }
  22.  
  23. void loop() {
  24. // if there's any serial available, read it:
  25. while (Serial.available() > 0) {
  26.  
  27. // look for the next valid integer in the incoming serial stream:
  28. int red = Serial.parseInt();
  29. // do it again:
  30. int green = Serial.parseInt();
  31. // do it again:
  32. int blue = Serial.parseInt();
  33.  
  34. // look for the newline. That's the end of your
  35. // sentence:
  36. if (Serial.read() == '\n') {
  37. // constrain the values to 0 - 255 and invert
  38. // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
  39. red = 255 - constrain(red, 0, 255);
  40. green = 255 - constrain(green, 0, 255);
  41. blue = 255 - constrain(blue, 0, 255);
  42.  
  43. // fade the red, green, and blue legs of the LED:
  44. analogWrite(redPin, red);
  45. analogWrite(greenPin, green);
  46. analogWrite(bluePin, blue);
  47.  
  48. // print the three numbers in one string as hexadecimal:
  49. Serial.print(red, HEX);
  50. Serial.print(green, HEX);
  51. Serial.println(blue, HEX);
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement