I am trying to make a simple Arduino game that keeps track of the number of times a button has been pressed. There are two buttons, one for each user, and whoever is closest to the random number that the Arduino picks, wins. "winning" being a light comes on next to the winers button. However I am stuck in what seems to be a rut—before I go further, I will divulge that I am just barely two weeks old into the whole arduino/C++ environment so my knowledge is pretty low.
Basically what I want to happen is that the game lasts a certain amount of time, lets say 5 seconds, then after the five seconds, all the button pushes are tallied and compared and a winner is picked. Then the game resets itself and we can play again. Where I am getting stuck at is in the timing function. I originally was subtracting a gameClock variable from the millis() function however I have read that it is bad practice to reset the millis() function. Perhaps my code will give you a better understanding of what I am trying to accomplish.
#include <Bounce.h>
//Using Arduino UNO
#define RBUTTON 2
#define RRLED 3
#define RGLED 4
#define MBLED 5
#define LGLED 6
#define RLLED 7
#define LBUTTON 8
Bounce BounceR = Bounce(RBUTTON, 5);
Bounce BounceL = Bounce(LBUTTON, 5);
int lastStateR = 0;
int lastStateL = 0;
int switchLCount = 0;
int switchRCount = 0;
long gameClock = 5000;
void setup() {
//LED from left to right
pinMode(RBUTTON, INPUT);
pinMode(RRLED, OUTPUT); //Red
pinMode(RGLED, OUTPUT); //Green
pinMode(MBLED, OUTPUT); //Blue
pinMode(LGLED, OUTPUT); //Green
pinMode(RLLED, OUTPUT); //Red
pinMode(LBUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BounceR.update();
BounceL.update();
int total = switchRCount + switchLCount;
int valueR = BounceR.read();
int valueL = BounceL.read();
if (valueL != lastStateL) {
if (valueL == HIGH) {
switchLCount++;
Serial.print("Left button:");
Serial.println(switchLCount);
Serial.print("Total: ");
Serial.println(total);
}
}
lastStateL = valueL;
if (valueR != lastStateR) {
if (valueR == HIGH) {
switchRCount++;
Serial.print("Right button: ");
Serial.println(switchRCount);
Serial.print("Total: ");
Serial.println(total);
}
}
lastStateR = valueR;
}
You really should look at the arduino.cc forums for help on this stuff. There's a whole archive of knowledge, plus many people who have a lot of arduino experience.
There are basically 3 problems with your sketch. First, as you said, the gameClock is not working like you want it to. Just using a counter isn't going to cut it, even at only 16Mhz, the arduino is going to zip right through it in a lot less that 5 seconds. You need to actually time it.
Second, your button 'debouncing' is broken. Debouncing buttons is actually quite tricky. Fortunately, many many people have already solved the problem. Check the arduino libraries, the forum and the playground. The problem here is that you never set the lastDebounceTime variables to anything.
Finally, your sketch only reads the buttons once per game. That throws everything else off. It kills your debouncing and messes up the button press count.
I'll stop now to give you a chance to work it out. Post another comment if you want more help.