Count if button pressed while LED is on

I am retired and trying to learn something new so I am a rookie at this. I am trying to create a game that if you press a button while the LED is on it counts as one point and continues to add points as you continue the game. I would like to add additional lights and buttons but need to get just one to work for now. It will count with just the if(val==HIGH) but not with the && led1==HIGH added. Any kind of advice would be helpful.
Thank you

#include <LiquidCrystal.h>
int led1=10;
int button=A5;
int val;
int count=1;
int press; 
int Y;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2);
  pinMode(button,INPUT);
  
}


void loop() {
  
  digitalWrite(led1,LOW);
  delay(1500);
  digitalWrite(led1,HIGH);
  delay(1500);
  
  val=digitalRead(button);
  
   if(val==HIGH && led1==HIGH){
    press=count++;
    Y=press;
    delay(250);
  }

  lcd.setCursor(0, 0);
  lcd.print("Points");
  lcd.setCursor(0, 1);
  lcd.print(Y);

}

Led1 is a variable you’ve defined, with a value 10. It appears to be the pin number of the LED. That variable will never ==HIGH. I think what you want is a different variable, you might call it “ledValue” which you start out setting to HIGH, and that’s what you check. And you’ll want some logic that sets it to LOW from time-to-time (and writes to the physical LED) or your game’s going to be pretty boring! Hope that helps

Step yourself through the code.

You turn off the LED and do nothing for awhile
You turn in the LED and do nothing for awhile

Then you very rapidly do all the other work there is to the loop() function, as fast as that might be.

Rinse and repeat.

Your game idea, nice first project yeah, is a bit more complicated, as you must "open the window" and be able count the button presses whilst waiting for it to be time to close the window.

That is programming without dirt naps delay() which begins with "blink without delay", google and examine the many ways keys have been presented that promise to unlock this one door. No matter anything else, it implies that it is an important one. Door.

    blink without delay Arduino

Perhaps then for some time nothing happens until round 2, like.

Sry, I mean in game play - random periods of on and off. Penalize ringing in when it is dark &c.

a7

Your code, slightly modified...

#include <LiquidCrystal.h>
constexpr byte led1 = 10;
constexpr byte button = A5;
int val;
int points = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

constexpr unsigned long BLINK_DELAY {1500};
unsigned long delayStart;

void setup() {
  lcd.begin(16, 2);
  pinMode(button, INPUT);
  pinMode(led1, OUTPUT);
  delayStart = BLINK_DELAY;
}


void loop() {
  static bool isLedOn {false};
  static bool doCount {false};

  if (millis() - delayStart > BLINK_DELAY) {
    delayStart = millis();
    (isLedOn = !isLedOn) ? digitalWrite(led1, HIGH) : digitalWrite(led1, LOW);
    doCount = (val == HIGH) ? false : true; // A continuous keystroke must not increase the counter. 
  }
  val = digitalRead(button);
  if (val == HIGH && isLedOn == true && doCount == true) {
    ++points;
    doCount = false;
    lcd.setCursor(0, 0);
    lcd.print("Points");
    lcd.setCursor(0, 1);
    lcd.print(points);
  }
}

maybe a bit simple (no LCD, macros, ternary)

int led1   = 10;
int button = A5;

byte butState;

bool press;
int  count;

enum { Off = LOW, On = HIGH };

const unsigned long MsecPeriod = 1500;
unsigned long msecLst;

void loop ()
{
    unsigned long msec = millis ();

    // toggle led every MsecPeriod
    if (msec - msecLst >= MsecPeriod)  {
        msecLst = msec;
        if (On == digitalRead (led1))  {
            digitalWrite (led1, Off);
        }
        else  {
            digitalWrite (led1, On);
            press = false;              // allow next count
        }
    }

    // check for button press
    byte but = digitalRead (button);
    if (butState != but)  {     // change in state
        butState = but;
        delay (20);             // debounce

        // button pressed, not pressed already, led On
        if (LOW == but && ! press && On == digitalRead (led1))  {
            press = true;
            count ++;
            Serial.println (count);
        }
    }
}

void setup ()
{
    Serial.begin (9600);

    pinMode (led1,   OUTPUT);
    pinMode (button, INPUT_PULLUP);
    butState = digitalRead (button);
}

Thank you Kai-R and gcjr,
Both of those suggestions work great. My bigger plan is to make this into a nerf target game for the grandkids with 3 or 4 targets and only counting the hit when they light up. I would like the lights to blink random. I have a bit to go but this is a great start.

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