Sure! Here is an example Arduino program that will turn on one of three LEDs based on the position of a potentiometer (POT):
const int POT_PIN = A0; // Analog input pin for the potentiometer
const int LED_PIN_1 = 2; // Digital output pin for LED 1
const int LED_PIN_2 = 3; // Digital output pin for LED 2
const int LED_PIN_3 = 4; // Digital output pin for LED 3
const int POT_LOW_THRESH = 341; // Threshold for low section (0 - 341)
const int POT_MEDIUM_THRESH = 682; // Threshold for medium section (342 - 682)
const int POT_HIGH_THRESH = 1023; // Threshold for high section (683 - 1023)
void setup() {
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_3, OUTPUT);
}
void loop() {
int potValue = analogRead(POT_PIN); // Read the value of the potentiometer
if (potValue <= POT_LOW_THRESH) {
digitalWrite(LED_PIN_1, HIGH); // Turn on LED 1
digitalWrite(LED_PIN_2, LOW); // Turn off LED 2
digitalWrite(LED_PIN_3, LOW); // Turn off LED 3
}
else if (potValue > POT_LOW_THRESH && potValue <= POT_MEDIUM_THRESH) {
digitalWrite(LED_PIN_1, LOW); // Turn off LED 1
digitalWrite(LED_PIN_2, HIGH); // Turn on LED 2
digitalWrite(LED_PIN_3, LOW); // Turn off LED 3
}
else if (potValue > POT_MEDIUM_THRESH && potValue <= POT_HIGH_THRESH) {
digitalWrite(LED_PIN_1, LOW); // Turn off LED 1
digitalWrite(LED_PIN_2, LOW); // Turn off LED 2
digitalWrite(LED_PIN_3, HIGH); // Turn on LED 3
}
}
In this program, we first define the input and output pins. The POT_PIN is the analog input pin connected to the potentiometer, while LED_PIN_1, LED_PIN_2, and LED_PIN_3 are the digital output pins connected to the three LEDs.
We also define the thresholds POT_LOW_THRESH, POT_MEDIUM_THRESH, and POT_HIGH_THRESH, which divide the potentiometer's range into three equal sections: low, medium, and high.
Inside the loop() function, we read the value of the potentiometer using analogRead(POT_PIN). Then, based on the potentiometer value, we use conditional statements (if, else if) to determine which LED to turn on and turn the rest off.
If the potentiometer value is less than or equal to POT_LOW_THRESH, we turn on LED 1 and turn off LED 2 and LED 3. If the potentiometer value is between POT_LOW_THRESH and POT_MEDIUM_THRESH, we turn on LED 2 and turn off LED 1 and LED 3. If the potentiometer value is between POT_MEDIUM_THRESH and POT_HIGH_THRESH, we turn on LED 3 and turn off LED 1 and LED 2.
This way, as you turn the potentiometer from the low section to the high section, the program will move between the low, medium, and high sections, turning on one LED at a time and turning the rest off based on the potentiometer's position.