I made a skill stop game from Arduino Uno and a pushbutton.Here is the code:
byte led_one=2;
byte led_two=3;
byte winner_led=4;
byte led_four=5;
byte led_five=6;
byte pushbutton=12;
byte score=0;
int pushbutton_value=digitalRead(pushbutton);
void setup()
{
// put your setup code here, to run once:
pinMode(led_one,OUTPUT);
pinMode(led_two,OUTPUT);
pinMode(winner_led,OUTPUT);
pinMode(pushbutton,INPUT_PULLUP);
pinMode(led_four,OUTPUT);
pinMode(led_five,OUTPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
for (int x=1;x<=6;x++)
{
digitalWrite(x,HIGH);
delay(250);
digitalWrite(x,LOW);
delay(250);
if(pushbutton_value==HIGH && x==winner_led)
{
score+=500;
}
else if(pushbutton_value==HIGH && x==led_one)
{
score+=100;
}
}
Serial.println(score);
}
My Problem is that Whenever I push the button The Serial Monitor is printing out the wrong score
pushbutton_value
initialization. Also if you haveINPUT_PULLUP
, you should be checking forLOW
as button should be switching ground.