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
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 napsdelay() 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.
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.