|
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; |
13 | 6 |
|
14 | 7 | 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); |
22 | 12 | } |
23 | 13 |
|
24 | 14 | 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); |
38 | 28 | } |
39 | 29 | } |
0 commit comments