Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. /*
  2. This P5 sketch is a template for getting started with Serial Communication.
  3. The SerialEvent callback is where incoming data is received
  4.  
  5.  
  6. By Arielle Hein, adapted from ITP Phys Comp Serial Labs
  7. Edited March 13 2018
  8.  
  9. */
  10.  
  11. var serial; //variable to hold an instance of the serial port library
  12. var portName = '/dev/cu.usbserial-DN040NQJ'; //fill in with YOUR port
  13. //global var for sensor values
  14. var sensor1 = 1;
  15. var sensor2 = 0;
  16. output = 0;
  17.  
  18. function setup() {
  19. createCanvas(400, 400);
  20. img1 = loadImage("lil-unify-1.png"); // Load the image
  21. img2 = loadImage("lil-unify-2.png"); // Load
  22.  
  23. serial = new p5.SerialPort(); //a new instance of serial port library
  24.  
  25. //set up events for serial communication
  26. serial.on('connected', serverConnected);
  27. serial.on('open', portOpen);
  28. serial.on('data', serialEvent);
  29. serial.on('error', serialError);
  30. serial.on('close', portClose);
  31.  
  32. //open our serial port
  33. serial.open(portName);
  34.  
  35. //let's figure out what port we're on - useful for determining your port
  36. // serial.on('list', printList); //set a callback function for the serialport list event
  37. // serial.list(); //list the serial ports
  38. }
  39.  
  40. function draw() {
  41. background(38, 47, 128);
  42. image(img1, 55, 120);
  43. image(img2, 55, 200);
  44.  
  45. output = abs(sensor1- 60 - sensor2); //distance between bottom of one image & top of next
  46. if (output != 0) {
  47. output = 1 / output;
  48. }
  49. printOutput(output);
  50. }
  51.  
  52.  
  53. //all my callback functions are down here:
  54. //these are useful for giving feedback
  55.  
  56. function serverConnected() {
  57. console.log('connected to the server');
  58. }
  59.  
  60. function portOpen() {
  61. console.log('the serial port opened!');
  62. }
  63.  
  64. //THIS IS WHERE WE RECEIVE DATA!!!!!!
  65. //make sure you're reading data based on how you're sending from arduino
  66. function serialEvent() {
  67. //receive serial data here
  68.  
  69. //read string from serial port
  70. var inString = serial.readLine(); //readLine listsns for carriage return
  71. /*
  72. //check if we received something
  73. //if we did, convert it to a number and save as a variable
  74. if (inString.length > 0) {
  75. sensor1 = Number(inString); //takes 'inString' and turns to a number
  76. }*/
  77.  
  78. //Receiving more than one value
  79. if (inString.length > 0) {
  80. //save my string to an array
  81. //parse apart at the comma
  82. var inputArray = split(inString, ","); //thing we're splitting, delimiter
  83. if (inputArray.length >= 2) {
  84. sensor1 = Number(inputArray[0]);
  85. sensor2 = Number(inputArray[1]);
  86. console.log(inputArray);
  87. }
  88. }
  89. }
  90.  
  91. function printOutput(output) {
  92. var outByte = int(map(output, 0, 1, 0, 255));
  93. serial.write(outByte);
  94. }
  95.  
  96. function serialError(err) {
  97. console.log('something went wrong with the port. ' + err);
  98. }
  99.  
  100. function portClose() {
  101. console.log('the port was closed');
  102. }
  103.  
  104. // get the list of ports:
  105. function printList(portList) {
  106. // portList is an array of serial port names
  107. for (var i = 0; i < portList.length; i++) {
  108. // Display the list the console:
  109. print(i + " " + portList[i]);
  110. }
  111. }
Add Comment
Please, Sign In to add comment