int buttonInput;
byte state = B00000001;
byte originalState = state; // Store the original state
unsigned long debounce = 50;
unsigned long lastChange = millis();
unsigned long prevMillis = 0;
const long waktu = 500;
boolean ledShiftEnabled = false; // Flag to control LED shifting
void setup() {
// Set pin 8 as input
pinMode(8, INPUT_PULLUP);
DDRD = B11111111;
buttonInput = digitalRead(8);
}
void loop() {
if(millis() - lastChange >= debounce){
byte buttonState = digitalRead(8);
if(buttonState != buttonInput){
lastChange = millis();
buttonInput = buttonState;
if (buttonInput == HIGH) {
// Toggle the LED shifting flag when the button is pressed
ledShiftEnabled = !ledShiftEnabled;
}
}
if (ledShiftEnabled) {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to shift the state
if (currentMillis - prevMillis >= waktu) {
state = state << 1; // Shift the state to activate the next LED
if (state == 0) {
state = originalState; // Reset the state to its original value if it becomes zero
}
prevMillis = currentMillis; // Update the previous time
}
// Update LEDs based on the state
PORTD = state; // Write the state to the port connected to the LEDs
}
}
}