Interface Lab 1
The Digital Input and Output was relatively simple, although I ran into a few issues familiarizing myself with the breadboard and an odd Arduino software issue. The goal was to build a circuit where by pressing a button would send a signal to the Arduino which would toggle power between two LEDs.
The first major issue I ran into was a misunderstanding with my breadboard. I’ve only built with boards half this size, but I knew about the seperation that prevented signal from crossing left to right. However, I wasn’t aware that some double boards are also split vertically. This caused half of my circuit to work as expected while the rest failed silently. I was eventually able to track down the problem by measuring voltage at the Arduino and progressively down through the circuit.
Once I figured this out, completing the circuit on the top half of the board was relatively straight forward.
As a challenge, I wanted to experiment with the 8-ohm speaker and try to create a circuit where pressing the button disabled the LED and caused the speaker to play. I couldn’t get the Arduino
tone function to work no matter what I tried. To debug, I learned about
Serial.println for logging out values. I found that the
tone function was getting hammered pretty hard. According to the Arduino docs, this should just change the frequency, no big deal. However, I found that adding a 2ms delay to the loop (1ms wasn’t sufficient), resolved the issue. As a result, I decided to use a global variable to track the button state and only call the tone function when the state changed.
I made a post on reddit asking for what might be causing this to happen. One commenter suggested, "
tone starts an oscillation. If you call it too frequently the oscillation will never occur - like restarting a song before the first note is played." On the other hand, it looks like this is only an issue with Arduino Nanos, another commentor was able to run the code successfully on their Uno.
From that thread, I also learned that c++ functions can have
static variables which seems like a super clean alternative to the global variable approach.