Guest User

Untitled

a guest
May 27th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. var serial; //variable to hold an instance of the serial port library
  2. var portName = '/dev/cu.usbmodem1411'; //fill in with YOUR port
  3.  
  4. var buttery;
  5. var x;
  6. var y;
  7. var r = 0;
  8. var g = 0;
  9. var b = 0;
  10. var c;
  11. var sensor1 = 0;
  12.  
  13.  
  14.  
  15. function setup() {
  16. createCanvas(600, 400);
  17. x = width;
  18. y = height;
  19. serial = new p5.SerialPort();
  20. serial.on('connected', serverConnected);
  21. serial.on('open', portOpen);
  22. serial.on('data', serialEvent);
  23. serial.on('error', serialError);
  24. serial.on('close', portClose);
  25. serial.open(portName);
  26. }
  27.  
  28. function draw() {
  29. background(r, g, b);
  30. stroke(0, 0, 0);
  31.  
  32. fill("red");
  33. rect((width / 5), height - 100, (width / 5), -r);
  34. fill("green");
  35. rect(((width / 5) * 2), height - 100, (width / 5), -g);
  36. fill("blue");
  37. rect((width / 5) * 3, height - 100, (width / 5), -b);
  38. stroke("white");
  39. fill("black");
  40. rect((width / 5), height - 100, (width / 5), 50);
  41. fill("black");
  42. rect((width / 5) * 2, height - 100, (width / 5), 50);
  43. fill("black");
  44. rect((width / 5) * 3, height - 100, (width / 5), 50);
  45. textSize(32);
  46. fill("white");
  47. text("Color Analyzer",width/3-10,30);
  48. text(r, width / 5 + 20, height - 65);
  49. text(g, ((width / 5)*2)+35 , height - 65);
  50. text(b, ((width / 5)*3)+30 , height - 65);
  51.  
  52. }
  53. //on mouse event send a byte (0-255) to arduino
  54. function mousePressed(){
  55.  
  56. //we want to send INTERGER
  57.  
  58. var outfo = int(map(mouseX,0,width,0,255));
  59. serial.write(outfo);
  60. print("Sending out:" + outfo);
  61.  
  62.  
  63. }
  64.  
  65. function keyPressed(){
  66. // serial.write(150);
  67. if(key === "R"){
  68. r=255;
  69. serial.write(150); //send the key
  70. print("pressed R");
  71.  
  72. }else if(key ==="G"){
  73. g=255;
  74. serial.write(180);
  75. print("pressed G");
  76.  
  77. }else if(key ==="B"){
  78. b=255;
  79. serial.write(190);
  80. print("pressed B");
  81.  
  82. }else if(key ==="L"){
  83. serial.write(200);
  84. print("pressed RAINBOW");
  85. var k=0;
  86. r=150;
  87. g=100;
  88. b=50;
  89. while(k<100){
  90. r=int(random(0,255));
  91. g=int(random(0,255));
  92. b=int(random(0,255));
  93. k++;
  94. }
  95.  
  96. }
  97.  
  98. }
  99.  
  100. function serverConnected() {
  101. console.log('connected to the server');
  102. }
  103.  
  104. function portOpen() {
  105. console.log('the serial port opened!');
  106. }
  107.  
  108. //THIS IS WHERE WE RECEIVE DATA!!!!!!
  109. //make sure you're reading data based on how you're sending from arduino
  110. function serialEvent() {
  111. //receive serial data here
  112. //read a string from the serial port
  113. var inString = serial.readLine(); //read line listens for carriage return
  114.  
  115. //check if we recieve
  116. /* ONE VALUE
  117. if(inString.length > 0){
  118. sensor1 = Number(inString);
  119. } */
  120. //MORE THAN ONE VALUE
  121. //save string to array
  122. //parse by commas
  123. if (inString.length > 0) {
  124. var inputArray = split(inString, ",");
  125. r = (inputArray[0]);
  126. g = (inputArray[1]);
  127. b = (inputArray[2]);
  128. c = color(inputArray[0], inputArray[1], inputArray[2]);
  129.  
  130. }
  131.  
  132.  
  133. //if so, convert and save
  134. //console.log(sensor1);
  135.  
  136.  
  137.  
  138.  
  139. }
  140.  
  141. function serialError(err) {
  142. console.log('something went wrong with the port. ' + err);
  143. }
  144.  
  145. function portClose() {
  146. console.log('the port was closed');
  147. }
  148.  
  149. // get the list of ports:
  150. function printList(portList) {
  151. // portList is an array of serial port names
  152. for (var i = 0; i < portList.length; i++) {
  153. // Display the list the console:
  154. print(i + " " + portList[i]);
  155. }
  156. }
Add Comment
Please, Sign In to add comment