Week #3
Analog output is apparently actually pseudo–analog output. It's sending digital output (being 1 and 0, or on and off), on a series of voltage pulses at regular intervals, and varying the width of the pulses. This is called pulse width modulation (PWM) and the Arduino pins that are compatible with it are marked with a Tilde ~
Started off with something simple: connecting a potentiometer to control the dimming of a LED using PWM:
Then moved onto controlling a servo motor in the same way — "The longer the pulse, the greater the angle".
Getting a variable voltage is nice and all, but when dealing with sounds, or speakers for that matter, we'd want to also control the pitch. Otherwise, it'll stay the same no matter the voltage. For that I learned to use the tone() function! While the use case itself is pretty straight forward, I had a hard time trying to control the pitch with a phototransistor. At first it didn't work at all, but not because I wired it wrong — the problem was in the code. I used the map() function to map the phototransistor's values to 100-1000 Hz. I printed out the reading from my phototransistor and it ranged between 800 to 1023. So I changed the map() function's input number to be 800-1023 as well. This has caused it to work for a few seconds and then stop — I realized it always stopped when it reached it's minimum value so that must mean the map() function is causing it to crash. I changed the range back to 0–1023 and now it works but the control itself is... not what I expected. It's pretty wonky and inconsistent. I hope to understand more about this next class though...
I did manage to make it play a silly tune though:
And lastly, made a DIY instrument:
This is really cool. I'm looking forward to exploring many other ways of creating such instruments. It's cool to kind of use FSRs as buttons, although it obviously doesn't completely utilize all of these sensors' capabilities. It does make sense if I wanted to maybe create something very flat, and possibly insert the FSRs beneath a sheet of paper. Definitely going to try that and report back!
Learned how to control and Arduino with an analog sensor (I used a FSR). Not sure why but really enjoyed that one.