// Define DIP switch and LED bar graph pins
const int dipSwitchPins[] = {2, 3, 4, 5}; // Pins connected to the DIP switch
const int numDipSwitches = 4; // Number of switches in the DIP switch
const int ledPins[] = {6, 7, 8, 9}; // Pins connected to the LEDs in the bar graph
const int numLEDs = 4; // Number of LEDs in the bar graph
void setup() {
// Set DIP switch pins as inputs with internal pull-up resistors
for (int i = 0; i < numDipSwitches; i++) {
pinMode(dipSwitchPins[i], INPUT_PULLUP);
}
// Set LED bar graph pins as outputs
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Read the state of the DIP switch
for (int i = 0; i < numDipSwitches; i++) {
int switchState = digitalRead(dipSwitchPins[i]);
// Illuminate corresponding LED if the switch is ON
if (switchState == LOW) {
illuminateLEDs(i);
} else {
turnOffLEDs();
}
}
}
void illuminateLEDs(int switchIndex) {
// Turn on LEDs up to the specified switch index
for (int i = 0; i <= switchIndex; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
void turnOffLEDs() {
// Turn off all LEDs in the bar graph
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], LOW);
}
}