Run a function only once inside loop

hi all,

I am aware this has been asked to death but still, i have not been able to find a satisfactory answer to my problem.

I have a simple push button that needs to write a message every time it goes high. It is low by default.

Now, my mockup code looks like this:

void loop() {
  
  if (digitalRead(pushPin) == LOW) {      
      // write everything is OK
      Serial.print(“OK”)
      
  } else {
    Serial.print(“OPEN”)
  }
}

The problem is, i need just ONE TIME write of "OPEN" message once the pus button pin goes high. Right now it would keep writing the message as long as the pin is HIGH.

Ideally, once the push pin goes high, i would get a message "open" once, then to get it again the push pin would have to go back to LOW and then back to HIGH again.

Many thanks in adavance,
Alek

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

hmmm, makes sense. on it now. many thanks

StateChangeDetection

Just try this (sorry... in Spanish :D). I'm sure you could understand the code :smiley:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.