int ledPins[] = {10, 9};
int ledAmount = 2;
int btnPin = 2;
int btnState = 0;
boolean isBtnPressed = false;
int counter = 0;
void setup() {
// put your setup code here, to run once:
for(int i=0; i<ledAmount; i++){
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
pinMode(btnPin, INPUT);
Serial.begin(9600);
}
void loop() {
// check the button
btnState = digitalRead(btnPin);
// check if the state is different from the previous loop
if (btnState == HIGH && isBtnPressed == false) {
// add one to the counter
counter++;
isBtnPressed = true;
// check if the counter is too large
if(counter>ledAmount){
counter = 0;
}
Serial.println(counter);
}else if(btnState == LOW){
isBtnPressed = false;
}
for(int i=0; i<ledAmount; i++){
if(counter >= i+1){
digitalWrite(ledPins[i], HIGH);
}else{
digitalWrite(ledPins[i], LOW);
}
}
// delay helps to prevent noise from the input signal
// even better way to do this would be to implement debouncing:
// https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
delay(10);
}