"Stop calling, stop calling, I don't wanna talk anymore." — My Serial Monitor
March 01, 2021
This assignment had us configure a two-way connection between our Microcontroller and a web page. This was done using the serial monitor and some fancy communication with p5.serialControl. p5.js allowed us to read and manipulate that information in Javascript as well as send some data back.
I really wanted to work with the web for my final project, so I wanted this assignment to be the groundwork for what I will be doing as the quarter wraps up. My final projects centers around configuring color patterns for an RGB LED strip (beat that, TikTok Lights). I wanted users to be able to click on a color from a page and read that into RGB values to use in the microcontroller. I also wanted to use the joystick as a pseudo-cursor, since the mousepad is lame and this could be more fun.
For this assignment, I wanted to solve the basic two-way communication betwee nthe Arduino and the web, get the joystick pseudo-cursor working, be able to extract RGB values froma specific pixel, send the pixel back to the Arduino, and light up an RGB LED. Simple!
Arduino Code for the Setup
The code is split up into two parts this time: Arduino code and JS code. The Arduino code is rather quite simple; the first half of the code is sending joystick and switch code to the serial monitor, and the other half is reading input values from the serial and mappign them to an RGB LED.
Javascript Code for the Setup
The Javascript code is a lot more involved than the Arduino side. Here, the serial port has to be configured to talk to the web page. Using p5.js, we can draw with canvas elements and create and manipulate elements using JS only, no HTML. The majority of the incoming serial positions a cursor onto the page using the coordinates of the joystick.
One feature of the code is to check if the incoming values from the serial monitor are joystick coordinates or the switch turning on. If the switch is on, then it will send the RGB values of pixel to the serial from the pseudo-cursor's position.
Getting the Circuit Up and Running
Usually, I like to do the schematic first. However, the three main components of this program (the joystick, the switch, and the RGB LED) are all straightforward, and I have done each of the components separately outside of this assignment. Hooking them up was no issue; the 10KΩ resistor on the switch is to limit and stabilize the current so we don't overload the Arduino. The 100Ω resistor is for blue, and 220Ω resistor is for red and green, as these values are the closest to their respective ideal resistances after voltage drop.
I originally didn't have the switch separate from the joystick. For some reason, my setup was not reading in any signal from the joystick build-in switch. I messed around with different pins, different code, and different wires (ended up breaking one), but to no avail. After getting the independent button to work, I concluded that my joystick switch was probably busted. Although it made me sad that my interaction wasn't confined in one neat little component, I was glad to finally solve that issue.
Drawing the Schematic
Kind of counterintuitive to make the schematic after the breadboard operation, but it was important to me to break up the circuit in chunks. Making sure the serial communication was working before hooking up the rest seemed logical to me, so I put off the circuit schematic until the very end. I also knew that I wasn't working with any high voltages, so I was hot-wiring my circuit as I went along. (I know, shame on me. However, sometimes I can't help it!)
Will the Web Answer My Call?
This one wasn't as much of a grand reveal as previous assignments, mostly because I had to make sure that the communication was working very early on before I started implementing more complicated components. However, seeing it all come together was still extremely satisfying:
I also tried capturing my screen during an test run, but the recording software doesn't seem to like capturing canvas elements updating every frame, so it's actually more choppy than the previous operation GIF.
This assignment felt really good to complete. There was a lot of hassle in debugging the serial communication: Most importantly, I had a really hard time troubleshooting, as printing messages to the serial monitor would for some reason mess up my incoming data from the web. I know that the two communicate over the same baud signal, but I always thought that the input and output channels were completely independent. Either way, I'm really glad to complete a major component of my potential final project!