xxxxxxxxxx
63
/* Danny Rozin
Introduction to Physical Computing
ITP
This sketch will send 2 values as ascii from P5 to arduino
See arduino code in bottom, have LED connected to pin 3 and 5
move mouseX to dim LED*/
var serial; // variable to hold an instance of the serialport library
var fromSerial = 0; //variable to hold the data
function setup() {
createCanvas(255, 255);
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/tty.usbmodem144201"); // open a port
}
function draw() {
background(0,0,255);
var firstValueToSend = mouseX;
var secondValueToSend = mouseY;
// serial.write(firstValueToSend + ","); // this makes it a string and adds a comma
// serial.write(secondValueToSend +","); // this makes it a string and adds a comma
// serial.write("\n" ); // this adds a linefeed in end (ascii 10)
serial.write(firstValueToSend + ','); // this makes it a string and adds a comma
serial.write(secondValueToSend + ','); // this makes it a string and adds a comma
// serial.write("\n" ); // this adds a linefeed in end (ascii 10)
}
// get the list of ports:
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
}
function serialEvent() {
// this is called when data is recieved
}
/*
// Arduino Code
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
int fromSerial = Serial.parseInt(); // read first part of string as int
analogWrite(3, fromSerial);
fromSerial = Serial.parseInt(); // read second part of string as int
analogWrite(5, fromSerial);
}
}
*/