Guest User

Untitled

a guest
May 27th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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. //global variable for sensor values
  11. var sensor1 = 0;
  12. var sensor2 = 0;
  13. var sensor3 = 0;
  14.  
  15. var serial; //variable to hold an instance of the serial port library
  16. var portName = '/dev/cu.usbmodem58'; //fill in with YOUR port
  17.  
  18. function setup() {
  19. createCanvas(windowWidth, windowHeight);
  20.  
  21. serial = new p5.SerialPort(); //a new instance of serial port library
  22.  
  23. //set up events for serial communication
  24. serial.on('connected', serverConnected);
  25. serial.on('open', portOpen);
  26. serial.on('data', serialEvent);
  27. serial.on('error', serialError);
  28. serial.on('close', portClose);
  29.  
  30. //open our serial port
  31. serial.open(portName);
  32.  
  33. //let's figure out what port we're on - useful for determining your port
  34. // serial.on('list', printList); //set a callback function for the serialport list event
  35. // serial.list(); //list the serial ports
  36.  
  37. }
  38.  
  39. function draw() {
  40. background('white');
  41.  
  42. RED = map(sensor1, 0, 1023, 0, 255);
  43. BLUE = map(sensor2, 0, 1023, 0, 255);
  44. GREEN = map(sensor3, 0, 1023, 0, 255);
  45.  
  46. fill(RED, 0, 0);
  47. rect(100, 150, 100, 100);
  48. text('Red value', 250, 200);
  49. text(int(RED), 250, 220);
  50.  
  51. fill(0, GREEN, 0);
  52. rect(100, 300, 100, 100);
  53. text('Green value', 250, 350);
  54. text(int(GREEN), 250, 370);
  55.  
  56. fill(0, 0, BLUE);
  57. rect(100, 450, 100, 100);
  58. text('Blue value', 250, 500);
  59. text(int(BLUE), 250, 520);
  60.  
  61. fill(RED, BLUE, GREEN);
  62. rect(400, 250, 200, 200);
  63.  
  64. }
  65.  
  66. function keyPressed(){
  67. serial.write(key);
  68. }
  69.  
  70. //all my callback functions are down here:
  71. //these are useful for giving feedback
  72.  
  73. function serverConnected(){
  74. console.log('connected to the server');
  75. }
  76.  
  77. function portOpen(){
  78. console.log('the serial port opened!');
  79. }
  80.  
  81. //THIS IS WHERE WE RECEIVE DATA!!!!!!
  82. //make sure you're reading data based on how you're sending from arduino
  83. function serialEvent(){
  84. //receive serial data here
  85. //read a string from the serial port
  86. var inString = serial.readLine();
  87.  
  88. //RECIEVING ONE VALUE
  89. //Only doing this if we have values
  90. // if(inString.length > 0){
  91. // sensor1 = Number(inString); // takes 'inString' and turns to a number
  92. // }
  93.  
  94. //RECIEVING MORE THAN ONE VALUE
  95. //save my string to an array, parse at comma
  96. if(inString.length > 0){
  97. var inputArray = split(inString, ',');
  98. sensor1 = Number(inputArray[0]); //convert to number
  99. sensor2 = Number(inputArray[1]);
  100. sensor3 = Number(inputArray[2]);
  101. }
  102. //console.log("Sensor 1 " + sensor1 + " : Sensor 2 " + sensor2);
  103. }
  104.  
  105. function serialError(err){
  106. console.log('something went wrong with the port. ' + err);
  107. }
  108.  
  109. function portClose(){
  110. console.log('the port was closed');
  111. }
  112.  
  113. // get the list of ports:
  114. function printList(portList) {
  115. // portList is an array of serial port names
  116. for (var i = 0; i < portList.length; i++) {
  117. // Display the list the console:
  118. print(i + " " + portList[i]);
  119. }
  120. }
Add Comment
Please, Sign In to add comment