-5

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 serial print output

9
  • 1
    It'll even notice that push button is pressed? I wonder how - the only reading is in pushbutton_value initialization. Also if you have INPUT_PULLUP, you should be checking for LOW as button should be switching ground.
    – KIIV
    Dec 3, 2016 at 10:44
  • LOW not working too
    – abc
    Dec 3, 2016 at 10:48
  • Well, more specifically: Where do you read the current state of the push button?
    – KIIV
    Dec 3, 2016 at 10:55
  • To the pushbutton_value variable
    – abc
    Dec 3, 2016 at 10:56
  • And when this digitalRead happens?
    – KIIV
    Dec 3, 2016 at 10:57

1 Answer 1

0

I wonder why you initialized score as byte! Change it to int. As you can see when you Serial Print your score value doesn't cross 256 because the largest value a byte can have is 256 but you are summing score to 500

5
  • Hmm after I did this It still isnt working
    – abc
    Dec 3, 2016 at 11:02
  • digitalread your pushbutton_value in the loop, and whats was the output before..its still not crossing 256? Dec 3, 2016 at 11:04
  • it is 0 now. What should I dp?
    – abc
    Dec 3, 2016 at 11:29
  • Could you please spoon feed me the code
    – abc
    Dec 3, 2016 at 11:30
  • How to predict the score without counting the number of pushbutton_value==HIGH recorded for the x==winner_led and x==led_one events ? Add 2 integers counters to follow those events.
    – J. Piquard
    Dec 3, 2016 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.