if statement

hello

if I have the following code

if(digitalRead(trigger) == HIGH){
delay(1000);
digitalWrite(5, HIGH);
}
if(digitalRead(trigger == LOW)){
digitalWrite(5, LOW);
}

and if after 500 ms ''trigger'' is set to LOW, what happens?
''5'' is going immediatly to LOW, or it waits more 500ms and only then going to LOW ?

delay(1000) means don't do anything until that 1 second is up. That means nothing (except interrupts) will happen.

delay halts everything, so in effect your arduino is brain dead for 1 second, as soon as that second is up then it will react to whatever state the conditions dictate

hmmm
but I want it to run only once and then stop when the trigger is set to LOW
there is the problem of the loop keeping running

softuino:
hmmm
but I want it to run only once and then stop when the trigger is set to LOW
there is the problem of the loop keeping running

So use a state variable that can be set when you read LOW and check if the state variable is set when you decide if you want to “run” whatever it is you are trying to run.

I want to pin5 be HIGH while trigger is HIGH
and then go LOW when trigger goes LOW
but I don't want the loop to run more than once

byte state = 0;

if(digitalRead(trigger) == HIGH && state == 0){
  delay(1000); 
  digitalWrite(5, HIGH);
}
 if(digitalRead(trigger == LOW && state == 0)){
  digitalWrite(5, LOW);
  state = 1;
}

softuino:
I want to pin5 be HIGH while trigger is HIGH
and then go LOW when trigger goes LOW
but I don't want the loop to run more than once

So what you need is called signal edge detection; you want an action to occur on the transition from LOW to HIGH. Take a look at the example "StateChangeDetection" under the Digital category in the Arduino IDE.

You have this code:if(digitalRead(trigger) == HIGH){which looks OK.

Then there's this code: if(digitalRead(trigger == LOW)){which looks different. Here's what I think it will do:

  • Compare the value of trigger to the value of LOW, or zero
  • If trigger is zero, the result will be 1; otherwise, the result will be zero
  • Do a digitalRead on pin 1 or pin 0, depending on the outcome of the test above
  • If the digitalRead returns a 1, the test will pass; if it returns a zero, it will fail

Did you want this instead?

 if(digitalRead(trigger) == LOW){

Osgeld:

byte state = 0;

if(digitalRead(trigger) == HIGH && state == 0){

}

why did u use byte?.... can't it be like this .... int state = o and the change it later to any value....

why did u use byte?

To save valuable RAM?

can't it be like this .... int state = o and the change it later to any value....

Yes, if you have a variable called "o", you can write int state = o;

Hani:

Osgeld:

byte state = 0;

if(digitalRead(trigger) == HIGH && state == 0){

}

why did u use byte?.... can't it be like this .... int state = o and the change it later to any value....

it can, byte is half the ram (and a max of 255 unique values vs 64K)

@ softuino:

there is the problem of the loop keeping running

Yes, that's your problem. I'ts caused by delay().
The solution is not to use delay, but keep track of the time elapsed since your last action ( digitalwrite(5,HIGH); ).

( See "blink without delay" in the getting started tutorials )

thanks Osgeld and AWOL