[solved] Start switch in off state

hello,
I am working on a project. I want to the user to press the button on my arduino to continue the setup. I want the program to force the button value to be 0 even if is reseving power. If the program is running i want to switch the values when the button is pressed from 0 to 1. So the program forces you to press the button to continue.

In short i want my button to start at the value 0. even if the input is receving power

CODE

int ButtonPin = A1;
int ButtonState = 0;
void setup() {
Serial.begin(9600);
pinMode(ButtonPin, INPUT);
Serial.println ("Hit the button to start");
ButtonState = digitalRead(ButtonPin);
ButtonState = 0;
while(ButtonState == LOW){
ButtonState = 0;
ButtonState = digitalRead(ButtonPin);
Serial.println(ButtonState);
delay(1000);
}

Serial.println ("Setup Finished");
}
void loop(){

ButtonState = digitalRead(ButtonPin); // read input value
if (ButtonState == LOW) { // check if the input is HIGH (button released)
Serial.println("Button OFF");
} else {
Serial.println("Button ON");
}
delay(5000);
}

how is the button connected? is it HIGH or LOW when relased? if it should be HIGH when released the pin must by INPUT_PULLUP mode. LOW is 0 and HIGH is 1, but don't mix it in code

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Thanks for your feedback. I just got done trying to figure it. Also the button clicks in the on and off so its more a switch. But i set the setup so if the program starts it automatically sets the state it is in to off (0). here is my code if your interested. And if you have any feedback please let me know.

int pushButton = A0;  // Button Pin
int CurrentRPM = A1;  //create max rpm reading from pin A0 while analog reading
void setup() {
  Serial.begin(9600); //serial monitor setup
    Serial.println("Please do not touch the device.");  //dislay text
      delay(2000);  //delay 2000ms
        pinMode(pushButton, INPUT); //pushbutton pinmode set to input
          int buttonoff = digitalRead(pushButton);  // create button off state and read
            Serial.println("Button off state set"); //display text
              int buttonState = digitalRead(pushButton);  //create buttonstate and read
                Serial.println("Current buttonstate set"); // display text
                  Serial.println("Change the state of the button"); // display text

  
                  
  while(buttonoff == buttonState){ //if buttonoff state is same as buttonstate then wait for change
      buttonState = digitalRead(pushButton); // refresh buttonstate 
  }
  
  int ButtonOn = digitalRead(pushButton); // create button on state and read
    Serial.println("Current Button Status"); //display text
      Serial.println(ButtonOn); //display text
        Serial.println("On State");  //display text 
          Serial.println(ButtonOn); //display text
            Serial.println("Off State");  //display text
              Serial.println(buttonoff);  //display text
  
  Serial.println("turn on water to max");// display text
      int TempButtonState = digitalRead(pushButton); //add button state temp
        Serial.println("Press the Button to continue");
        
          while (buttonState == TempButtonState) {
              buttonState = digitalRead(pushButton); // refresh buttonstate 
                delay(1000); // delay 1000ms
          }
            int MaxRPM = analogRead(CurrentRPM);
            int TempRPM = analogRead(CurrentRPM);
delay(250);
          
            for (int i=0; i <= 5; i++){
            TempRPM = analogRead(CurrentRPM);
            Serial.println(MaxRPM);
            Serial.println(TempRPM);
                if (MaxRPM < TempRPM) {
                  MaxRPM = analogRead(CurrentRPM);
                  MaxRPM = TempRPM;
               }
           delay(250);
            }

            
  Serial.println("Setup Finished");  // display end text
  Serial.println(MaxRPM);
}
void loop() {

}

input pin 'floats' if it isn't connected to anything. so you read a random value 0 or 1.

you must decide how do you want to connect the button.

Tutorial/InputPullupSerial
Tutorial/Button

As also mentioned in your other thread, please format your code properly.

The best way to wire a button is between pin and GND, and set the pin to INPUT_PULLUP. Then it's active LOW, and the pin guaranteed HIGH when the button is released/open.

Your waiting-for-button-press code becomes then as simple as:

while (digitalRead(ButtonPin);

No need to do a delay() there unless you really want the user to experience a random delay in reacting to the button.