1

I have a analog inputs to my Uno that I am cleaning up and using to keep a digital pin high until the output drops to zero. At this point I want to stop reading that input because it may come back up after being low for a short time. Is there any way to set a variable that can be triggered by that first zero that prevents the loop from sending the digital pin high from then on? Like a latch of some sort, or could I use an interrupt somehow?

  • 1
    Sure. Just set a variable to some value. And use if to compare that value. – Majenko May 14 '20 at 10:54
  • You may call that variable a "flag" which is set once your input == 0 and read before settings the output to high. – towe May 14 '20 at 11:00

1 Answers1

0

You've already described what you need to do. Just set a boolean variable. It really couldn't be easier.

boolean runThisPart = true;

void loop() {

  if(runThisPart) {
     // this part only runs if runThisPart is true
     //  if something happens and you don't want
     //  to run this part anymore then
     if(some-condition){
        runThisPart = false;
     }
  }
  //  The rest of your code can also set runThisPart
  //  to true or false to turn that section on or off.
}
Delta_G
  • 3,270
  • 2
  • 10
  • 24