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