Arduino 2

Getting ready

Log in and start a terminal session. Type the command id. Make sure you are in the dialout group.

Plug in Arduino (hopefully with DangerShield) and type the command Arduino. This make sure you have the latest version.

Under Tools make sure you are using the Board Arduino/Genuino Uno and the Board Arduino/Genuino Uno and the port /dev/ttyACM0. The port will be very different on a Windows or Mac OS X computer.

First program

Code references

Experiment 1 — Persistence of vision

Try a smaller delays: 100 mSec.

Try an even smaller delays: 10 mSec. This should look on, but maybe a little dimmer.

Try an uneven delay: 1 mSec with high and 9 mSec when low. It should look quite dim.

Experiment 2 — sound

Make both delays equal to 2 mSec. Change the pin number 6 to 3 in all three places. Use your cell phone’s guitar tuner to measure the experience. See the schematic for the Danger Shield to determine how I knew how to use 4.

This experiment will irritate some people. Put a Post-It over the buzzer in that case.

If you are using a bare Arduino, you need to look at the Experiment 11: Using a Piezo Buzzer and borrow and breadboard and a Piezo buzzer.

Shutting it up

Change the 3 in setup to 5 and reload.

Trying for Middle C

The frequency of middle C is about 261.625565 Hz. That means the period is about 3822 uSec and the half period is 1911 uSec.

Look at the Arduino documentation for delayMicroseconds and see if you can get a middle C.

Playing a song

Try FileExamples02.DigitaltoneMelody. You will need to change the 8 to 3 in the tone and noTone calls.

You can try to play it over and over and over and over again.

Experiment 3: Printing data

You can take a look at Limor Fried’s ten-year old tutorial. for a bit more information about printing.

Of you can pay attention in class. You will need to use the following methods.

Let’s try to count down from 10 and then start again.

Experiment 4: Building a musical instrument (sort of)

Start by downloading another example FileExamples01.BasicsAnalogInOurSerial. You will need to save it and change the value assigned to analogOutPin to 5, for Danger Shield, or 13, for plain Arduino.

If you have an bare Arduino, you will need to add a potentiometer. You can see how to do this on an Arduino tutorial foring read a poteniometer. Everyone should do this! Users of the bare Arduino should use analog input 0 rather than input 2 to get the example to work.

Your Arduino should now dim the LED off and on. It is using the analogWrite() routine which uses pulse width modulation, or PWM, a common way to control speakers, lights, and motors.

Now switch the analogOutPin to your speaker: 3, for Danger Shield, or 11, for plain Arduino with Piezzo. This is not very exciting at all.

Download a ZIPped Arduino sketch and expand it in your sketchbook directory, probably Arduino. If you are using a danger shield, you will need to change the value of soundPin to 3.

Try to figure out what the program is doing. It’s a little complicated. You should be able to play a little music by moving the slider or by turning the pot. (Everyone calls them “pots.”)

What we’d like to do here is modify the program so that the range is a bit more useful. I think that setting the halfPeriod so that it ranges from 625 µsec (800 Hz) to 2500 µsec (200 Hz) would be a good start. See if you can figure out how to do that.