raderraderrader

Processing Color Mixer

Aug 13th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1.  
  2.  
  3. // This example code is in the public domain.
  4.  
  5. import processing.serial.*;
  6.  
  7. float redValue = 0; // red value
  8. float greenValue = 0; // green value
  9. float blueValue = 0; // blue value
  10. float alphaValue = 0; // alpha value
  11.  
  12. Serial myPort;
  13.  
  14. void setup() {
  15. size(512, 512 );
  16.  
  17. // List all the available serial ports
  18. // if using Processing 2.1 or later, use Serial.printArray()
  19. println(Serial.list());
  20.  
  21. // I know that the first port in the serial list on my Mac is always my
  22. // Arduino, so I open Serial.list()[0].
  23. // Open whatever port is the one you're using.
  24. myPort = new Serial(this, Serial.list()[0], 9600);
  25. // don't generate a serialEvent() unless you get a newline character:
  26. myPort.bufferUntil('\n');
  27. }
  28.  
  29. void draw() {
  30. // set the background color with the color values:
  31. background(redValue, greenValue, blueValue, alphaValue);
  32. }
  33.  
  34. void serialEvent(Serial myPort) {
  35. // get the ASCII string:
  36. String inString = myPort.readStringUntil('\n');
  37.  
  38. if (inString != null) {
  39. // trim off any whitespace:
  40. inString = trim(inString);
  41. // split the string on the commas and convert the resulting substrings
  42. // into an integer array:
  43. float[] colors = float(split(inString, ","));
  44. // if the array has at least three elements, you know you got the whole
  45. // thing. Put the numbers in the color variables:
  46. if (colors.length >= 3) {
  47. // map them to the range 0-255:
  48. redValue = map(colors[0], 0, 1023, 0, 255);
  49. greenValue = map(colors[1], 0, 1023, 0, 255);
  50. blueValue = map(colors[2], 0, 1023, 0, 255);
  51. alphaValue = map(colors[3], 0, 1, 0, 255);
  52.  
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment