Tuesday, October 11, 2011

Analog Input

Example 6 of Getting Started is the first example to use analog input rather than digital input, specifically from a light meter. Now, instead of getting on/off impulses from a button, we will be taking values in the range of 0-1023, representing voltages from 0 to 5 volts.

In Example 6A, we still used a digital output, in that we used the input from the light meter to control how fast an LED blinked. Specifically, the input value was used directly as the delay value, so that increasing the amount of light present slowed the blinking of the light. No pictures for that, since it's rather difficult to photograph the rate of a blinking light. Given that it was some ten or so lines of code and used (almost) the same circuit as that which I used for Example 6B (pictured below), I can assure you that it worked. I did have to toy with the resistance a bit though, since I worked on this one before I labeled all of my resistors. The lower the resistance, the slower the light blinked. Makes sense!

Example 6B used both analog input and output. This time, the input from the light meter directly controlled the PWM of the LED, just like in Example 4:

     val = analogRead(0);     //sensor is at pin 0
     analogWrite(LED, val/4); //send that (modified) value to the LED!

So when the meter is getting full light...
...the light is superflous!
And when the meter is getting no light...
...the light is pointless!

So for any practical purposes, this is about the opposite of what you would want, but it does demonstrate a direct relationship between analog input and output. By the way, I believe all you would need for a practical reworking of this (say, a night light or solar powered porch light) would be the reciprocal (4/val), and an exception case for val == 0, though val never seems to come out as 0 for some reason.

Next time, we'll be sending data back to the computer through a virtual serial connection, which, believe it or not, completes the basic knowledge necessary to make this bike work: analog input routed through USB to a computer. 

No comments:

Post a Comment