xxxxxxxxxx
35
//Sketch modified from https://github.com/shfitz
let serial; // variable for the serial object
let colors = [0,0,0];
function setup() {
createCanvas(windowWidth, windowHeight);
// serial constructor
serial = new p5.SerialPort();
// serial port to use - you'll need to change this
serial.open('COM5');
// what to do when we get serial data
serial.on('data', gotData);
}
// when data is received in the serial buffer
function draw() {
let red = int(map(colors[0],0,1023,0,255));
let green = int(map(colors[1],0,1023,0,255));
let blue = int(map(colors[2],0,1023,0,255));
background(red,green, blue);
text(str(red)+" "+str(green)+" "+str(blue), width/2,height/2);
}
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
colors = split(currentString, ',');
//console.log(colors);
}