Hi Jeremy
First of all, change this :int key = random(0, 5);
back to :int key = random(0, LEDS_BUTTS);
Or you will never chose a led other than the first 5.
Here i made a small mistake
for (int i = 0; i < 6; i++) {
if (digitalRead(22 + i * 2) == 0) choice = i; // remember button pressed:
}
the loop should be :
for (int i = 0; i < LED_BUTTS; i++) {
if (digitalRead(22 + i * 2) == 0) choice = i; // remember button pressed:
}
error like this happen. The idea of a #define is that you can easily adjust the program to the hardware that you have, without having to change every point where a value depends on the hardware. Like that you prevent a lot of bugs.
Guess my book will be here on Saturday.
Oh great, well it's in the reading. Of course there will be parts of the book that you will just flick through, and as far as i remember it is actually focused on C++ in general, not on the Arduino dialect, but a lot of the techniques are explained.
For now though the only thing I can think of is one thing we half mentioned and that's the difficulty level. 7 buttons vs 14 vs 20.....easy, medium, and hard. So I've read several different post and examples that's used either a button press or a users typed in.
I think with your current setup the obvious choice would be 3 different start buttons.
What is going to be a bit of a thing is that the way you have it setup, there is only 16 consecutive led & button pins. If you want to add more you may have to create an array with pin locations (rather than the offset + 2 * led/button-nr calculation that you do now)
For implementation what you can do is instead of using the #define LEDS_BUTTS 6
where the compiler just replaces any occurrence of 'LEDS_BUTTS' with '6' for some parts with a variable. We can keep it local and pass it back and forth between the functions.
Here is the example :
#define LEDS_BUTTS 16 // the absolute max
#define EASY_BUTT 2
#define MED_BUTT 3
#define HARD_BUTT 4
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
for (int i = 0; i < LEDS_BUTTS; i++) {
pinMode(22 + i * 2, INPUT_PULLUP);
pinMode(23 + i * 2, OUTPUT); // 23 + 15 * 2 = 53
}
pinMode(MED_BUTT, INPUT_PULLUP);
pinMode(EASY_BUTT, INPUT_PULLUP);
pinMode(HARD_BUTT, INPUT_PULLUP);
}
void loop() {
int ledandbuttons = WaitForStart(); // now WaitForStart returns the number of leds/buttons
PlayIntro(ledandbuttons);
int scr = PlayGame(ledandbuttons);
ShowScore(scr);
}
int WaitForStart() {
while(1) {
if (digitalRead(EASY_BUTT) == LOW) return 5;
if (digitalRead(MED_BUTT) == LOW) return 10;
if (digitalRead(HARD_BUTT) == LOW) return 16;
int ledon = LOW;
if ((millis() % 1000) / 500) ledon = HIGH;
for (int i = 0; i < LEDS_BUTTS; i++) { // blink the leds while waiting
digitalWrite(23 + i * 2, ledon);
}
}
}
void PlayIntro(int led_butts) { // in the intro you will see which leds can be used
for (int i = 0; i < led_butts; i++) { // turn 'm all on
digitalWrite(23 + i * 2, HIGH);
}
for (int i = 0; i < led_butts; i++) { // turn 'm off one by one
delay(200);
digitalWrite(23 + i * 2, LOW);
}
delay(500); // wait a bit before we start.
}
int PlayGame(int led_butts) {
int score = 0;
float timeout = 5000.0;
while (1) { // keep looping
int key = random(0, led_butts);
for (int i = 0; i < led_butts; i++) { // light the led
if (i == key) digitalWrite(23 + i * 2, HIGH);
else digitalWrite(23 + i * 2, LOW);
}
int choice = -1;
unsigned long millisRef = millis();
while ((choice == -1) && (millis() - millisRef < (uint32_t) timeout)) {
for (int i = 0; i < led_butts; i++) {
if (digitalRead(22 + i * 2) == 0) choice = i; // remember button pressed:
}
}
if (choice != -1) { // button pressed
bool bpress = true;
delay(50);
while (bpress) {
bpress = false;
for (int i = 0; i < led_butts; i++) {
if (digitalRead(22 + i * 2) == 0) bpress = true;
}
} // and released
delay(50);
}
if (choice == key) {
score++; // increase score
timeout = timeout * .90;
Serial.print("OK, score=");
Serial.println(score); // print score in Arduino output Window.
// delay(200);
}
else return score; // or exit.
} // while(1)
}
void ShowScore(int score) {
Serial.print("Wrong key or timeout; score=");
Serial.println(score);
}
Note that i use all uppercase for a #define and lowercase for variables. This is just a convention, you can use upper and lowercase any way you like but it does help to be consistent. I did not add your buzzer and horn thing to the example. Please remember to indent your code properly, it will make it more clear what closing braces belongs to which opening brace and what part of the code is conditionally executed. Just hit ctrl-T and the IDE will do it for you.