Last active
February 17, 2017 01:01
-
-
Save mjvo/f1f0a3fdfc16a3f9bbda4bba35e6be5b to your computer and use it in GitHub Desktop.
Serial Output from P5.js - Arduino and p5js code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino code for Redboard | |
const int ledPin = 5; // the pin that the LED is attached to | |
int incomingByte; // a variable to read incoming serial data into | |
void setup() { | |
// initialize serial communication: | |
Serial.begin(9600); // note baudrate set at 9600. Other speeds available/must be matched in P5.js options | |
// initialize the LED pin as an output: | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() { | |
// see if there's incoming serial data: | |
if (Serial.available() > 0) { | |
// read the oldest byte in the serial buffer: | |
incomingByte = Serial.read(); | |
// if it's a capital H (ASCII 72), turn on the LED: | |
if (incomingByte == 'H') { | |
analogWrite(ledPin, 255); | |
} | |
// if it's an L (ASCII 76) turn off the LED: | |
if (incomingByte == 'L') { | |
analogWrite(ledPin, 0); | |
} | |
else { | |
analogWrite(ledPin,incomingByte); | |
} | |
Serial.write(incomingByte); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Be sure to choose File > Import Library > p5.serialport.js from the P5 IDE main menu to load serial library | |
// Terminal command to install p5.serialserver: npm install p5.serialserver | |
// Terminal command to start server: node ~/node_modules/p5.serialserver/startserver.js | |
var serial; // variable to hold an instance of the serialport library | |
var options = { baudrate: 9600}; // set baudrate to 9600; must match Arduino baudrate | |
var portName = '/dev/cu.usbserial-DA00WVK6'; // fill in your serial port name here | |
var inData; // for incoming serial data | |
var outByte = 0; // for outgoing data | |
var slider; | |
function setup() { | |
createCanvas(400, 300); // make the canvas | |
serial = new p5.SerialPort(); // make a new instance of the serialport library | |
serial.on('data', serialEvent); // callback for when new data arrives | |
serial.on('error', serialError); // callback for errors | |
serial.open(portName, options); // open a serial port @ 9600 | |
slider = createSlider(0, 255, 100); | |
slider.position(30, 50); | |
slider.changed(transmitSlider); | |
} | |
function draw() { | |
// black background, white text: | |
background(0); | |
fill(255); | |
// display the incoming serial data as a string: | |
text("incoming value: " + inData, 30, 30); | |
outByte = slider.value(); | |
} | |
function transmitSlider(){ | |
serial.write(outByte); | |
} | |
function keyPressed() { | |
if (key ==='H' || key ==='L') { // if the user presses H or L | |
serial.write(key); // send it out the serial port | |
text("incoming value: " + key, 30, 30); | |
} | |
} | |
function serialEvent() { | |
// read a byte from the serial port: | |
var inByte = serial.read(); | |
// store it in a global variable: | |
inData = inByte; | |
} | |
function serialError(err) { | |
println('Something went wrong with the serial port. ' + err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment