Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This P5 sketch is a template for getting started with Serial Communication.
- The SerialEvent callback is where incoming data is received
- By Arielle Hein, adapted from ITP Phys Comp Serial Labs
- Edited March 13 2018
- */
- //global variable for sensor values
- var sensor1 = 0;
- var sensor2 = 0;
- var sensor3 = 0;
- var serial; //variable to hold an instance of the serial port library
- var portName = '/dev/cu.usbmodem58'; //fill in with YOUR port
- function setup() {
- createCanvas(windowWidth, windowHeight);
- serial = new p5.SerialPort(); //a new instance of serial port library
- //set up events for serial communication
- serial.on('connected', serverConnected);
- serial.on('open', portOpen);
- serial.on('data', serialEvent);
- serial.on('error', serialError);
- serial.on('close', portClose);
- //open our serial port
- serial.open(portName);
- //let's figure out what port we're on - useful for determining your port
- // serial.on('list', printList); //set a callback function for the serialport list event
- // serial.list(); //list the serial ports
- }
- function draw() {
- background('white');
- RED = map(sensor1, 0, 1023, 0, 255);
- BLUE = map(sensor2, 0, 1023, 0, 255);
- GREEN = map(sensor3, 0, 1023, 0, 255);
- fill(RED, 0, 0);
- rect(100, 150, 100, 100);
- text('Red value', 250, 200);
- text(int(RED), 250, 220);
- fill(0, GREEN, 0);
- rect(100, 300, 100, 100);
- text('Green value', 250, 350);
- text(int(GREEN), 250, 370);
- fill(0, 0, BLUE);
- rect(100, 450, 100, 100);
- text('Blue value', 250, 500);
- text(int(BLUE), 250, 520);
- fill(RED, BLUE, GREEN);
- rect(400, 250, 200, 200);
- }
- function keyPressed(){
- serial.write(key);
- }
- //all my callback functions are down here:
- //these are useful for giving feedback
- function serverConnected(){
- console.log('connected to the server');
- }
- function portOpen(){
- console.log('the serial port opened!');
- }
- //THIS IS WHERE WE RECEIVE DATA!!!!!!
- //make sure you're reading data based on how you're sending from arduino
- function serialEvent(){
- //receive serial data here
- //read a string from the serial port
- var inString = serial.readLine();
- //RECIEVING ONE VALUE
- //Only doing this if we have values
- // if(inString.length > 0){
- // sensor1 = Number(inString); // takes 'inString' and turns to a number
- // }
- //RECIEVING MORE THAN ONE VALUE
- //save my string to an array, parse at comma
- if(inString.length > 0){
- var inputArray = split(inString, ',');
- sensor1 = Number(inputArray[0]); //convert to number
- sensor2 = Number(inputArray[1]);
- sensor3 = Number(inputArray[2]);
- }
- //console.log("Sensor 1 " + sensor1 + " : Sensor 2 " + sensor2);
- }
- function serialError(err){
- console.log('something went wrong with the port. ' + err);
- }
- function portClose(){
- console.log('the port was 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:
- print(i + " " + portList[i]);
- }
- }
Add Comment
Please, Sign In to add comment