forked from p5-serial/p5.serialserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
executable file
·56 lines (46 loc) · 1.29 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
2 serial port test. Put the Arduino analogReadSerial sketch
on two Arduinos and connect them to your computer using 2
USB ports. Note the port names and change firstPort and
secondPort below to match. Then run this sketch. You should see
data coming in from both ports independently.
*/
var serial1 = new p5.SerialPort();
var serial2 = new p5.SerialPort();
var firstPort = '/dev/cu.usbmodem14111';
var secondPort = '/dev/cu.usbmodem141431';
var input1 = '';
var input2 = '';
function setup() {
createCanvas(400, 300);
serial1.on('data', serialEvent);
serial1.on('error', serialError);
serial2.on('data', serial2Event);
serial2.on('error', serial2Error);
serial1.open(firstPort);
serial2.open(secondPort);
}
function draw() {
background(0x23, 0x76, 0xF3);
fill(255);
text("data from serial port 1:" + input1, 30, 30);
text("data from serial port 2: " + input2, 30, 90);
}
function serialEvent(){
data = serial1.readStringUntil('\r\n');
if (data.length > 0){
input1 = data;
}
}
function serialError(err) {
println("error with serial port 1: " + err);
}
function serial2Event() {
var data = serial2.readStringUntil('\r\n');
if (data.length > 0){
input2 = data;
}
}
function serial2Error(err) {
println("error with serial port 2: " + err);
}