why ledPin unexpectedly turn off in the third if statement situation?

why ledPin unexpectedly turn off in the third if statement situation?

const int buttonPin = 8;
const int ledPin = 9;

int buttonState = LOW;
int trigger = 0;

void setup() {
 pinMode (ledPin, OUTPUT);
 pinMode (buttonPin, INPUT);

}

void loop() {
  buttonState = digitalRead (buttonPin);

 if (buttonState == HIGH)  {digitalWrite (ledPin, HIGH);trigger = 1;}  // ledPin turn on

 if (trigger == 1) {digitalWrite (ledPin, HIGH);}       //hold the ledPin on //all ok until here

 

 if (buttonState == HIGH && trigger == 1) {digitalWrite (ledPin, LOW);(trigger = 0);} 
 //i don't know why but in this code line turn off the ledPin without buttonState in HIGH situation

}

What's connected to pin 8? Is there a pulldown resistor or something connected to ground to make sure it reads LOW?

yes, i connected a push button with 1k resistor

GrOnThOs:
yes, i connected a push button with 1k resistor

1K resistor between where and where?

sorry 10k resistor between pin 8 and GND

Ok, so the only way the led gets turned on is if the buttonState is HIGH and that's exactly the same thing you test for to turn it off.

I guess the real question is, how do you want it to work?

Ok, so the only way the led gets turned on is if the buttonState is HIGH and that’s exactly the same thing you test for to turn it off.

yes but not only the the buttonState, also consider the trigger value (0 or 1)
<< if (buttonState == HIGH && trigger == 1)>>

Ok, follow along. You read the button and store in button state. Lets say it’s HIGH. Its not going to change until the next iteration of loop. If it is HIGH then you turn on the led and set trigger to 1. If trigger is 1, you again write the led HIGH, so nothing changes. Now button State is HIGH and trigger is 1. So you hit that third if that says exactly that, if button State is HIGH and trigger is 1 turn it off. I don’t see how button State could be anything but HIGH and trigger could be anything but 1 if the led came on.

i completely understand now.
i didn't realize that button state change in the next iteration of loop.
thank you very much for your help!!!

GrOnThOs:
i completely understand now.
i didn't realize that button state change in the next iteration of loop.
thank you very much for your help!!!

It's the first line of loop that reads the button. That's the only place it can change. If it is HIGH in the first if then it will be HIGH in the third one as there is no code reading thatinput between them.

OK, i have been changed the code and now the LED can’t turn off if i pres the button again.
what is the mistake I am doing now?

 // variables will not change
const int buttonPin = 8;  // the number of the pushbutton pin            
const int ledPin = 9;  // the number of the LED pin

// variables will change
int buttonState = LOW;    // variable for reading the pushbutton status
int trigger = 0;         // variable that helps to change the led situation


void setup() {
 pinMode (ledPin, OUTPUT);  // initialize the LED pin as an output:
 pinMode (buttonPin, INPUT);   // initialize the pushbutton pin as an input:


}

void loop() {

  buttonState = digitalRead (buttonPin);  // read the state of the pushbutton value:

 if (buttonState == HIGH && trigger == 0)  {digitalWrite (ledPin, HIGH); trigger = 1;} 


if (trigger == 1 ) {digitalWrite (ledPin, HIGH);buttonState = LOW;  }      

 
 if (buttonState == HIGH && trigger == 1) { digitalWrite (ledPin, LOW);(trigger = 0); }

That last if can never be true. If trigger is 1 then the line before it makes buttonState low. So buttonState cannot be HIGH there if trigger is 1.

Remember that loop runs those instructions in order one after the other.

I think what you want is if button State is HIGH and trigger is 0, then turn on the led and set trigger to 1. If butonState is HIGH and trigger is 1 then turn led off and set trigger to 0. You only need two if statements to do that.

That code gets executed in order without any updates far faster than your finger can react. So upon trying to press the button a second time 'trigger' is set to 0, then is immediately set back to 1 in the next loop cycle. Nullifying the use of that code line. Having an LED go off for such a short amount of time is unnoticeable to the human eye.

You are forcing the variable buttonState in your program. You shouldn't. That variable should naturally take the form of what the button is doing, and instead use a variable that captures the last known state of the button to be used in logic.

EDIT: Actually I think the last line won't ever be called anyway. Which is still likely caused by trying to write code that messes with buttonState.

That last if can never be true. If trigger is 1 then the line before it makes buttonState low.

OK i understand now but why don't i get conflict error?

I think what you want is if button State is HIGH and trigger is 0, then turn on the led and set trigger to 1. If butonState is HIGH and trigger is 1 then turn led off and set trigger to 0. You only need two if statements to do that.

void loop() {

  buttonState = digitalRead (buttonPin);  // degital read the state of the buttonPin and store them in the buttonState

 if (buttonState == HIGH && trigger == 0)  {digitalWrite (ledPin, HIGH); trigger = 1; buttonState = LOW;  }    

 
 if (buttonState == HIGH && trigger == 1) { digitalWrite (ledPin, LOW);(trigger = 0); } 



}

its works in that way but with some unstable.
sometimes not get OFF, sometimes not get ON
is because i can't push the push button in the correct time between the loop line?

GrOnThOs:
OK i understand now but why don't i get conflict error?

Because there is no error. The compiler can read and compile your code. It doesnt check to see if it makes any sense.

GrOnThOs:

void loop() {

buttonState = digitalRead (buttonPin);  // degital read the state of the buttonPin and store them in the buttonState

if (buttonState == HIGH && trigger == 0)  {digitalWrite (ledPin, HIGH); trigger = 1; buttonState = LOW;  }

if (buttonState == HIGH && trigger == 1) { digitalWrite (ledPin, LOW);(trigger = 0); }

}




its works in that way but with some unstable.
sometimes not get OFF, sometimes not get ON
is because i can't push the push button in the correct time between the loop line?

Don't set buttonState to LOW. Let buttonState be set by whatever you get from digitalRead.

This program is going to run very fast. If you press the button, it's going to run hundreds of times before you can get your finger off the button. First it sees that the button is HIGH and the trigger is 0 so it turns the led on and sets trigger to 1. Then it sees the button is HIGH and trigger is 1 so it sets trigger to 0 and turns the led off. It does this over and over and over until you take your finger off the button. Which state it will be in at the instant you stop pressing the button is anyone's guess.

If you want the program to respond once and only once when you press the button then you need to look for CHANGES in the button state, not just for it being HIGH. You don't want to react when the button IS HIGH, you want to react when it BECOMES HIGH. You need to keep up with the last state of the button so you can compare and see if it changed. Look at the State Change Example that came with the IDE for some inspiration.

Don't set buttonState to LOW. Let buttonState be set by whatever you get from digitalRead.

 if (buttonState == HIGH && trigger == 0)  {digitalWrite (ledPin, HIGH); trigger = 1; }   

 if (buttonState == HIGH && trigger == 1) { digitalWrite (ledPin, LOW);(trigger = 0);}

its not works in this way. when i push the button the LED is little less brightness than normal, probably because of second if statement. i think an output voltage Frequency analyzer will be helpful here. also the LED immediately tern off just when i release the button, i think again because of second if statement.

but if i change the code as below and follow the serial monitor signals. all works great.

void loop() {
 Serial.println ("push now"); 
 delay(1000);
 buttonState = digitalRead (buttonPin);  // degital read the state of the buttonPin and store them in the buttonState
 Serial.println ("release");
 delay(1000);
 if (buttonState == HIGH && trigger == 0)  {digitalWrite (ledPin, HIGH); trigger = 1; buttonState = LOW; }   
 
 if (buttonState == HIGH && trigger == 1) { digitalWrite (ledPin, LOW);(trigger = 0);} 


}