For this week's assignment, we were required to write code to interface with an input or output device from prior weeks. Due to limited time, I wanted to write up an extremely simple serial interface to a servo I had connected to my Xiao board from a prior week.
While I was familiar with how serial worked with Arduino, I was still surprised by the amount of thought that needed to be put into making a robust and bug-free keyboard interface to my microcontroller. Not only did I need to pay attention to connections, baud rates, permissions, etc. that made it challenging to write to my Xiao via my desktop, but I also needed to consider things like when to read in data, ensuring it wouldn't be blocking, and how to parse characters that signified the end of the message.
Fortunately, there was a great resource for this week's assignment that helped me get started, which can be found here. Even receiving a regular float number required parsing all the individual digits and decimal points as characters, and converting it to floats to be used by the Servo.
I used the Arduino Servo library to control the servo, and the Serial library to read in commands from the serial monitor. The main loop consisted of checking for serial data every iteration, and then commanding the servo to move to the read position, if available.
The serial code functions mainly consisted of parsing the array of chars into a float, for which there was a handy "atof()" method. I was surprised by how involved the functions needed to be to ensure that the serial data was read in correctly.
I was able to debug everything eventually, and directly interface to my microcontroller from my computer:
How to Make Almost Anything 2023