@@ -32,15 +32,17 @@ const char* words[arraySize] = {"moose", "beaver", "bear", "goose", "dog", "cat"
32
32
" frog" , " alligator" , " kangaroo" , " hippo" , " rabbit"
33
33
};
34
34
35
- int sequence[] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; // start with an array full of 0s
35
+ // the start value in the sequence array must have a value that could never be an index of an array
36
+ // or at least a value outside the range of 0 to the size of the words array - 1; in this case, it can't be between 0 to 24
37
+ int sequence[] = { -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 }; // start with an array full of -1's
36
38
37
39
void setup () {
38
40
39
41
pinMode (buttonPin, INPUT_PULLUP); // set the button pin as an input
40
42
41
43
lcd.begin (16 , 2 ); // tell the LCD library the size of the screen
42
44
43
- generateRandomOrder (); // generate an array of random numbers from 1-25 that will determine which order the words are shown in
45
+ generateRandomOrder (); // generate an array of random numbers from 0 to 24 that will determine which order the words are shown in
44
46
45
47
showStartSequence (); // print the start sequence text
46
48
@@ -73,7 +75,7 @@ void loop() {
73
75
}
74
76
75
77
if (digitalRead (buttonPin) == LOW) {
76
- tone (buzzerPin, 272 , 1 );
78
+ tone (buzzerPin, 272 , 10 ); // emit a short beep when the button is pressed
77
79
}
78
80
79
81
} // exit this loop when the button is pressed
@@ -123,14 +125,14 @@ void generateRandomOrder() {
123
125
124
126
randomSeed (analogRead (0 )); // reset the random seed (Arduino needs this to generate truly random numbers
125
127
126
- for (int i = 0 ; i < 24 ; i++) { // do this until all 25 positions are filled
128
+ for (int i = 0 ; i < arraySize ; i++) { // do this until all 25 positions are filled
127
129
128
130
int currentNumber = 0 ; // variable to hold the current number
129
131
boolean match = false ; // does the currentNumber match any of the previous numbers?
130
132
131
133
// generate random numbers until you've generated one that doesn't match any of the other numbers in the array
132
134
do {
133
- currentNumber = random (0 , arraySize); // generate a random number from 1-25
135
+ currentNumber = random (0 , arraySize); // generate a random number from 0 to 24
134
136
match = false ; // we haven't checked for matches yet, so start by assuming that it doesn't match
135
137
for (int i = 0 ; i < arraySize; i++) { // for all 25 numbers in the array
136
138
if (currentNumber == sequence[i]) { // does the currentNumber match any of the numbers?
0 commit comments