0

code dose not perfect work

int pushButton = 2;

int gearstatus = 0 ;

int buttonState;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);


}
void gearfunction(){
  buttonState = digitalRead(pushButton);

  while(gearstatus <= 5){

    Serial.println( gearstatus);

    if(buttonState == HIGH){
      gearstatus++;}
    }
  }


void loop() {


  gearfunction();

}

in this code i am trying to if statement in while loop, but code doesn't work . can some one give me how to did this ? i want to increase gearstatus up to 5 but value not increase .

Ravina Jasani
  • 111
  • 1
  • 1
  • 7

3 Answers3

1

It doesn't work because marking pushButton as INPUT wont make it equal to HIGH

You need to put inside setup function after the input instruction:

digitalWrite(pushButton,HIGH)
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

A Button-Pin has to be initialized on Setup, like in the preceding answer or with:

pinMode(2, INPUT_PULLUP); //Pin-D2. This command activates it's internal 
//resistor, so the resulting signal is clear HIGH and not floating like a duck...

Hint: on StartUp all pins of a uC are floating-inputs, so if pulled-UP they may be grounded with a button or anything else like a sensor, NTC-Resistor, etc.), resulting in a clear "1" or "0" - or a defined Analog-Signal, which later may be scanned like:

boolean buttonState = digitalRead(2); //Pin D2
//or
int value = analogRead(A0); //Pin A0
-1

Can you please explain a little what you are trying to do, It would be better if you attach a small image of the circuit. I can suggest you the code and answer using circuits.io if you give the required info.

Arjun Singh
  • 677
  • 6
  • 18