xxxxxxxxxx
109
/*
Minimal p5.js code to communicate with Arduino
using handshaking. Arduino code is below
*/
let serial; // variable to hold an instance of the serialport library
let portName = "/dev/ttyACM0";
let outgoingData = 0;
function setup() {
createCanvas(200, 50);
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
background(200);
}
function draw() {
if (mouseIsPressed) {
if (mouseX <= width / 2)
{
outgoingData = 0;
} else {
outgoingData = 1;
}
}
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (let i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
}
function serverConnected() {
print("connected to server.");
}
function portOpen() {
print("the serial port opened.");
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
let incomingData = serial.readLine();
//check to see that there's actually a string there:
if (incomingData.length > 0) {
print("incoming: " + incomingData + " outgoing: " + outgoingData);
}
// this implements the handshaking: Only
// send data if we received something
serial.write(outgoingData);
}
function serialError(err) {
print("Something went wrong with the serial port. " + err);
}
function portClose() {
print("The serial port closed.");
}
/***********************************************
//
// Arduino code for minimal communication
// with a P5.js sketch using handshaking
//
const int LED_PIN = 13;
const int SWITCH_PIN = 7;
void setup() {
Serial.begin(9600);
pinMode(SWITCH_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// wait until the other side responds
while (Serial.available() <= 0) {
Serial.println("1"); // send a starting message
delay(300);
}
}
void loop() {
while (Serial.available() > 0) {
int inByte = Serial.read();
digitalWrite(LED_PIN, inByte);
int switchValue = digitalRead(SWITCH_PIN);
Serial.println(switchValue);
}
}
***************************************************/