Skip to content

Commit e7eee86

Browse files
refactored for more dynamic pin count
1 parent e27ca70 commit e7eee86

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed
Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
1-
// constants won't change. They're used here to set pin numbers:
2-
const int buttonPinA = 2;
3-
const int buttonPinB = 3;
4-
const int buttonPinC = 4;
5-
const int buttonPinD = 5;
6-
const int ledPin = 13; // the number of the LED pin
7-
8-
// variables will change:
9-
int buttonStateA = 0; // variable for reading the pushbutton status
10-
int buttonStateB = 0;
11-
int buttonStateC = 0;
12-
int buttonStateD = 0;
1+
int buttonPins[] = {2, 3, 4, 5};
2+
int buttonPinsCount = 4;
3+
const int outputPin = 13;
4+
int sequence[]= {3, 5, 2, 4};
5+
int correctGuesses = 0;
136

147
void setup() {
15-
// initialize the LED pin as an output:
16-
pinMode(ledPin, OUTPUT);
17-
// initialize the pushbutton pin as an input:
18-
pinMode(buttonPinA, INPUT);
19-
pinMode(buttonPinB, INPUT);
20-
pinMode(buttonPinC, INPUT);
21-
pinMode(buttonPinD, INPUT);
8+
for(int i=0; i < buttonPinsCount; i++){
9+
pinMode(buttonPins[i], INPUT);
10+
}
11+
pinMode(outputPin, OUTPUT);
2212
}
2313

2414
void loop() {
25-
// read the state of the pushbutton value:
26-
buttonStateA = digitalRead(buttonPinA);
27-
buttonStateB = digitalRead(buttonPinB);
28-
buttonStateC = digitalRead(buttonPinC);
29-
buttonStateD = digitalRead(buttonPinD);
30-
31-
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
32-
if (buttonStateA == HIGH || buttonStateB == HIGH || buttonStateC == HIGH || buttonStateD == HIGH) {
33-
// turn LED on:
34-
digitalWrite(ledPin, HIGH);
35-
} else {
36-
// turn LED off:
37-
digitalWrite(ledPin, LOW);
15+
bool inputRegistered = false;
16+
for(int i=0; i < buttonPinsCount; i++){
17+
int pin = buttonPins[i];
18+
int pinState = digitalRead(pin);
19+
if (pinState == HIGH){
20+
digitalWrite(outputPin, HIGH);
21+
inputRegistered = true;
22+
break;
23+
}
24+
}
25+
26+
if(!inputRegistered){
27+
digitalWrite(outputPin, LOW);
3828
}
3929
}

0 commit comments

Comments
 (0)