Lab: Two-Way (Duplex) Serial Communication Using An Arduino and P5.js
For this lab, I was successful in sending multiple serial data using punctuation and in controlling the flow of data through call-and-response/handshaking.
However, I had some issues with the hardware set-up that were reflected in my p5.js sketch. In the video below you can see the white dot is regularly flickering. In this setup, a push-button was responsible for the visibility of the white circle. It should disappear when the button is pressed (sensorValue = 1) and appear when the button is not pressed (sensorValue=0). However, the button when unpressed was constantly flickering between 1 and 0. It would be all 1s when pressed but never all 0s. I suspect this had something to do with the strength of the resistor because when I touched the resistor with my finger the number would stabilize. I did this lab while traveling so unfortunately I did not have a stronger resistor to test out this theory before class.
Build an Application of Your Own
For this portion of the lab, I decided to try using an IR distance sensor to control a candle lighting sketch (original code found here: https://editor.p5js.org/mathura/sketches/ByrHNu53b). This idea came from my concept for Project 2. I want to build a responsive altar that 1) beckons people to come engage with it and 2) responds to how people engage with it. The idea of having a row of lights light up as you approach something is a very clear signal, very intriguing, and almost ominous.
I used an Ultrasonic Range Finder that can detect an object between 6 and 254 inches away to control the flame of 4 candles drawn in p5.js. (Link to p5.js sketch: https://editor.p5js.org/mayae.willi/sketches/EB34UFqJA) When an object is 25 inches or closer the flame is meant to appear.
My code is based on an example program written by the manufacturer: https://www.maxbotix.com/Arduino-Ultrasonic-Sensors-085/.
/* Candle Controller, p5.js Used to Read MaxSonar MB1010 Written for Arduino Nano Reading Pin3 output */ const int anPin = 0; long anVolt, mm, inches; void setup() { Serial.begin(9600); } void loop() { read_sensor(); approach(); delay(100); } void read_sensor() { anVolt = analogRead(anPin); mm = anVolt * 5; //Takes bit count and converts it to mm inches = mm / 25.4; //Takes mm and converts it to inches Serial.write(inches); delay(1); } void approach() { if (inches <= 25) { Serial.println("candles on"); } else { Serial.print("S1"); Serial.print("="); Serial.print(mm); Serial.print(" "); Serial.println(inches); } }
When I ran the two sketches the flames did light when a sensor value lower than 25 was sent to the p5 sketch. I know this because I displayed the sensor value and could observe the flame appearing after the port the Arduino is connected to was selected. However, the sensor value only displays 10 no matter how close or far I place the sensor in relation to an object. This is not reflective of what I saw in the serial monitor when I was testing the sensor or what I get when I constructed a similar circuit using real LEDs. Iām not sure where the disconnect is between Arduino and p5 is at this point.