Assignment: Sensing: Potentiometers
Collaborators:
Collaborators: Beyonder
We used 4 potentiometers to controls each LED's brightness and blink-rate independently.
Pots A, B, and C control the brightness or blink-rate depending on the state of pot D. When pot D's value is below 512, the other three control the LEDs' brightness. When pot D's value is equal to or above 512, the remaining three pots control the LEDs' blink-rate.
A minor modification to the system was made so that higher pot values produced higher blink-rates. This seems a more intuitive behavior. Also interesting is that given the values of pots A, B, and C, pot D can be used to "convert" brightness values to blink-rates and vice-versa by adjusting it. In effect, it is being used as a toggle switch.
Caveat: In "blink mode" the brightness of the the LEDs is 100%. The system does not remember the brightness previously set.
Here is the code:
// Analog pin settings
int aIn = 0; // Potentiometers connected to analog pins 0, 1, and 2
int bIn = 1; // (Connect power to 5V and ground to analog ground)
int cIn = 2;
int dIn = 3;
// Digital pin settings
int aOut = 9; // LEDs connected to digital pins 9, 10 and 11
int bOut = 10; // (Connect cathodes to digital ground)
int cOut = 11;
// Values
int aVal = 0; // Variables to store the input from the potentiometers
int bVal = 0;
int cVal = 0;
int dVal = 0;
unsigned long Atime = 0;
unsigned long Btime = 0;
unsigned long Ctime = 0;
boolean value;
void setup()
{
pinMode(aOut, OUTPUT); // sets the digital pins as output
pinMode(bOut, OUTPUT);
pinMode(cOut, OUTPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop()
{
aVal = analogRead(aIn); // read input pins, convert to 0-255 scale
bVal = analogRead(bIn);
cVal = analogRead(cIn);
dVal = analogRead(dIn);
//Pot D controls whether the other three post affect blinking or brightness
//Controls Brightness
if (dVal < 512) {
analogWrite(aOut, aVal/4); // Send new values to LEDs
analogWrite(bOut, bVal/4);
analogWrite(cOut, cVal/4);
}
//Controls Blinking
if (dVal >= 512) {
//LED A
if (millis() - Atime > 1024-aVal) {
Atime = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(aOut, value);
}
//LED B
if (millis() - Btime > 1024-bVal) {
Btime = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(bOut, value);
}
//LED C
if (millis() - Ctime > 1024-cVal) {
Ctime = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(cOut, value);
}
}
}