I started out by setting out my materials and reading the lab. I’m still in the process of re-familiarizing myself with the different terms for sound (Hertz, period, frequency, etc). During class, I’m hoping Pedro will explain the in-depth understanding of why we can’t use analogWrite() to change the frequency and what tone() does differently to do that instead.
I forgot to take a picture of my setup, but my circuit was done according to this plan on the right. Note to self: I want to get better at drawing schematics from memory.
The first thing now that I have my setup connected is to test that the input is reading correctly. I do this by using the analogRead() command. It’s interesting to note that the analogRead() does not need a pinMode() in the setup() function as digital input/output does.
Input is confirmed to be working correctly.
After input is confirmed to be working, I check output next by asking the speaker to play just a regular one note tone.
Hooray! My input and output are both working correctly. Now, I can get into the more complicated stuff by changing up the computation. I followed the code suggested from the labs, but tried to code the for loop on my own, just to refresh my memory on programming in C++.
This code asks to play the melody just once using "pitches.h" as a reference library for all the notes.
After hearing the computation play the melody okay, I started preparing the breadboard to have multiple sensors so that the tones the speaker plays would be dictated by the analog input (using analogRead) instead of just the information stored from the code.
The code here reads whether each of the sensors are being activated, and if so, plays their designated note. I noticed that the speaker wasn't playing the note correctly until I set the threshold to 200 (so there was less noise) and added a delay(). Maybe next time, I will try using a counter against millis() instead to read the sensor every 20 ms instead of stopping the whole program for 20 ms every time the loop runs.
After finishing the speaker lab, I moved onto the servo lab. At first, I tried to connect power to the 5v prong instead of the 3.3v prong because I thought the servo would use more power and was confused why my components weren’t working. My board was powered on, but the analog reading wasn’t working properly. This was resolved as soon as I adjusted back to the 3.3v prong.
This is what the code looks likes when using analogRead() on the potentiometer and mapping it to a range that the servo is able to move (up to 180 degrees). This time, I am using millis() as suggested by the lab's code.
The servo moves! Even better when there’s a shark plushie attached on top so you can move the pot to make the shark look like it’s swimming. As my final part of the lab, I combined both the speaker and servo together into one code and one board. I also included my own song instead, choosing the most iconic line from “Take Me Home, Country Roads” by John Denver. I still wanted to keep the pot in to control the direction the shark faces when dancing at the end of the line. The way that I programmed it was to have the loop() parse through the melody array using a global counter (instead of playing the melody all at once in the setup() function). At the 15th and 16th note of the melody, the shark will move a step every time.
Note: I changed the mapping range from 0-1024 to 500-1024. I noticed that the full range the pot was giving from the analogRead() Serial printing was only within that range.