Arduino Serial Moniter Keeps Saying 10

Hi I’ve got a problem with my code or Arduino, not sure which but it keeps saying 10 in the serial moniter even though its not meant to go above 4. I think I might have written the code wrong and if any one could help me it would be great. I am trying to make a program where the light flashes and you have to press the button when it flashes and there are multiple buttons. I’ve attached the code. I’m also using a sound attatchment which is there with the code.

P.S I have an Arduino uno

Tone3.ino (2.8 KB) (9.52 KB)

@CSMArduino, do not cross-post. Other thread removed.

Thread moved.

  int score = score;

Create a local variable with a value equal to itself. What the heck do you think score will equal? It will NOT equal the value of the local variable in setup() as that variable has gone out of scope. There is no global variable with that name, so you haven't a clue what value score will be.

You never change the value of score, so it is silly to print garbage later on.

    while(light_no == 5)
      if(digitalRead(BUTTON_C) == HIGH)
        light_no = random(4);

Since light_no is assigned a random value in the range 0 to 3 (the 4 is NEVER returned), this code will never be executed, so you might as well delete it.

Why are you so hung up on using while loops? It looks like you need to let loop() actually loop, and use if statements, instead of while loops.

Thanks soo much. I'm new to Arduino so this helps a lot. Also I only declared score as score so it wouldn't say it wasn't declared.

Also I only declared score as score so it wouldn't say it wasn't declared.

You have to declare a variable in the correct place. If it is declared outside all functions it will be a global variable visible everywhere. If it is declared inside a function it will be a local variable and only visible within that function.

It is possible (but not recommended) to create a local variable with the same name as a global variable. Because they are quite separate that can cause no end of confusion.