Whack a mole

Hey, so for a project I'm working on currently, I have 3 LEDs which will blink. It's always random which one turns on and also for how long. I also have 3 buttons, 1 for each LED. The purpose of the project is the same concept as whack a mole. When aLED turns on, you press the button that belongs to the LED and you get a point. It's all set up and done but I can't get the last part done. How do I make the user gain a point when the LED is on and the right button is pushed? My problem is that I can't get how to know which LED turned on.

Palmboom333:
My problem is that I can't get how to know which LED turned on.

You could use digitalRead() on the led pin, even though the pin is set as OUTPUT. That will tell you if it is HIGH or LOW.

Thanks for your answer! This is what I thought about too. I tried it and I still can't get it to work.

if (digitalRead(9) == HIGH)
{
a++;
lcd.setCursor(3, 1);
lcd.print(a);
delay(200);
}

This is what it looks like right now but it doesn't work, I have no idea what goes wrong...

Dear Arduino Team,

I have problems finding data sheets for the components supplied with the Ardoino starter Kit. Furthermore, the delivered transistor is not suitable for the use of USB (5 Volt).

With kind regards

Tiddel

Palmboom333:
My problem is that I can't get how to know which LED turned on.

How can you not know? Isn't it your code that's turning on the LED? Perhaps you should post the code.

gfvalvo:
How can you not know? Isn’t it your code that’s turning on the LED? Perhaps you should post the code.

int ranNum;
int ranDel;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a = 0;
int e = 7;

void setup() {
// Seed RNG from analog port.
randomSeed(analogRead(0));
// Setup 8 output ports for LED's

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

 // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("De mol");
  pinMode(e,INPUT);
  
}
void loop() {
    //Generate random number between 8 and 10
    ranNum=random(8,11);
    // Generate random delay time
    ranDel=random(500,1001);
    //Turn on the LED
    digitalWrite(ranNum, HIGH);
    delay(ranDel);
    //Turn off the LED
    digitalWrite(ranNum, LOW);  

    int button = digitalRead(e);
  
  
    lcd.setCursor(3, 1);
    lcd.print(a);
  
  
  if (digitalRead( 8 ) == HIGH)
  {
    a++;
    lcd.setCursor(3, 1);
    lcd.print(a);
    delay(200);
  }
}

Go to Topic: How to use this forum - please read.

Read it, especially Item #7 on how to PROPERLY post your code.

how long does your LED stay lit when you run the sketch ?

is length of time for the LED to be lit part of the project ?

I would think it would be easy enough to use a global variable for

voidLoop {
    currentLED = ranNum ;  // stores the value for use in the next bit

ReadButtons()

. . . . .

void ReadButton {
  digitalWrite(currentLED, HIGH);
    delay(ranDel);
    //Turn off the LED
    digitalWrite(CurrentLED, LOW); 

//  check for your inputs here

this way, you go to the part of your sketch that turns the LED's on and off and reads your buttons, but while in that loop, you only have the one LED variable to work with and all checking would be with that one LED.
any other button press would be a miss.

it does not currently offer a time to wait for a button press.
and you would need to move all your button press testing over to that bit.

I am assuming this is a school project, so we typically offer minimal guidance to get you past a mental block and not actually do your work for you.
in the spirit of your actually learning, I hope this helps.

Try this…
I’ve used pins 5,6 and 7 as inputs for your pushbuttons. I don’t know if you are pulling the pins down or up with an external resistor. If you’re not you should use INPUT_PULLUP instead of INPUT. And invert the digitalReads by putting an exclamation mark in front of it, like this: !digitalRead(button);

Always try to avoid using delay() in your code.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttons[3] = {5,6,7};
const int leds[3] = {8,9,10};

int ranLed;
unsigned long ranDelay = 500;

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;

bool ledState = false;

int i = 0;

int score;

void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("De mol");
  
  for(int i=0; i<3; i++)
  {
    pinMode(leds[i], OUTPUT);
    pinMode(buttons[i], INPUT);
  }

  randomSeed(analogRead(0));
}

void loop()
{
  ranLed = random(0,3);
  ranDelay = random(500,1001);

  lcd.setCursor(1,0);
  lcd.print("Score:");
  lcd.setCursor(1,8);
  lcd.print(score);

  while(i < 1)
  {
    currentMillis = millis();
    if(currentMillis - previousMillis > ranDelay)
    {
      previousMillis = currentMillis;
      ledState = !ledState;
      digitalWrite(leds[ranLed], ledState);
      i++;
    }
    if(ledState)
    {
      for(int j=0; j<3; j++)
      {
        if(digitalRead(buttons[j]))
        {
          if(j == ranLed)
          {
            score++;
          }else{
            score--;
          }
        }
      }
    }
  }
  i = 0;
}

Thanks for all the help but I still can’t get it to work. I took a step back and just cut out the button part for now. This is what my code looks like right now. I want to add 1 point to the score whenever the LED connected with pin 8 is high. Does anybody knows what goes wrong here? The LEDS change exactly how I want it but the score stays 0.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ranNum;
int ranDel;
int score = 0;
void setup() {

// Setup 8 output ports for LED's
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
}
    
void loop() {
    //Generate random number between 8 and 10
    lcd.begin(16, 2);
    lcd.print("De mol");
    lcd.setCursor(3, 1);
    lcd.print(score);
    
    ranNum = random(8, 11);
    // Generate random delay time
    ranDel = random(500, 1000);
    //Turn on the LED
    digitalWrite(ranNum, HIGH);
    delay(ranDel);
    //Turn off the LED
    digitalWrite(ranNum, LOW);  

    if (digitalRead(8))
    {
      score++;
      lcd.setCursor(3, 1);
      lcd.print(score);
    }
}

You pick a random led pin. Let's assume that it's pin 8.

You turn it on.
You delay for a bit.
You turn it off.
You check to see whether it is on so you can increment the score.

It should be no surprise that score is and forever more will be zero.

wildbill:
You pick a random led pin. Let's assume that it's pin 8.

You turn it on.
You delay for a bit.
You turn it off.
You check to see whether it is on so you can increment the score.

It should be no surprise that score is and forever more will be zero.

did grasp the concept in post 7 of using a global and to go to a new void() to do your testing ?
assign a global varaible the value of your randdome LED pin.
jump to a new void with that global varibel.
use that global variable as if it were fixed
lite the LED, test for a button
then return if is good and add a point
return if it is not good, no point
repeat.

wildbill:
You pick a random led pin. Let's assume that it's pin 8.

You turn it on.
You delay for a bit.
You turn it off.
You check to see whether it is on so you can increment the score.

It should be no surprise that score is and forever more will be zero.

Thanks for this, I'm still a noob so thanks for explaining!

The crux of the matter is your use of delay. Look at Robin2's sticky thread at the top of the forum which explains how to use millis. You're going to need to do something similar to control when the led goes off. Then you can check for button presses while the led is on.

dave-in-nj:
did grasp the concept in post 7 of using a global and to go to a new void() to do your testing ?
assign a global varaible the value of your randdome LED pin.
jump to a new void with that global varibel.
use that global variable as if it were fixed
lite the LED, test for a button
then return if is good and add a point
return if it is not good, no point
repeat.

Thank you for taking your time to help me and being patient! I appreciate it!

wildbill:
The crux of the matter is your use of delay. Look at Robin2's sticky thread at the top of the forum which explains how to use millis. You're going to need to do something similar to control when the led goes off. Then you can check for button presses while the led is on.

Will take a look at that, thanks!