xxxxxxxxxx
125
// Declare a "SerialPort" object
var serial;
var latestData = "waiting for data"; // you'll use this to write incoming data to the canvas
var data = 0;
var fanSpeed
var fanAngle
var output;
var autoplayfan;
var xtime;
var AngleTime;
var fanDirA;
var fanDirB;
function setup() {
createCanvas(windowWidth, windowHeight);
// Instantiate our SerialPort object
frameRate(100);
// serial = new p5.SerialPort();
// var options = { baudrate: 9600}; // change the data rate to whatever you wish
// serial.open("/dev/cu.usbmodem1411",options);
// serial.on('data', gotData);
angleMode(DEGREES);
var data = map(latestData, 0, 1023, 0, height);
//fanMovement();
fanSpeed = 100;
fanAngle = 90;
autoplayfan = 1;
xtime = 1;
AngleTime = 1;
fanDirA = 1;
fanDirB = -0.5;
}
//There is data available to work with from the serial port
function gotData() {
var currentString = serial.readLine(); // read the incoming string
//same as readStringUntil(‘\r\n’)
trim(currentString); // remove any trailing whitespace
if (!currentString) return; // if the string is empty, do no more
latestData = int(currentString); // save it for the draw method
//console.log(latestData); // println the string
// var output = map(mouseX,0,width,0,255);
// fanAngle = fanSpeed += 3;
// if (fanAngle > width) {
// fanAngle = 100
//}
//console.log(output);
//output = int(map(mouseX, 0, windowWidth, 0, 1023));
// output = autoplayfan
//serial.write(output, +'/n');
serial.write(output);
console.log(output);
}
function draw() {
xtime ++;
AngleTime = (253 / fanDirA);
//autoplayfan += .5;
output = map(mouseX, 0, width, 0, 255);
background(70, 200, 110);
fill(0, 0, 0);
var data = map(latestData, 0, 1023, 0, height);
fill(random(data), 0, random(data));
var degree = map(output, 0, 255, 280, 440);
//console.log(degree);
fill(0);
arc(width / 2, height / 2, 400, 400, degree-90, -degree-90, PIE);
text(degree - 180, width / 2, height / 1.8);
}
// if (xtime > 0 && xtime <= AngleTime) {
// autoplayfan += fanDirA;
// }
// else if (xtime >= AngleTime && xtime <= (2 * AngleTime)) {
// autoplayfan -= fanDirA;
// }
// else if (xtime >= 2 * AngleTime) {
// autoplayfan = 5;
// xtime = 5;
// }
// console.log("time", xtime);
// console.log("autopla",autoplayfan);
// console.log("output", output);
// }
// function fanMovement()
// {
// fanAngle = mouseX;
// var output = map(fanAngle,0,width,0,255);
// serial.write(output);
// }