xxxxxxxxxx
102
var serial; //variable to hold an instance of the serial port library
var portName = 'COM6'; //port for Arduino
var sensorValue = 0; //for incoming serial
function preload() {
wall = loadImage('images/brick_wall.jpg');
splat = loadImage('images/target_splat.png');
}
function setup() {
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
createCanvas(600, 600);
}
// function draw() {
// background(wall);
// stroke(0);
// strokeWeight(2);
// fill(255);
// textSize(20);
// text(sensorValue, 20, 20);
// if (sensorValue <= 745 && sensorValue >= 350); {
// image(splat, 175,110);
// }
// if (sensorValue >= 0 && sensorValue <= 350);{
// fill(255,0);
// ellipse(0,0,1,1);
// }
// }
///////////////
function draw() {
background(wall);
stroke(0);
strokeWeight(2);
fill(255);
textSize(20);
text(sensorValue, 20, 20);
if (sensorValue <= 745 || sensorValue >= 350); {
image(splat, 175,110);
}
if (sensorValue >= 0 || sensorValue >= 350);{
fill(255,0);
ellipse(0,0,1,1);
}
}
// ellipse.X = map(sensorValue, 600, 900, 255, 0);
// ellipse.Y = map(sensorValue, 600, 900, 255, 0);
// noStroke();
// fill(0, 0, 255);
// 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 in the console:
println(i + " " + portList[i]);
}
}
function serialEvent() {
//read a string from the serial port:
var inString = serial.readLine();
//check to see that there's actually a string there:
if (inString.length > 0) {
//convert it to a number:
sensorValue = Number(inString);
}
}
function serverConnected() {
println('connected to server.');
}
function portOpen() {
println('the serial port opened.');
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}