xxxxxxxxxx
72
/* Danny Rozin
Introduction to Physical Computing
ITP
This sketch will send one value in ascii from arduino to P5
See arduino code in bottom, have pot connected to A0*/
var serial; // variable to hold an instance of the serialport library
var fromSerial = 0; //variable to hold the data
let array = [],
count = 0,
total = 0,
avg = 0;
function setup() {
createCanvas(320, 240);
serial = new p5.SerialPort(); // make a new instance of serialport librar
serial.on('list', printList); // callback function for serialport list event
serial.on('data', serialEvent); // callback for new data coming in
serial.list(); // list the serial ports
serial.open("/dev/cu.usbmodem1421"); // open a port
}
function draw() {
background(255);
total = 0;
count = ((count++)+5)%5;
if (count < 5) {
array[count] = fromSerial;
//count++;
for (let i = 0; i < array.length; i++) {
total += array[i];
}
avg = total / array.length;
console.log(avg);
}
textSize(avg);
text(avg, 0, height / 2);
console.log(array);
}
// get the list of ports:
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
function serialEvent() {
// this is called when data is recieved, data will then live in fromSerial
var stringFromSerial = serial.readLine();
if (stringFromSerial.length > 0) {
var trimmedString = trim(stringFromSerial);
fromSerial = Number(trimmedString);
}
}
/*
// Arduino Code
void setup() {
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(A0);
Serial.println(analogValue);
delay(50);
}
*/