Arduino whack-a-mole game not working Everyone, I am new to this. Currently I am trying to make a Whack-a-mole game for my school project. There will be a total of 6 moles (corresponding to 6 LEDs, which will light up randomly). At each moment, there will only be one LED lit up. The whole game duration is 30 seconds. At the end of the game, the score will be displayed.

I use a piezo electric sensor to detect the knock of the user. I program it in such a way that if the reading of the piezo sensor is greater than 50, it will take it as knock, and if the user knocks it correctly, one point will be awarded.

But unfortunately my code isn't working. The score displayed appears to be random numbers, doesn't work as it is supposed to be.

Here is my code, please hep me to have a look and provide me with some guidance. Thank you.

unsigned long time_since_reset;
int previousNum=0;
int currentNum =0;
int randomGenerator (void);
void light(int port);
int checker(int port);
int score =0;

void setup() {
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  time_since_reset = millis();
  while((millis()-time_since_reset)&lt;30000) {
    randomSeed(analogRead(0));
    currentNum = randomGenerator();
    light(currentNum);
    checker(currentNum);
  }
  Serial.println("End Game");
  Serial.println(score);
}

int randomGenerator(void) {
  int cNum;
  cNum = random(3, 8);
  if(cNum!= previousNum) {
    return cNum;
  }
}

void light(int port) {
  digitalWrite(port,HIGH);
  delay(1000);
}

int checker(int port) {
  jump:
  int mole1 = analogRead(0);
  int mole2 = analogRead(1);
  int mole3 = analogRead(2);
  int mole4 = analogRead(3);
  int mole5 = analogRead(4);
  int mole6 = analogRead(5);
  while(mole1&lt;10 &amp;&amp; mole2&lt;10 &amp;&amp; mole3&lt;10 &amp;&amp; mole4&lt;10 &amp;&amp;mole5&lt;10 &amp;&amp;mole6&lt;10) {
    goto jump;
  }
  if(mole1&gt;50 &amp;&amp; port == 3) {
    score++;
    digitalWrite(port, LOW);
  }
  if(mole2&gt;50 &amp;&amp; port == 4) {
    score++;
    digitalWrite(port, LOW);
  }
  if(mole3&gt;50 &amp;&amp; port == 5) {
    score++;
    digitalWrite(port, LOW);
  }
  if(mole4&gt;50 &amp;&amp; port == 6) {
    score++;
    digitalWrite(port, LOW);
  }
  if(mole5&gt;50 &amp;&amp; port == 7) {
    score++;
    digitalWrite(port, LOW);
  }
  if(mole6&gt;50 &amp;&amp; port == 8) {
    score++;
    digitalWrite(port, LOW);
  }
}
The code as shown has a bunch of problems; the probably-most-important are as follows.

• score is not initialized.  Add score = 0; after 
 time_since_reset = millis();

• As noted in a comment, the delay(1000) statement in light() prevents checking for whacks during the first second of a light being on.  This probably is undesirable – most people expect to whack a mole much sooner than a second after it appears.  If you want a delay there, make it at most 20 milliseconds. [See edit 2]

• The sequence randomSeed(analogRead(0)); currentNum = randomGenerator(); has a good chance of generating the same value of currentNum several times in a row, which may result in multiple counts for a given whack. 

• Instead of saying randomSeed(analogRead(0)); within the inner loop, say it just after time_since_reset = millis(); or (probably better) in setup().  If analogRead(0) changes slowly, the same random seed may get used over and over, resulting in the same sequence of numbers from the random(3,8) expression.  

• Your randomGenerator() routine has undefined behavior:  It doesn't return a valid result if the current random  number is the same as the previous one.  You could add a loop that calls random() again if the value is the same as before; but not returning a value results in  undefined behavior.

Edit 2:  When you turn a light on, record the current millis() time reading, say in onTime[i] for light i, and set flag isOn[i] true.  Later, when isOn[i] &amp;&amp; (millis()-onTime[i] &gt; targetDelay[i]), turn the light off and clear the flag (via isOn[i] = false).  Within checker(), a condition like port == 6 translates to isOn[6], an expression which is true when light 6 is on, else false.

The approach of using arrays like onTime[] and isOn[] and targetDelay[], variable subscripts, and recorded on-times has several benefits:  It lets you loop through lights, getting rid of that needlessly repetitious code in checker(); it enables other processing in the main game loop (such as whack-testing) to proceed independently of light control; it allows multiple moles to pop up at the same time; and it allows different moles to have different or varying up times.