/*
Description:
- Read the potentiometer value on A0.
- Divide the 0-1023 range into three regions.
• Lower third (0-341) turns on led1, and the RGB LED shows red.
The red channel’s PWM is mapped from the potentiometer reading (0->0, 341->255).
• Middle third (342-682) turns on led2, and the RGB LED shows green.
The green PWM is mapped from 342 (0) to 682 (255).
• Upper third (683-1023) turns on led3, and the RGB LED shows blue.
The blue PWM is mapped from 683 (0) to 1023 (255).
- All other channels are turned off in each region.
*/
const int potPin = A0; // Potentiometer signal pin
// Single color LED pins
const int led1Pin = 2;
const int led2Pin = 3;
const int led3Pin = 4;
// RGB LED pins (PWM capable)
const int redPin = 5;
const int greenPin = 6;
const int bluePin = 7;
void setup() {
// Set LED pins as outputs.
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Initialization: all LEDs off.
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value (0-1023)
// Turn off all single-color LEDs first.
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
// Set all RGB channels to 0 initially.
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
if(potValue <= 341) {
// Lower third: led1 indicator on, RGB shows red.
digitalWrite(led1Pin, HIGH);
int redBrightness = map(potValue, 0, 341, 0, 255);
analogWrite(redPin, redBrightness);
}
else if(potValue <= 682) {
// Middle third: led2 indicator on, RGB shows green.
digitalWrite(led2Pin, HIGH);
int greenBrightness = map(potValue, 342, 682, 0, 255);
analogWrite(greenPin, greenBrightness);
}
else { // potValue is between 683 and 1023.
// Upper third: led3 indicator on, RGB shows blue.
digitalWrite(led3Pin, HIGH);
int blueBrightness = map(potValue, 683, 1023, 0, 255);
analogWrite(bluePin, blueBrightness);
}
delay(10); // Small delay for stability
}