xxxxxxxxxx
162
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.thisWontWorkEver'; // fill in your serial port name here
var portTest = "fruit"; //This string is what I will look for in the port list to find the one I want. When I used the Adafruit BT module "fruit" was the easy unique identifier. The FTDI chip shoes up as "usbserial" and regular arduinos as "usbmodem"
var logSerial = true; //since the console is slow-ish in p5, once I see serial data comes in I stop logging it there.
var lastSerialString = ""; //however, it is useful to keep the last serial string and maybe show that in the front end; not slow, and helpful for seeing that the app is getting data
var rValue, gValue; //globals for the red and green LED PWM ammount.
var s1, s2, s3, ldr; //Globals to save the 3 switch states and the photocell ("light dependent resistor") reported from the arduino
var state = "neutral"; //Should I show the good picture, bad picture, or nothing.
var goodImage, badImage; //the good and bad pics.
function setup() {
createCanvas(640, 480);
background(0x08, 0x16, 0x40);
//everything here is the same as class 6
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 NOTE: I like to chain open from list, so I can decide which port is the arduino
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
badImage = loadImage("bad.jpg");
goodImage = loadImage("good.jpg");
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
if (portList[i].indexOf(portTest) != -1) { //indexOf gives the starting position of a substring within a longer string, and returns -1 if not found.
portName = portList[i];
console.log("Will attempt to open Arduino on " + portName + " for serial connection");
}
}
serial.open(portName); //NOTE: here is my open command.
}
//This is very similar to class 6. Get a line, split on the commas, and save the separate results.
function serialEvent() {
//console.log(".");
var inData = serial.readLine();
if (inData.length>0) {
lastSerialString = inData;
var temp = inData.trim().split(',');
if (temp.length==4) { //This line effectively filters out the sensor-reporting strings from all the other things the Arduino is writing to the serial port for debugging. Unless those lines happened to have four commas, we're good.
s1 = temp[0];
s2 = temp[1];
s3 = temp[2];
ldr = temp[3];
}
}
}
function draw() {
background(200);
if (state=="bad") { //yikes!
image(badImage, 0,0);
} else if (state=="good") { //aww...
image(goodImage, -200, 0);
} else { //draw the primitive "gui"
textSize(32);
fill(0);
text(lastSerialString, 50, 50); //see what the last message from the Arduino was
//represent LEDs as circles colored in depending on their PWM value
stroke(0);
fill(rValue, 0, 0);
ellipse(100, 300, 50, 50);
fill(0, gValue, 0);
ellipse(200, 300, 50, 50);
//represent switches:
fill(0);
rect(50, 100, 30, 50+20*s1);
rect(150, 100, 30, 50+20*s2);
rect(250, 100, 30, 50+20*s3);
//make a pretty circle for the LDR:
stroke(255, 0, 255);
fill(255, 0, 255, 120);
ellipse(350, 100, 20+ldr/10, 20+ldr/10);
}
}
//If we click, calculate red and green PWM ammounts proportional to the mouse X and YH position.
function mousePressed() {
rValue = int(map(mouseX, 10, width-10, 0, 255));
gValue = int(map(mouseY, 10, height-10, 0, 255));
gValue = constrain(gValue, 0, 255);
rValue = constrain(rValue, 0, 255);
serial.write("L:" + rValue + "," + gValue + "\r\n"); //Note the \r\n. On the p5 side we need both to trigger readLine(). So although the Arduino might not need, I decided to use both so that everything is the same both ways.
//serial.write("!"); //originally, I just sent one character out when the mouse was pressed, to test my setup with the echo serial function on the arduino side.
}
function keyPressed() {
console.log(key);
//If we get a number key, generate tone commands with different pitches:
if (key>=0 && key<=9) {
serial.write("T:" + key*40 + "\r\n");
}
}
//Trigger the "good" and "bad" functions:
function keyTyped() {
if (key=='g') good();
if (key=='b') bad();
}
//Using the L: and T: commands we've established, trigger the Arduino to flash a green light and play a happy beep while the picture of the kitten is on the wcreen
function good() {
console.log("Doing good");
state="good";
serial.write("L:0,255\r\n");
serial.write("T:2000\r\n");
setTimeout(function() { //setTimeout makes it easy to defer calling some things to the future.
serial.write("T:2500\r\n");
}, 200);
setTimeout(function() {
serial.write("T:0\r\n");
serial.write("L:0,0\r\n");
state="neutral";
}, 1000);
}
//for the bad function, play a nasty sound with the red light while Zombie MJ creeps around.
function bad() {
console.log("Doing BAD");
state="bad";
serial.write("L:255,0\r\n");
serial.write("T:14\r\n");
setTimeout(function() {
serial.write("T:16\r\n");
}, 200);
setTimeout(function() {
serial.write("T:0\r\n");
serial.write("L:0,0\r\n");
state="neutral";
}, 1000);
}
//These were also functions I used during testing.
//function mousePressed() {
//serial.write("255,128\r\n");
// serial.write(13);
// serial.write(10);
//}
function serialError(err) { println('Something went wrong with the serial port. ' + err); }
function portClose() { println('The serial port closed.'); }
function serverConnected() { println('connected to server.'); }
function portOpen() { println('the serial port opened.'); }
//end