Whack a Mold Game Not Reading Presses Fast Enough

Hello,

I am new to Arduino and was hoping that I could get some help with my project for school. I decided to make a whack-a-mole game using LEDs and buttons. I modified a Whack-a-mole code from the University of Wyoming to change it from two buttons to five. I also added a potentiometer to act as a difficulty dial, with the value of the sensorValue corresponding to the difficulty. I have run into the problem that if you hit the button corresponding to the light that is on too fast, the press will not register. I have an LED that lights up if you press the correct button. If you press the correct button too fast, the LED doesn't turn on.

Here is my code

/*
 * this is a 2-mole whack a mole game
 */

//Declare your variables here
int mole1LED = 12;      //LED for first mole
int mole2LED = 10;      //LED for second mole
int mole3LED = 8;
int mole4LED = 6;
int mole5LED = 4;
int mole1Switch = 13;  //mole 1 switch
int mole2Switch = 11;   //mole 2 switch
int mole3Switch = 9;
int mole4Switch = 7;
int mole5Switch = 5;
int successLED = 3; //LED if successful mole whack
int failLED = 2;    //LED if miss mole
int easyLED = 14; //easy LED
int mediumLED = 15; 
int hardLED = 16;



void setup() {
  pinMode(mole1LED, OUTPUT);
  pinMode(mole2LED, OUTPUT);
  pinMode(mole3LED, OUTPUT);
  pinMode(mole4LED, OUTPUT);
  pinMode(mole5LED, OUTPUT);
  pinMode(mole1Switch, INPUT_PULLUP);
  pinMode(mole2Switch, INPUT_PULLUP);
  pinMode(mole3Switch, INPUT_PULLUP);
  pinMode(mole4Switch, INPUT_PULLUP);
  pinMode(mole5Switch, INPUT_PULLUP);
  pinMode(successLED, OUTPUT);
  pinMode(failLED, OUTPUT);
  pinMode(easyLED, OUTPUT);
  pinMode(mediumLED, OUTPUT);
  pinMode(hardLED, OUTPUT);
  randomSeed(millis());     //initialize random number generator
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability
  // read potentiometer to deteremine difficulty led
  if (sensorValue >= 683) {  //easy LED turn on
    digitalWrite(easyLED, HIGH);
  } else {
    digitalWrite(easyLED, LOW);
    }
  if (sensorValue <= 682 && sensorValue >= 342) {  //medium LED turn on
    digitalWrite(mediumLED, HIGH);
  } else {
    digitalWrite(mediumLED, LOW);
    }
  if (sensorValue <= 341) {  //easy LED turn on
    digitalWrite(hardLED, HIGH);
  } else {
    digitalWrite(hardLED, LOW);
  }

  if (random(5)<<0) {
    if (random(5)==1) {  //Flip Coin: random returns value 1.  
      moleWhack(mole1Switch, mole1LED);  //Mole is at position 1
    }
    if (random(5)==2){   //if its value 2
      moleWhack(mole3Switch, mole3LED);  //Mole is at postiion 3
    }
    if (random(5)==3){   //if its value 3
      moleWhack(mole4Switch, mole4LED);  //Mole is at postiion 4
    }
    if (random(5)==4){   //if its value 4
      moleWhack(mole5Switch, mole5LED);  //Mole is at postiion 5
    }
  }
  else {              //if its vale 0 mole is at position 2
    moleWhack(mole2Switch, mole2LED);
  }
  delay(random(1000, 2000));  //wait a random time before a new mole
}

void moleWhack(int moleSwitch, int moleLED) {
  //mole is at position given by moleSwitch and moleLED
  //check if mole gets whacked
  digitalWrite(moleLED, HIGH);  //turn on the mole LED
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  // read potentiometer to deteremine difficulty
  if (sensorValue >= 683) {  //easy 
    delay(900);
  } 
  if (sensorValue <= 682 && sensorValue >= 342) {  //medium 
    delay(500);
  } 
  if (sensorValue <= 341) {  //hard
    delay(300);
  } 
  long startTime = millis();      //save the start time
  while (millis() - startTime < sensorValue) { //stay in loop until timer expires
    if (digitalRead(moleSwitch) == LOW) { //pushed=whacked!
      digitalWrite(successLED, HIGH);  //blink on
      digitalWrite(moleLED, LOW);      //turn off mole LED. He's been whacked
      delay(200);
      digitalWrite(successLED, LOW);   //blink off
      delay(200);
      digitalWrite(successLED, HIGH);
      delay(200);
      digitalWrite(successLED, LOW);
      delay(200);
      digitalWrite(successLED, HIGH);
      delay(200);
      digitalWrite(successLED, LOW);
      return;  //this terminates the function
    } else {
      digitalWrite(moleLED, LOW);           //turn off mole light to prepare for next mole
      digitalWrite(failLED, HIGH);          //blink on
      delay(200);                           //blink delay
      digitalWrite(failLED, LOW);           //blink off
      delay(200);
      digitalWrite(failLED, HIGH);          //blink on
      delay(200);                           //blink delay
      digitalWrite(failLED, LOW);           //blink off
      delay(200);
      digitalWrite(failLED, HIGH);          //blink on
      delay(200);                           //blink delay
      digitalWrite(failLED, LOW);           //blink off
      delay(200);
      digitalWrite(failLED, HIGH);          //blink on
      delay(200);                           //blink delay
      digitalWrite(failLED, LOW);           //blink off
      return;
    }
  }
  //Timed out - If we get here then mole was not whacked
  digitalWrite(moleLED, LOW);           //turn off mole light to prepare for next mole
  digitalWrite(failLED, HIGH);          //blink on
  delay(200);                           //blink delay
  digitalWrite(failLED, LOW);           //blink off
  delay(200);
  digitalWrite(failLED, HIGH);          //blink on
  delay(200);                           //blink delay
  digitalWrite(failLED, LOW);           //blink off
  delay(200);
  digitalWrite(failLED, HIGH);          //blink on
  delay(200);                           //blink delay
  digitalWrite(failLED, LOW);           //blink off
  delay(200);
  digitalWrite(failLED, HIGH);          //blink on
  delay(200);                           //blink delay
  digitalWrite(failLED, LOW);           //blink off
  
}

Are you aware delay( ) stops normal code execution while you are waiting ?

Welcome to the forum.

There are 24 calls to delay() in the sketch, and most of them have to be removed. That means you have to re-write the sketch.
This is a starting point: https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

Let the loop() run hundreds or thousands times per second. In the loop(), check if something needs to be done. That means you may not use a while-statement to wait for something.

I am missing a overall good structure of the code.
If you can explain in words what it should do, then half the sketch is already written :nerd_face:

Hello toogit

Use the Forum search engine and ask for 'whack-a-mole game'.

When you start adding numbers to the variable names it usually means it is time to use an array instead.

Thank you for the link. It was very helpful!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.