-1

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;

  }
sanch
  • 696
  • 1
  • 7
  • 21
  • 1
    You need to ask an actual question. – JBentley Dec 19 '13 at 23:14
  • 1
    It's helpful to ask you question in the following format. "I want to do `X`. I tried `Y`, but when I ran it I saw `Z`. What should I do differently to see `X`?" – Bill Dec 19 '13 at 23:20
  • Wow, an Arduino question that isn't about using Serial correctly. Still the basic loop problem, you only read the button state when you enter the loop. And then loop again by yourself without ever updating the state. So you can never see the button's being pressed. – Hans Passant Dec 19 '13 at 23:40

1 Answers1

3

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.

Chris J. Kiick
  • 1,487
  • 11
  • 14
  • Sorry for the such a delayed response, got all carried away with the holidays. :) So I have updated my code to make use of the "Bounce" library found on the arduino site. To my knowledge, it is functioning properly. I know that I didn't really give a clear cut answer as to what exactly I needed help with, so here is what I need. I need to be able to effectively create a "gameClock" that sets a time for the game. Once the time is up, the button counters are reset and we can play again. I updated my code above. – sanch Dec 25 '13 at 20:49
  • Much better. Now we can focus on the original question. I'm not going to write the code for you. When a game starts, read and store the value of millis() as the start time. To see if the game is over, subtract start time from millis(), then compare to game time. – Chris J. Kiick Dec 26 '13 at 08:20