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;
}