-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5Arduino
82 lines (69 loc) · 2.44 KB
/
p5Arduino
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/tty.usbmodem14101'
let inData = [];
var serialString;
let x = 400;
let speed = 5;
function setup() {
createCanvas(800, 600);
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
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}
// 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:
console.log(i + " " + portList[i]);
}
}
function serialEvent(){
}
function draw(){
background(0);
serialString =serial.readLine();
if (serialString.length != null ) {
let splitString = split(serialString,";");
for(let i=0;i<3;i++){
inData[i]= Number(splitString[i]);
}
if (inData[2]==0){
fill(230,5,108);
}else{
fill(125,175,44);
}
if(inData[0]==0){
x += speed;
}
if(inData[1]==0){
x -= speed;
}
console.log(inData);
if(x<0){
x = 0
}
if(x>400){
x = 400
}
ellipse(x, height/2, 150, 150);
}
}