Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Serial commands to send data to Arduino
  2. var serial; // variable to hold an instance of the serialport library
  3. var portName = '/dev/cu.usbmodem1421'; // fill in your serial port name here
  4. var options = {
  5. baudrate: 9600
  6. }; // change the data rate to whatever you wish
  7. var inData; // for incoming serial data
  8. // var xPos = 0; // x position of the graph // UNNECESSARY
  9.  
  10.  
  11. function setup() {
  12. createCanvas(400, 400);
  13. serialCommands();
  14. // connectKinect();
  15. }
  16.  
  17. var outByte = '';
  18.  
  19. function draw() {
  20. // background(120)
  21. // fill(255)
  22. // ellipse(xpos, ypos, 20, 20)
  23. // movePos()
  24. // drawBird()
  25. // getSpeed()
  26. // outByte = int(ypos) + ',' + int(xpos) + ',' + int(avgSpeed)
  27. // outByte = 25;
  28. // console.log(outByte);
  29. // serial.write(outByte)
  30.  
  31. }
  32.  
  33.  
  34. function serialCommands() {
  35. serial = new p5.SerialPort(); // make a new instance of the serialport library
  36. serial.open(portName); // open a serial port
  37. serial.on('connected', serverConnected); // callback for connecting to the server
  38. serial.on('list', printList); // set a callback function for the serialport list event
  39. //serial.on('data', serialEvent); // callback for when new data arrives
  40. serial.on('error', serialError); // callback for errors
  41. serial.on('open', portOpen); // callback for the port opening
  42. //serial.on('close', portClose); // callback for the port closing
  43. //serial.open(portName);
  44. //serial.list(); // list the serial ports
  45.  
  46. }
  47.  
  48.  
  49. function serverConnected() {
  50. console.log('connected to server.');
  51. }
  52.  
  53. function portOpen() {
  54. console.log('the serial port opened.')
  55. }
  56.  
  57. function serialEvent() {
  58. // serial.write(outByte + '\n')
  59.  
  60. var inByte = serial.read();
  61. // store it in a global variable:
  62. inData = inByte;
  63. // write data to serial port
  64. // serial.print(outByte)
  65.  
  66.  
  67. // // read a string from the serial port:
  68. // var inString = serial.readLine();
  69. // console.log(inString)
  70. // // check to see that there's actually a string there:
  71. // if (inString.length > 0 ) {
  72. // // convert it to a number:
  73. // inData = int(inString);
  74. // }
  75. }
  76.  
  77. function serialError(err) {
  78. console.log('Something went wrong with the serial port. ' + err);
  79. }
  80.  
  81. function portClose() {
  82. console.log('The serial port closed.');
  83. }
  84.  
  85.  
  86. function keyPressed() {
  87. console.log("press");
  88. serial.write(5);
  89. }
  90.  
  91. // get the list of ports:
  92. function printList(portList) {
  93. // portList is an array of serial port names
  94. for (var i = 0; i < portList.length; i++) {
  95. // Display the list the console:
  96. console.log(i + " " + portList[i]);
  97. }
  98. }
  99.  
  100. function connectKinect() {
  101. var address = {
  102. host: '172.16.216.97',
  103. port: 9001,
  104. path: '/'
  105. };
  106. kinectron = new Kinectron('kinectron', address);
  107. kinectron.makeConnection();
  108. kinectron.startTrackedBodies(trackBody);
  109. }
Add Comment
Please, Sign In to add comment