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?

2
  • 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
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.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.