xxxxxxxxxx
239
let switchcase=2; // 0--> elliptical brush, 1--> blurry brush, 2--> generative art
let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem101"; // fill in your serial port name here
let xPos=0;
let yPos=0;
let onOff=0;
let inData=0; //potentiometer 1
let indata=0; // potentiometer 2
var i=2
var angle = 2.0;
var incangle = 3.5;
var speed = 0.1;
let autoRotate = false;
let manualRotationAngle = 0;
let autoHue = true;
let manualHue = 0;
function setup() {
createCanvas(400, 400);
background(0);
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on("list", printList); // set a callback function for the serialport list event
serial.on("connected", serverConnected); // callback for connecting to the server
serial.on("open", portOpen); // callback for the port opening
serial.on("data", serialEvent); // callback for when new data arrives
serial.on("error", serialError); // callback for errors
serial.on("close", portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
}
function serialEvent() {
let inString = serial.readLine();
if (inString.length > 0) {
let sensors = split(inString, ",");
if (sensors.length == 2) {
inData=sensors[0];
indata=sensors[1];
}
}
print(indata,inData)
}
function draw() {
xPos=map (inData,0,255,2,400);
yPos=map (indata,0,255,2,400);
xp=map (inData,0,255,0,400);
yp=map (indata,0,255,0,400);
switch (switchcase) {
case 0: // elliptical brush stroke
colorMode(HSB);
translate(xPos, yPos);
rotate(autoRotate ? frameCount : manualRotationAngle);
noStroke();
const hue = autoHue ? frameCount % 360 : manualHue;
fill(hue, 255, 255);
const brushWidth = 20;
const bristleSeparation = 5;
for (let xo = -brushWidth / 2; xo <= brushWidth / 2; xo += bristleSeparation) {
ellipse(xo, xo, 5, 2);
}
break;
case 1:
noStroke()
linearGradient(
width/2-200, height/2-200, //Start point
width/2+100, height/2+100, //End point
color(50, 90, 540, 80), //Start color
color(200, 100, 100, 100) //End color
);
rect(xp,yp, 30,30,100)
//print("h", xp)
break;
case 2:
i+=1
//spiral code
push()
noStroke();
fill(255)
//background(0)
var x = 200 + cos(angle) * incangle; //middle of screen
var y = 200 + sin(angle) * incangle; //middle of screen
fill(194, 245, 219);
noStroke();
ellipse(x, y, 5, 5);
angle += speed;
incangle += speed;
pop()
push()
fill(34, 228, 172)
translate(200, 200)
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
ellipse(200, 200, xp,yp);
rotate(PI/5);
pop()
push()
fill(34, 228, 172)
translate(200,200)
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
ellipse(200, 200, xp+5,yp+5);
rotate(PI/5);
pop()
push()
translate(200,200)
fill(27, 215, 187)
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
ellipse(200, 200, xp-100,yp-100);
rotate(PI/5);
pop()
push()
translate(200,200)
fill(20, 201, 203)
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
ellipse(200, 200, xp-200,yp-200);
rotate(PI/5);
pop()
break;
}}
function linearGradient(sX, sY, eX, eY, colorS, colorE){
let gradient = drawingContext.createLinearGradient(
sX, sY, eX, eY
);
gradient.addColorStop(0, colorS);
gradient.addColorStop(1, colorE);
drawingContext.fillStyle = gradient;
}
function printList(portList) {
for (let i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
}
function serverConnected() {
print("connected to server.");
}
function portOpen() {
print("the serial port opened.");
}
function serialError(err) {
print("Something went wrong with the serial port. " + err);
}
function portClose() {
print("The serial port closed.");
}