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
- */
- var serial; //variable to hold an instance of the serial port library
- var portName = '/dev/cu.usbserial-DN040NQJ'; //fill in with YOUR port
- //global var for sensor values
- var sensor1 = 1;
- var sensor2 = 0;
- output = 0;
- function setup() {
- createCanvas(400, 400);
- img1 = loadImage("lil-unify-1.png"); // Load the image
- img2 = loadImage("lil-unify-2.png"); // Load
- 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(38, 47, 128);
- image(img1, 55, 120);
- image(img2, 55, 200);
- output = abs(sensor1- 60 - sensor2); //distance between bottom of one image & top of next
- if (output != 0) {
- output = 1 / output;
- }
- printOutput(output);
- }
- //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 string from serial port
- var inString = serial.readLine(); //readLine listsns for carriage return
- /*
- //check if we received something
- //if we did, convert it to a number and save as a variable
- if (inString.length > 0) {
- sensor1 = Number(inString); //takes 'inString' and turns to a number
- }*/
- //Receiving more than one value
- if (inString.length > 0) {
- //save my string to an array
- //parse apart at the comma
- var inputArray = split(inString, ","); //thing we're splitting, delimiter
- if (inputArray.length >= 2) {
- sensor1 = Number(inputArray[0]);
- sensor2 = Number(inputArray[1]);
- console.log(inputArray);
- }
- }
- }
- function printOutput(output) {
- var outByte = int(map(output, 0, 1, 0, 255));
- serial.write(outByte);
- }
- 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