1

I am a beginner with c and arduino and am trying to create a 'whack-a-mole' style game. I am having problems with the 'score' variable. At the moment, I am just trying to get it to print to the Serial Monitor. There are no errors when I verify it but nothing shows up when it is uploaded. Could somebody please help me understand what is going wrong and inform me of how I can fix it. Here's my code:

int led1Pin = 11;  
int led2Pin = 10;  
int led3Pin = 4;  
int led4Pin = 2;  
int button1pin = 13;  
int button2pin = 8;  
int button3pin = 12;  
int button4pin = 7;  

float highLED = 0;  

int score = 0;  

int exitSubLoop = 0;  

byte leds = 0;  

void setup()   
{  
  pinMode(led1Pin, OUTPUT);  
  pinMode(led2Pin, OUTPUT);  
  pinMode(led3Pin, OUTPUT);  
  pinMode(led4Pin, OUTPUT);  
  pinMode(button1pin, INPUT_PULLUP);    
  pinMode(button2pin, INPUT_PULLUP);    
  pinMode(button3pin, INPUT_PULLUP);    
  pinMode(button4pin, INPUT_PULLUP);  
  digitalWrite(led1Pin, LOW);  
  digitalWrite(led2Pin, LOW);  
  digitalWrite(led3Pin, LOW);  
  digitalWrite(led4Pin, LOW);  
}  

void loop()   
{  
  Serial.println(score, DEC);  
  
  exitSubLoop = 1;  
  
  highLED = random(1, 5);  
  
  if(highLED == 1){  
    digitalWrite(led1Pin, HIGH);  
  }  
  if(highLED == 2){  
    digitalWrite(led2Pin, HIGH);  
  }  
  if(highLED == 3){  
    digitalWrite(led3Pin, HIGH);  
  }  
  if(highLED == 4){  
    digitalWrite(led4Pin, HIGH);  
  }  

  while(exitSubLoop != 0){  
    
  if ((digitalRead(button1pin) == LOW) && (highLED == 1))  
  {  
    digitalWrite(led1Pin, LOW);  
    score++;  
    exitSubLoop--;  
      
  }  
  if ((digitalRead(button2pin) == LOW) && (highLED == 2))  
  {  
    digitalWrite(led2Pin, LOW);  
    score++;  
    exitSubLoop--;  
 
  }  
  if ((digitalRead(button3pin) == LOW) && (highLED == 3))  
  {  
    digitalWrite(led3Pin, LOW);  
    score++;  
    exitSubLoop--;  
      
  }  
  if ((digitalRead(button4pin) == LOW) && (highLED == 4))  
  {  
    digitalWrite(led4Pin, LOW);  
    score++;  
    exitSubLoop--;  
    
  }   



  if ((digitalRead(button1pin) == LOW) && (highLED != 1))  
  {  
      score--;  
    }  
    if ((digitalRead(button2pin) == LOW) && (highLED != 2))  
    {  
      score--;  
    }  
    if ((digitalRead(button3pin) == LOW) && (highLED != 3))  
    {  
      score--;  
    }  
    if ((digitalRead(button4pin) == LOW) && (highLED != 4))  
    {  
      score--;  
    }  
    delay(200);  
  }  
}  

P.S I don't know whether it makes any difference but I am using the Arduino UNO

  • Serial.begin() is missing in setup – chrisl Jul 24 at 8:46
  • What a silly mistake :D Thanks! – William Pennanti Jul 24 at 8:49
  • 2
    I’m voting to close this question because the problem is trivial. So trivial that it is hard to find specific keywords to search for a duplicate. – Juraj Jul 24 at 11:30
  • Side note: you want to get used to arrays. Variable names like led1Pin, led2Pin etc. let me believe this is a good idea. Your code will become about 4x smaller, which is 4x less error prone. The principle behind this is called DRY, don't repeat yourself. Just think you had 20 buttons. Would you like writing the same code 20 times? – Thomas Weller Jul 24 at 13:27