int ledPin1 = 3;
int ledPin2 = 5;
int ledPin3 = 7;
int buttonUp = 10; // Button to increase count
int buttonDown = 8; // Button to decrease count
int counter = 0; // Binary counter
bool lastButtonUpState = LOW; // Previous state of the buttonUp
bool lastButtonDownState = LOW; // Previous state of the buttonDown
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonUp, INPUT); // Initialize button pin as input
pinMode(buttonDown, INPUT); // Initialize button pin as input
}
void loop() {
bool currentButtonUpState = digitalRead(buttonUp);
bool currentButtonDownState = digitalRead(buttonDown);
// Check for buttonUp state change from not pressed to pressed
if (currentButtonUpState == HIGH && lastButtonUpState == LOW) {
if (counter < 7) {
counter++;
updateLEDs(counter);
}
delay(50); // Basic debouncing
}
lastButtonUpState = currentButtonUpState; // Update the last button state
// Check for buttonDown state change from not pressed to pressed
if (currentButtonDownState == HIGH && lastButtonDownState == LOW) {
if (counter > 0) {
counter--;
updateLEDs(counter);
}
delay(50); // Basic debouncing
}
lastButtonDownState = currentButtonDownState; // Update the last button state
}
void updateLEDs(int value) {
digitalWrite(ledPin1, bitRead(value, 0));
digitalWrite(ledPin2, bitRead(value, 1));
digitalWrite(ledPin3, bitRead(value, 2));
}