Step 6: Setting the Time With Potentiometers
If you're curious, a potientiometer works as a voltage divider. The reason why the resistance of the potentiometer is not necessary is because it's the only load between 5V and ground. Thus, on one extreme end of the knob you're connected directly to ground (0V), on the other extreme you're connected directly to 5V, and in between you'll get a smooth transistion from 0 to 5V. If you added some other load in series to one of the outside pins, then you'd need to carefully consider what resistance the potentiometer should be.
Reading Values from Potentiometers
In order to set time using potentiometers, we'll hook up one outside lead to ground and the other outside lead to 5V, and measure the voltage from the middle lead using analogRead(). This will return a value between 0 (0V) and 1023 (5V). After that, it's just a matter of scaling the value to the number of hours (24) and minutes (60). We'll read the hours from pin A0, and the minutes from pin A1.
Behavior of Potentiometers (ie, Taper)
The relationship between the angle of the knob and the amount of resistance is known as the taper. Volume knobs often have a non-linear taper, where it requires more of a turn near the end to make a difference. Since all hour and minute values are equal, it seems logical to have a linear taper.
All that's needed is to set the m_inTimeSetMode to true for this step, and add the implementation for the getTimeFromPots() method to test out our time setting:
boolean m_inTimeSetMode = true;
int HOUR_INPUT_PIN = A0;
int MIN_INPUT_PIN = A1;
* This method reads the values from the 2 potentiometers, converts them to
* minutes and hours, and sets m_minute and m_hour to the associated values.
// read the potentiometers
int hourSensor = analogRead(HOUR_INPUT_PIN);
int minuteSensor = analogRead(MIN_INPUT_PIN);
// scale the readings (from 0 to 1023) to the appropriate scale (0 to 23 for hours, 0 to 59 for minutes)
setTime(map(hourSensor, 0, 1023, 0, 23), map(minuteSensor, 0, 1023, 0, 59), 0);
// we set the last tick to now, because when we set m_inTimeSetMode to false,
// many seconds could have passed since the last one, and the time would jump ahead
m_lastTick = millis();