#include <Servo.h> // allowed package. Not used in this code but available if needed later.
const int led1Pin = 10; // PWM pin for led1
const int led2Pin = 11; // PWM pin for led2
// Define the Arduino analog connections for the potentiometers
const int pot1Pin = A0; // Controls led1 brightness
const int pot2Pin = A1; // Controls led2 brightness
const int pot3Pin = A2; // Controls the number of lit segments for the bargraph
// Define digital pins for the 10 segments of the LED bar graph
const int bargraphPins[10] = {2, 3, 4, 5, 6, 7, 8, 9, 12, 13};
void setup() {
// Initialize LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// Initialize bargraph segment digital pins as outputs
for(int i=0; i < 10; i++){
pinMode(bargraphPins[i], OUTPUT);
// Ensure segments are off on startup
digitalWrite(bargraphPins[i], LOW);
}
// Ensure individual LEDs are off initially
analogWrite(led1Pin, 0);
analogWrite(led2Pin, 0);
}
void loop() {
// Read the three potentiometer values
int pot1Value = analogRead(pot1Pin); // 0-1023 for led1 brightness
int pot2Value = analogRead(pot2Pin); // 0-1023 for led2 brightness
int pot3Value = analogRead(pot3Pin); // 0-1023 for the LED bar graph control
// Map the potentiometer values accordingly
int led1Brightness = map(pot1Value, 0, 1023, 0, 255);
int led2Brightness = map(pot2Value, 0, 1023, 0, 255);
// For the bar graph, map pot3Value to an integer from 0 (none lit) to 10 (all lit)
int segmentsToLight = map(pot3Value, 0, 1023, 0, 10);
// Update individual LED brightness using PWM
analogWrite(led1Pin, led1Brightness);
analogWrite(led2Pin, led2Brightness);
// Update the LED bar graph segments
// Turn on the first 'segmentsToLight' segments and turn off the rest
for (int i = 0; i < 10; i++) {
if (i < segmentsToLight) {
digitalWrite(bargraphPins[i], HIGH);
} else {
digitalWrite(bargraphPins[i], LOW);
}
}
// A short delay to allow for stable readings
delay(10);
}