Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. // This example shows how to read an analog
  2. // value from an serial port
  3. // and draw circle with size mapped to that value
  4.  
  5. // Declare a "SerialPort" object
  6. var serial;
  7.  
  8. // Variable for size
  9. var switchState = 0;
  10.  
  11. //zoe and renata var
  12. var words1 = ["banana", "coffee", "computer"];
  13. var words2 = ["pb+J", "foosball", "plants"];
  14. var words3= ["moon", "lockers", "Midori"];
  15.  
  16. var verb = ["has", "wants", "eats"]
  17. var prep = ["with", "on", "under"]
  18.  
  19. var index = 0;
  20.  
  21. function preload(){
  22. //images1
  23. banana = loadImage("images/banana.jpg");
  24. coffee = loadImage("images/coffee.jpg");
  25. computer = loadImage("images/computer.jpg");
  26.  
  27. //images2
  28. pbj = loadImage("images/pbj.jpg")
  29. foosball = loadImage("images/foosball.jpg");
  30. plant = loadImage("images/plant.jpg");
  31.  
  32. //images3
  33. moon = loadImage("images/moon.jpg")
  34. midori = loadImage("images/midori.jpg");
  35. locker = loadImage("images/locker.jpg");
  36.  
  37. }
  38.  
  39. function setup() {
  40. createCanvas(700,700);
  41. fill(0);
  42. textSize(32);
  43.  
  44. // Instantiate our SerialPort object
  45. serial = new p5.SerialPort();
  46.  
  47. // Assuming our Arduino is connected, let's open the connection to it
  48. // Change this to the name of your arduino's serial port
  49. serial.open("/dev/cu.usbmodem1421");
  50.  
  51. // if you need to see the list
  52. // serial.onList(gotList);
  53.  
  54. // This is a new concept!
  55. // Whenever there is new data, the "gotData" function happens.
  56. // This is called a *CALLBACK*
  57. serial.onData(gotData);
  58. }
  59.  
  60. // This happens when there is data
  61. function gotData() {
  62. // Read the data as text (a string)!
  63. var data = serial.readLine();
  64. // Check to make sure something really came in
  65. if (data.length > 0); {
  66. // Get the 0 or 1
  67. switchState = Number(data);
  68. }
  69. }
  70.  
  71. function draw() {
  72.  
  73. // Do something based on whether
  74. // switch is on or off!
  75. if (switchState === 0) {
  76.  
  77. background(0);
  78. fill(255);
  79. textAlign(CENTER);
  80. textSize(24);
  81. text('PRESS THE BUTTON', width / 2, height -12);
  82. } else if (switchState === 1) {
  83. index = index + 1;
  84. if (index == words1.length) {
  85. index = 0;
  86. }
  87.  
  88. if (index == images1.length) {
  89. index = 0;
  90. }
  91.  
  92.  
  93. //verb
  94. index = index + 1;
  95. if (index == verb.length) {
  96. index = 0;
  97. }
  98.  
  99.  
  100. //set2
  101. if (index == words2.length) {
  102. index = 0;
  103. }
  104.  
  105. if (index == images2.length) {
  106. index = 0;
  107. }
  108.  
  109. //prep
  110. index = index + 1;
  111. if (index == prep.length) {
  112. index = 0;
  113. }
  114.  
  115. //set3
  116. if (index == words3.length) {
  117. index = 0;
  118. }
  119.  
  120. if (index == images3.length) {
  121. index = 0;
  122. }
  123.  
  124. background(255);
  125. textAlign(CENTER);
  126. text(words1[index], 100, 400);
  127. text(verb[index], 200, 400);
  128. text(words2[index], 300, 400);
  129. text(prep[index], 400, 400);
  130. text(words3[index], 500, 400);
  131.  
  132. var images1 = [banana, coffee, computer];
  133. image(images1[index], 100, 100, 100, 100);
  134. //image(image,[x=0],[y=0],[width],[height])
  135.  
  136. var images2 = [pbj, foosball, plant];
  137. image(images2[index], 300, 100, 100, 100);
  138.  
  139. var images3 = [moon, locker, midori];
  140. image(images3[index], 500, 100, 100, 100);
  141. }
  142. }
  143.  
  144.  
  145. /*
  146. // Got the list of ports
  147. function gotList(thelist) {
  148. println("List of Serial Ports:");
  149. // theList is an array of their names
  150. for (var i = 0; i < thelist.length; i++) {
  151. // Display in the console
  152. println(i + " " + thelist[i]);
  153. }
  154. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement