Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. int b = 1;
  2. int numDataPoints = 50000;
  3. int dataIndex = 1;
  4. String[] dataStrings = new String[numDataPoints]; // Save up to 10k values
  5.  
  6. // loads a library needed to establish a connection to a serial device
  7. // in our case, the serial device is a Circuit Playground
  8. import processing.serial.*;
  9.  
  10. // create a new array that will hold on to the sensor values
  11. // from the Circuit Playground
  12. float[] portValues = new float[8];
  13.  
  14. // create a new serial connection
  15. Serial myPort;
  16. String inString;
  17.  
  18. void setup (){
  19. size (300,300);
  20. background(255,255,255);
  21. myPort = new Serial(this, "/dev/cu.usbmodem1421", 9600);
  22. for(int i = 0; i<8; i++)
  23. {
  24. portValues[i] = 0;
  25. }
  26. dataStrings[0] = "x,y,z,leftButton,rightButton,lightSensor,soundSensor,tempSensor";
  27. }
  28.  
  29. // convert float data to string data in order to save to a file
  30. String buildDataString(float[] v) {
  31. String result = "";
  32. for(int i = 0; i<v.length-1; i++) {
  33. result += str(v[i]) + ",";
  34. }
  35. result += str(v[7]);
  36. return result;
  37. }
  38.  
  39.  
  40.  
  41. void draw()
  42. {
  43. // changes background color
  44. if(portValues[4] == 1) { // if the left button is pressed...
  45. background (random(255),random(255),random(255));
  46. } else {
  47. background(255,255,255);// if no buttons are pressed
  48. }
  49. // Mondrian Code
  50. line(0,100,167,100);
  51. line(57,0,57,100);
  52. line(167,0,167,190);
  53. line(0,190,250,190);
  54. line(130,190,130,250);
  55. line(250,143,300,143);
  56. line(72,250,72,300);
  57. line(250,250,250,300);
  58. line(142,175,175,175);
  59. line(67,175,67,190);
  60. line(67,153,0,153);
  61. line(285,0,285,143);
  62. line(142,0,142,250);
  63. line(230,230,0,230);
  64. line(180,250,180,190);
  65. line(130,200,130,175);
  66. line(250,190,300,190);
  67. line(67,33,142,33);
  68. line(117,58,117,175);
  69. line(117,58,67,58);
  70. line(117,33,117,0);
  71. line(142,33,167,33);
  72. line(142,58,167,58);
  73. line(285,10,250,10);
  74. line(285,75,250,75);
  75. line(230,230,230,165);
  76.  
  77. fill(255,0,0);
  78. rect(0,0,67,100);
  79.  
  80.  
  81. fill(0,0,255);
  82. rect(67,100,75,75);
  83.  
  84. fill(0,0,255);
  85. rect(0,190,130,60);
  86.  
  87. fill(0,255,0);
  88. rect(167,0,83,190);
  89.  
  90. fill(255,0,0);
  91. rect(230,230,70,70);
  92.  
  93. fill(0,255,0);
  94. rect(130,250,50,50);
  95.  
  96. fill(255,255,0);
  97. rect(142,33,-25,25);
  98.  
  99. fill(255,255,0);
  100. rect(300,10,-15,65);
  101.  
  102. // Recognizing and stores data from arduino.
  103. if (inString != null) {
  104. portValues = processSensorValues(inString); // get data
  105. // manage data points
  106. dataIndex++;
  107. if(dataIndex > numDataPoints - 1) {
  108. dataIndex = 1;
  109. }
  110. dataStrings[dataIndex] = buildDataString(portValues);
  111. saveStrings("values.csv",dataStrings);
  112. text(dataIndex,width-80,40);
  113. }
  114.  
  115.  
  116.  
  117. // Change the color of recangles based on the button pressed.
  118. if(portValues[3] == 1) { // if the right button is pressed...
  119.  
  120. fill (0,0,255); //rearange color patter
  121. fill(random(255),random(255),random(255));
  122. rect(0,0,67,100);
  123.  
  124. fill(random(255),random(255),random(255));
  125. rect(67,100,75,75);
  126.  
  127. fill(random(255),random(255),random(255));
  128. rect(0,190,130,60);
  129.  
  130. fill(random(255),random(255),random(255));
  131. rect(167,0,83,190);
  132.  
  133. fill(random(255),random(255),random(255));
  134. rect(230,230,70,70);
  135.  
  136. fill(random(255),random(255),random(255));
  137. rect(130,250,50,50);
  138.  
  139. fill(random(255),random(255),random(255));
  140. rect(142,33,-25,25);
  141.  
  142. fill(random(255),random(255),random(255));
  143. rect(300,10,-15,65);
  144. }
  145. }
  146.  
  147. float[] processSensorValues(String valString) {
  148.  
  149. String[] temp = {"0", "0", "0", "0", "0", "0", "0", "0"};
  150.  
  151. temp = split(valString,"\t");
  152.  
  153. if(temp == null) {
  154. for(int i = 0; i<8; i++) {
  155. temp[i] = "0";
  156. }
  157. }
  158.  
  159. float[] vals = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  160. for(int i = 0; i<8; i++)
  161. {
  162. if(temp != null)
  163. {
  164. vals[i] = float(temp[i]);
  165. }
  166.  
  167. else
  168. {
  169. vals[i] = 0;
  170. }
  171.  
  172. }
  173. return vals;
  174. }
  175.  
  176. // read new data from the Circuit Playground
  177. void serialEvent(Serial p) {
  178. inString = myPort.readStringUntil(10);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement