Button problem in my while loop

for a school project i'm trying to set up a timer as a highscore system where it counts down from 24000 ack to 0 en whatever's left when they finish the race'll be their score, which is the easy part. now what i try to is make them run from a box with a button and some LEDs to the next box and every time they press the button one more LED will light up on the boxes while in the meantime the timer is running.

this is a simple version of the code I used:

if (button1 is pressed ){
timer = 24000;
digitalWrite(LED1, HIGH);
}

while (timer > 0) {
if (button 2 is pressed) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}

if (button 2 etc.){
}

if (last button is pressed){
timer = score;
HighScore[pos] = score
++pos
digitalWrite all the LEDs
}

else {
--timer;
delay(2);
}

this is all written in the loop but my problem is that when the while loop starts my buttons (well at least button 2 never got it further than that) just gets stuck at HIGH at all times.. works perfectly fine before the loop begins though so it isn't a hardware problem.

I would really appreciate your help guys, I'm stumped

Here, we prefer to see your code. Explanations are nice, but it is important to see the real code, also.

To post:

  1. Use CTRL-T in the Arduino IDE to autoformat your code.
  2. Paste the autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.

ZeyvGaming wrote:

while (timer > 0) {
if (button 2 is pressed) {
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
}

You do realize that this is probably an infinite loop? Unless timer is declared volatile and handled in an ISR (which I cannot be certain of because I cannot see the code), this loop will never end.

I don’t understand your intent, so I cannot rewrite the code. One possibility is that you intended

code]
if ((timer>0) && (button 2 goes from unpressed to pressed)) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
[/code]

I cannot see whether the code is within setup() or within loop(), so I cannot be of much help.

I've found that 75% or more of "my button doesn't work the way I want" problems are because the button is not sewn on properly. How IS your switch wired?

This would be all of the code, seen as it’s a bit long with all the LEDs going on an off i thought it was easier to not show the whole thing but if it help i’d gladly put it up.

—UPDATE, uploaded the wrong code here, it has been updated as of 9.14 25-04-2016

#include <Servo.h>

//--------------------------------------=all the variables=------------------------
int Pin_Butt1 = 2;
int Pin_Butt2 = 3;

const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;

const int LED4 = 10;
const int LED5 = 9;
const int LED6 = 8;

Servo Servo1;
Servo Servo2;

const int Pin_Servo1 = 7;
const int Pin_Servo2 = 6;

int Direct[] = {0, 180};

int timer;
int score;
int Hscores[100];
int pos;
int x;

long ran;

//----------------------------------------------------------------=the set-up=---------------------------
void setup() {
  Serial.begin(9600);

//--------------------------------------=assign the pins=----------------------
  pinMode(Pin_Butt1, INPUT);
  pinMode(Pin_Butt2, INPUT);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);

//--------------------------------------=Set up the servo motors
  Servo1.attach(Pin_Servo1);
  Servo1.write(90);

  Servo2.attach(Pin_Servo2);
  Servo2.write(90);
}

//------------------------------------------------------------=The Loop=----------------------------
void loop() {

//--------------------------------------=Variable set up=-------------------------
  int Butt1_Value = digitalRead(Pin_Butt1);
  int Butt2_Value = digitalRead(Pin_Butt2);

  int LED1_Value = digitalRead(LED1);
  int LED2_Value = digitalRead(LED2);
  int LED3_Value = digitalRead(LED3);

  int LED4_Value = digitalRead(LED4);
  int LED5_Value = digitalRead(LED5);
  int LED6_Value = digitalRead(LED6);

  ran = random(2);
  
  timer = 0;
  score = 0;

//--------------------------------------=IF statement #1=----------------------
  if(Butt1_Value == LOW && LED1_Value == LOW){
    timer = 24000;

    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    Servo1.write(Direct[ran]);
    delay(2000);
    Servo1.write(90);
  }

  while (timer > 0) {
    //-------------------------------=IF statement #2=----------------------
    if(Butt2_Value == LOW && LED1_Value == HIGH){
   
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      Servo2.write(Direct[ran]);
      delay(2000);
      Servo2.write(90);
    }

    //---------------------------------=IF statement #3=----------------------
    if(Butt1_Value == LOW && LED2_Value == HIGH){
      score = timer;
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);

      Hscores[pos] = score;
      ++pos;
      if (pos == 91) {
        pos = 0;
      }

      Serial.println("Congratulations, You made it!");
      Serial.println("Your score is:");
      Serial.println(score);
      delay(500);

      Serial.println("Here are the 10 latest scores:");
      for(x=0+pos; x<10+pos; ++x){
        Serial.println(Hscores[x]);
      }
    
  //----------------=LOW=----------------
    digitalWrite(LED1, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED6, LOW);
    delay(100);
  //----------------=HIGH=----------------
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);

    digitalWrite(LED3, HIGH);
    digitalWrite(LED6, HIGH);
    delay(100);
  //----------------=LOW=----------------
    digitalWrite(LED1, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED6, LOW);
    delay(100);
  //----------------=HIGH=----------------
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);

    digitalWrite(LED3, HIGH);
    digitalWrite(LED6, HIGH);
    delay(100);
  //----------------=LOW=----------------
    digitalWrite(LED1, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED6, LOW);
    delay(100);
  //----------------=HIGH=----------------
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);

    digitalWrite(LED3, HIGH);
    digitalWrite(LED6, HIGH);
    delay(100);
  //----------------=LOW=----------------
    digitalWrite(LED1, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED6, LOW);
    delay(100);
  //----------------=HIGH=----------------
    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    digitalWrite(LED2, HIGH);
    digitalWrite(LED5, HIGH);

    digitalWrite(LED3, HIGH);
    digitalWrite(LED6, HIGH);
    delay(100);
  //----------------=LOW=----------------
    digitalWrite(LED1, LOW);
    digitalWrite(LED4, LOW);

    digitalWrite(LED2, LOW);
    digitalWrite(LED5, LOW);

    digitalWrite(LED3, LOW);
    digitalWrite(LED6, LOW);
    delay(100);
    }
    
    else {
      --timer; 
      delay(2);
    }
  }
}

You do realize that this is probably an infinite loop? Unless timer is declared volatile and handled in an ISR (which I cannot be certain of because I cannot see the code), this loop will never end.

For as far as I know it doesn’t make the loop infinite it just takes a bit of time to finish, hence why I would like the timer to run unless one of the 2 other if statement is true (so pushing the second button and returning to the first button to complete the circle).

I don’t understand your intent, so I cannot rewrite the code.

I’m trying to make a running game to make people run form one button to the other and eventually end up back at the first button where they started. above every button are a row of LEDs indicating how many buttons have already been pressed and a servo motor that shows them to go left or right for the next button. to give it more of a game like element the whole process is timed to give them a high score at the end. for that purpose i made a int timer, set that to 24000 and let that score count backwards with a while loop, however once it has started for some reason it doesn’t even recognize my button inputs anymore so I can’t make it do anything else while it’s counting down.

I’ve found that 75% or more of “my button doesn’t work the way I want” problems are because the button is not sewn on properly. How IS your switch wired?

one part of the button goes to the ground, another goes to pin 2/3 and in between the button and the connection to pin 2/3 is a 10k resistor connected to 5V
–UPDATE, IMG of the button added–

thanks for the replies, I hope everything is a bit easier to understand now for you

one part of the button goes to the ground, another goes to pin 2/3 and in between the button and the connection to pin 2/3 is a 10k resistor connected to 5V

A picture is worth a thousand words.

Your code is missing pinMode() calls. You should be using INPUT_PULLUP as the mode, and ditch the external resistor.

actually i just found out i uploaded the wrong coe, I'm sorry that was my test code :o i've updated the original post to now harbour the actual code... sorry guys

this is a picture of my buttons so far

Your code is missing pinMode() calls. You should be using INPUT_PULLUP as the mode, and ditch the external resistor.

wouldn't that make my pin float.. i'm still pretty new to arduino and C++ so i don't exactly know what INPUT_PULLUP will do

Hi,
No No No, do not update your original post, just add another.
By updating you dislocate the flow of the thread, anybody reading it will wonder what the following post are talking about.

Tom... :slight_smile:
Damn to late...

As I have been told not to update (After i mistakenly updated alread :confused:) i shall put everything i updated also in here

Here’s an image of my button so far

and here is my actual code so far

#include <Servo.h>

//--------------------------------------=all the variables=------------------------
int Pin_Butt1 = 2;
int Pin_Butt2 = 3;

const int LED1 = 13;
const int LED2 = 12;
const int LED3 = 11;

const int LED4 = 10;
const int LED5 = 9;
const int LED6 = 8;

Servo Servo1;
Servo Servo2;

const int Pin_Servo1 = 7;
const int Pin_Servo2 = 6;

int Direct[] = {0, 180};

int timer;
int score;
int Hscores[100];
int pos;
int x;

long ran;

//----------------------------------------------------------------=the set-up=---------------------------
void setup() {
  Serial.begin(9600);

  //--------------------------------------=assign the pins=----------------------
  pinMode(Pin_Butt1, INPUT);
  pinMode(Pin_Butt2, INPUT);

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);

  //--------------------------------------=Set up the servo motors
  Servo1.attach(Pin_Servo1);
  Servo1.write(90);

  Servo2.attach(Pin_Servo2);
  Servo2.write(90);
}

//------------------------------------------------------------=The Loop=----------------------------
void loop() {

  //--------------------------------------=Variable set up=-------------------------
  int Butt1_Value = digitalRead(Pin_Butt1);
  int Butt2_Value = digitalRead(Pin_Butt2);

  int LED1_Value = digitalRead(LED1);
  int LED2_Value = digitalRead(LED2);
  int LED3_Value = digitalRead(LED3);

  int LED4_Value = digitalRead(LED4);
  int LED5_Value = digitalRead(LED5);
  int LED6_Value = digitalRead(LED6);

  ran = random(2);

  timer = 0;
  score = 0;

  //--------------------------------------=IF statement #1=----------------------
  if (Butt1_Value == LOW && LED1_Value == LOW) {
    timer = 24000;

    digitalWrite(LED1, HIGH);
    digitalWrite(LED4, HIGH);

    Servo1.write(Direct[ran]);
    delay(2000);
    Servo1.write(90);
  }

  while (timer > 0) {
    //-------------------------------=IF statement #2=----------------------
    if (Butt2_Value == LOW && LED1_Value == HIGH) {

      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      Servo2.write(Direct[ran]);
      delay(2000);
      Servo2.write(90);
    }

    //---------------------------------=IF statement #3=----------------------
    if (Butt1_Value == LOW && LED2_Value == HIGH) {
      score = timer;
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);

      Hscores[pos] = score;
      ++pos;
      if (pos == 91) {
        pos = 0;
      }

      Serial.println("Congratulations, You made it!");
      Serial.println("Your score is:");
      Serial.println(score);
      delay(500);

      Serial.println("Here are the 10 latest scores:");
      for (x = 0 + pos; x < 10 + pos; ++x) {
        Serial.println(Hscores[x]);
      }

      //----------------=LOW=----------------
      digitalWrite(LED1, LOW);
      digitalWrite(LED4, LOW);

      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);

      digitalWrite(LED3, LOW);
      digitalWrite(LED6, LOW);
      delay(100);
      //----------------=HIGH=----------------
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);
      delay(100);
      //----------------=LOW=----------------
      digitalWrite(LED1, LOW);
      digitalWrite(LED4, LOW);

      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);

      digitalWrite(LED3, LOW);
      digitalWrite(LED6, LOW);
      delay(100);
      //----------------=HIGH=----------------
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);
      delay(100);
      //----------------=LOW=----------------
      digitalWrite(LED1, LOW);
      digitalWrite(LED4, LOW);

      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);

      digitalWrite(LED3, LOW);
      digitalWrite(LED6, LOW);
      delay(100);
      //----------------=HIGH=----------------
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);
      delay(100);
      //----------------=LOW=----------------
      digitalWrite(LED1, LOW);
      digitalWrite(LED4, LOW);

      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);

      digitalWrite(LED3, LOW);
      digitalWrite(LED6, LOW);
      delay(100);
      //----------------=HIGH=----------------
      digitalWrite(LED1, HIGH);
      digitalWrite(LED4, HIGH);

      digitalWrite(LED2, HIGH);
      digitalWrite(LED5, HIGH);

      digitalWrite(LED3, HIGH);
      digitalWrite(LED6, HIGH);
      delay(100);
      //----------------=LOW=----------------
      digitalWrite(LED1, LOW);
      digitalWrite(LED4, LOW);

      digitalWrite(LED2, LOW);
      digitalWrite(LED5, LOW);

      digitalWrite(LED3, LOW);
      digitalWrite(LED6, LOW);
      delay(100);
    }

    else {
      --timer;
      delay(2);
    }
  }
}

sorry guys never really used forums so this is still all a bit new to me :-X

wouldn't that make my pin float

No. The internal pullup resistor holds the pin at 5V until the switch is pressed. Then, the pin drops to 0V.

PaulS:
No. The internal pullup resistor holds the pin at 5V until the switch is pressed. Then, the pin drops to 0V.

Thanks, that is a really handy thing to know!!

I think for the sake of time I might try an build my timer by using Millis() since the deadline is tomorrow midday, but I would still like to know how it’s possible that my while loop keeps running when I specifically tell it only to run whenever nothing else is happening… If anyone can enlighten meni would be delighted ^^ would love to know what i’m doing wrong

but I would still like to know how it's possible that my while loop keeps running when I specifically tell it only to run whenever nothing else is happening..

You specifically tell it to run whenever timer is greater than 0. You set timer to 0, and then change the value to 24000 if either the switch is pressed or LED1 is on. I can't really figure out the need to read an output pin, since you (could) KNOW what you set the pin to.

I don't really understand the claim that "nothing is happening". Something is ALWAYS happening on the Arduino. That something may be the same as the something that was happening last time through loop(), but there is never nothing happening.