{"id":2322,"date":"2022-03-01T19:51:18","date_gmt":"2022-03-02T00:51:18","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2322"},"modified":"2022-03-01T19:51:18","modified_gmt":"2022-03-02T00:51:18","slug":"crit1-development-experiment-with-button-and-p5js","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2322","title":{"rendered":"Crit1 Development: Experiment with Button and p5js"},"content":{"rendered":"<p>I ran into challenges with the previous DHT22 sensor to p5js assignment. I performed another test run using input from a button. With this, I was able to connect the p5.js web editor to the arduino, and have an image on the screen of the web editor change when the button input changed.<\/p>\n<p>Arduino sketch below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const int buttonPin = 2;\r\nint buttonValue = 0; \r\n\r\nvoid setup() {\r\n  \/\/ put your setup code here, to run once:\r\n  pinMode(buttonPin, INPUT);\r\n  Serial.begin(9600);\r\n\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ put your main code here, to run repeatedly:\r\n  buttonValue = digitalRead(buttonPin);\r\n  Serial.println(buttonValue);\r\n  delay(10);\r\n\r\n}<\/pre>\n<p>p5js sketch:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">let serial; \r\nlet newData = \"waiting for data\";\r\n\r\nfunction setup() {\r\n  createCanvas(windowWidth, windowHeight);\r\n  serial = new p5.SerialPort();\r\n  serial.list();\r\n  serial.open('COM5');\r\n  serial.on('connected', serverConnected);\r\n  serial.on('list',gotList);\r\n  serial.on('data',gotData);\r\n  serial.on('error',gotError);\r\n  serial.on('open',gotOpen);\r\n  \r\n}\r\n\r\nfunction serverConnected() {\r\n  print(\"Connected to Server\");\r\n}\r\n\r\nfunction gotList(thelist) {\r\n  print(\"List of Serial Ports:\");\r\n  \r\n  for (let i=0; i &lt; thelist.length; i++) {\r\n    print(i + \" \" + thelist[i]);\r\n    \r\n  }\r\n}\r\n\r\nfunction gotOpen() {\r\n  print(\"Serial Port is Open\");\r\n}\r\n\r\nfunction gotClose() {\r\n  print(\"Serial Port is Closed\");\r\n  newData = \"Serial Port is Closed\";\r\n}\r\n\r\nfunction gotError(theerror) {\r\n  print(theerror);\r\n}\r\n\r\nfunction gotData() {\r\n  let currentString = serial.readLine();\r\n  trim(currentString);\r\n  if (!currentString) return;\r\n  console.log(currentString);\r\n  newData = currentString;\r\n}\r\n\r\nfunction draw() {\r\n  background(245,245,245);\r\n  fill(50,50,200);\r\n  text(newData,10,10);\r\n  \r\n  \r\n  if (newData == 0) {\r\n    rectMode(CENTER);\r\n    rect(width\/2,height\/2,100,100);\r\n  } else {\r\n    ellipse(width\/2,height\/2,200,200);\r\n    \r\n  }\r\n  \r\n}<\/pre>\n<p>Next, for the Crit1 project, I want to be able to record or represent the actions of the plant turntable with p5js.<\/p>\n<p>Resources and Demos:<\/p>\n<p><iframe loading=\"lazy\" title=\"p5.js and Arduino serial communication - Send a digital sensor to a p5.js sketch\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/feL_-clJQMs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into challenges with the previous DHT22 sensor to p5js assignment. I performed another test run using input from a button. With this, I was able to connect the p5.js web editor to the arduino, and have an image on the screen of the web editor change when the button input changed. Arduino sketch &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2322\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Crit1 Development: Experiment with Button and p5js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2322"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2322"}],"version-history":[{"count":6,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2322\/revisions"}],"predecessor-version":[{"id":2341,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2322\/revisions\/2341"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}