Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. var serial;
  2. var portName = '/dev/cu.usbmodem14101';
  3.  
  4. var sensor1 = 0;
  5. var sensor2 = 0;
  6.  
  7. function setup() {
  8. createCanvas(100, 600);
  9.  
  10. serial = new p5.SerialPort();
  11.  
  12. serial.on('connected', serverConnected);
  13. serial.on('open', portOpen);
  14. serial.on('error', serialError);
  15. serial.on('close', portClose);
  16. serial.open(portName);
  17. }
  18.  
  19. function draw() {
  20. background('dodgerblue');
  21. noStroke();
  22. ellipse(width/2, height/2, sensor1, sensor2);
  23. }
  24.  
  25. function mouseDragged(){
  26. var outputVal = Math.floor(map(mouseY, 0, height, 0, 255));
  27. print(outputVal);
  28. serial.write(outputVal);
  29. }
  30.  
  31. function serverConnected(){
  32. console.log('connected to the server');
  33. }
  34.  
  35. function portOpen(){
  36. console.log('the serial port opened!');
  37. }
  38.  
  39. function serialError(err){
  40. console.log('something went wrong with the port. ' + err);
  41. }
  42.  
  43. function portClose(){
  44. console.log('the port was closed');
  45. }
  46.  
  47. function printList(portList) {
  48. for (var i = 0; i < portList.length; i++) {
  49. print(i + " " + portList[i]);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement