Instructables
Picture of 100x button press timed game
The game is simple. You have an allotted amount of time to press the button 100 times. Also, there are 5 LED lights on the breadboard. Every 10 presses, one lights up. Once you hit 60 they start to turn off one by one. If you get 100 presses in the time limit then a message comes up declaring you "The Winner!". If you lose, it tells you that you lost and to please try again. There is a time limit between when you can restart the game.
-We used the float, long, and millis systems to measure our time.
-5 LED Pins
-1 Push Button
- Named the LED buttons
- An elapsed time = start time - millis()
- A losing and winning system
- Messages that pop up when you win and lose
-Automated reset timer when you lose
- customized LED light patterns when you win and when you lose
- Extensive code, majoring with "if" statements.
 
Remove these adsRemove these ads by Signing Up

Step 1: Code

int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the button state
int buttonPresses = 0;          // how many times the button has been pressed
int ledPin = 13;
int ledPin1 = 12;
int ledPin2 = 11;
int ledPin3 = 10;
int ledPin4 = 9;
long interval = 100;
long previousMillis = 0;
float startTime;
float elapsedTime;
void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  if (val != buttonState) {          // the button state has changed!
    if (val == LOW) {                // check if the button is pressed
      buttonPresses++;               // increment the buttonPresses variable
      Serial.print("Button has been pressed ");
      Serial.print(buttonPresses);
      Serial.println(" times");

      if (buttonPresses==1) {
        (startTime = millis());
      }
      else if (buttonPresses>1) {
        elapsedTime = millis() - startTime;

        Serial.print(  (elapsedTime / 1000));
        Serial.println(".");
      }
    }
  }
  if (elapsedTime>15000)
  {
    buttonPresses=0;
    elapsedTime=0;
    Serial.print("You Lose");
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin1, LOW);
    digitalWrite (ledPin2, LOW);
    digitalWrite (ledPin3, LOW);
    digitalWrite (ledPin4, LOW);
    delay(10000);
  }
  if (buttonPresses==100)
  {
    (buttonPresses=0);
    elapsedTime=0;
    Serial.print("You WIN!!");
    digitalWrite (ledPin, HIGH);
    delay(100);
    digitalWrite (ledPin1, HIGH);
    delay(100);
    digitalWrite (ledPin2, HIGH);
    delay(100);
    digitalWrite(ledPin3, HIGH);
    delay(100);
    digitalWrite (ledPin4, HIGH);
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin1, LOW);
    digitalWrite (ledPin2, LOW);
    digitalWrite (ledPin3, LOW);
    digitalWrite (ledPin4, LOW);
    delay(2300);
    digitalWrite (ledPin, HIGH);
    digitalWrite (ledPin1, HIGH);
    digitalWrite (ledPin2, HIGH);
    digitalWrite (ledPin3, HIGH);
    digitalWrite (ledPin4, HIGH);
    delay(5000);
    digitalWrite (ledPin, LOW);
    digitalWrite (ledPin1, LOW);
    digitalWrite (ledPin2, LOW);
    digitalWrite (ledPin3, LOW);
    digitalWrite (ledPin4, LOW);
    delay(2300);
  }


  if (buttonPresses==10){
    pinMode(ledPin, OUTPUT);
    digitalWrite (ledPin, HIGH);
  }
  if (buttonPresses==20) {
    pinMode(ledPin1, OUTPUT);
    digitalWrite (ledPin1, HIGH);
  }
  if (buttonPresses==30) {
    pinMode (ledPin2, OUTPUT);
    digitalWrite (ledPin2, HIGH);
  }
  if (buttonPresses==40){
    pinMode(ledPin3, OUTPUT);
    digitalWrite (ledPin3, HIGH);
  }
  if (buttonPresses==50) {
    pinMode(ledPin4, OUTPUT);
    digitalWrite (ledPin4, HIGH);
  }
  if (buttonPresses==60){
    pinMode(ledPin, OUTPUT);
    digitalWrite (ledPin, LOW);
  }
  if (buttonPresses==70) {
    pinMode(ledPin1, OUTPUT);
    digitalWrite (ledPin1, LOW);
  }
  if (buttonPresses==80) {
    pinMode (ledPin2, OUTPUT);
    digitalWrite (ledPin2, LOW);
  }
  if (buttonPresses==90){
    pinMode(ledPin3, OUTPUT);
    digitalWrite (ledPin3, LOW);
  }
  if (buttonPresses==100) {
    pinMode(ledPin4, OUTPUT);
    digitalWrite (ledPin4, LOW);
  }
  if (buttonPresses==100) {
    buttonPresses=0;
  }

  buttonState = val;                 // save the new state in our variable
}
doug2.01 year ago
maybe if it was 15500 millis mike could maybe beat the game
pfred21 year ago
Here I thought this article was about how to have a breakdown. I've been considering it lately ...
You could spif this up a lot if you rotated the photos, it is not too late. Also if you properly format the code more people will read it. If you really want people to try this provide a source code download. The above should give you a much better instructable.