const int switch1Pin = 5; // Pin for switch 1
const int switch2Pin = 6; // Pin for switch 2
const int ledPins[] = {1, 2, 3, 4}; // Pins for the four LEDs
int counter = 0; // Initialize counter
bool ledStates[] = {false, false, false, false}; // Initialize LED states
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize switch pins as inputs
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
// Serial.begin(9600);
}
void loop() {
//Serial.println(counter);
// Read the state of the switches
int switch1State = digitalRead(switch1Pin);
int switch2State = digitalRead(switch2Pin);
// If switch 1 is pressed, increase the counter and display it on LEDs
if (switch1State == LOW) {
counter++;
if (counter > 15) {
counter = 0;
}
DisplayCounterOnLEDs(counter);
delay(300);
}
// If switch 2 is pressed, decrease the counter and display it on LEDs
if (switch2State == LOW) {
counter--;
if (counter < 0) {
counter = 15;
}
DisplayCounterOnLEDs(counter);
delay(300);
}
}
void DisplayCounterOnLEDs(int count) {
for (int i = 0; i < 4; i++) {
// Set LED state based on binary representation of count
bool state = bitRead(count, i);
digitalWrite(ledPins[i], state);
}
}