4. Analog Input-Output

Due Thursday 24 September, 2009

Using a red and a green LED, connected to two PWM (analogWrite) ports on the Arduino, make the lights pulse, increasing from off to brightest over a few seconds, and then back to off (either gradually or abruptly, as you wish). You can do this simply, by starting a counter variable at zero, and add one to it each time the program loop executes. (Hint: the colors will blend if you put the two LEDs together under a piece of milky plastic, a ping-pong ball, or white JELL-O®).

Add two switches to your circuit. Call one “red control” and the other “green control”. Now write a program to mix red and green. When the program starts, both lights are off. Each time you press the green control, the green light gets a little brighter (it need not blink or pulse). Every time you press the red control, the red light gets brighter. But when you press both buttons at the same time, they both reset to off. This time you’ll need two counter variables (say, redCount and GreenCount), and every time you press the red control, you add one to the redCount; same for green.

Now put the two ideas together. Both lights start out dark. When you press the green control light the first time, the green LED pulses from dark to a low brightness. Every time you press the green control light again, the pulsing goes to a brighter value. Same for red.

Although the analogRead function will return a value between 0 and 1023, in fact most sensors you connect will only produce values in a smaller range. It’s useful to know what range your sensor is producing. So: using any analog sensor you can find or make (a photocell, a pressure switch made of foam and two wires, your skin, or a potentiometer (variable resistor)), write a program that keeps track of the lowest and highest values that the sensor provides.

You’ll need two variables (say, lowValue and highValue). Each time through the loop, read the analog port, and compare the value you get with each of these variables. If it’s lower than lowValue, then set lowValue to the value you just read. If it’s higher than highValue, then set highValue to the new value you just read.

Use Serial.print and Serial.println to print out the lowest, highest, and current analog sensor values on the serial monitor on your computer. The display should look something like this:
400 410 410
394 410 394
392 410 392
392 410 399

Where the first number is the lowest sensor reading that the program has seen, the second number is the highest sensor reading it’s seen, and the last number is the current sensor reading.

Hint: Do not use digital pins 0 or 1 when you are using Serial.print — they won’t work.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: