Best function for pausing the loop while a pin input reads high.

Hi all
Just started having a play with arduino.
I functionally test UI and IOs for my job role so the idea is to run some arduino projects to better understand how the system works, however I am stuck with the code.

I have searched Google and read posts on how to pause the loop with digital read high, interrupts ect but nothing seems to hold the loop.

I have a relay connected to the outputs of HV transformers, when they are triggered the relay goes closed creating the 5v output.

I've check all wiring and it's good.
I've checked serial read and I get a change in state 0 - 1 when the relay switches.

What would you recommend the best method for pausing the loop is?

Any help would be appreciated and if you need more info so you can assist, let me know.

Thanks in advance

You can pause the loop easily.

while (digitalRead(someInputPin)); /* empty while body! */

the problem is that this is effective but crude. Everything comes to a screeching halt until the pin goes low. Well except for anything you’ve got going on with interrupts.

I did this on a device I made, just was too lazy to do it any other way and it provided me a means to halt progress while I took care of business, never mind precisely about that.

It may be OK on your circumstances. What else is going on that might be compromised by a total lack of processor executing your other functions?

a7

What would you recommend the best method for pausing the loop is?

Nothing. The loop should be free to run.
https://forum.arduino.cc/index.php?topic=503368.0
https://forum.arduino.cc/index.php?topic=223286.0

You could also put the code you don't want to have run under a giant "if" that triggers when a buttonIsPressed flag is false. Then you can use an interrupt to change the flag to true when the button is pressed.

As @PerryBebbington said, it's best that you don't completely pause your loop, and by using a flag instead, you're avoiding this while also effectively stopping the things you don't want to have run.

Thanks for the quick replies.

I will have a look at the while function just to get it up and running

but will definitely read the linked posts too!
The code is pretty short at the moment but it will grow as I learn so I would like it written properly so as not to have confusing bugs in later down the road.

Thanks again all!

The while "works" but I now see what you mean about it being bad practice.

I have sensors reading temp, volts and current and that stops too, not good but not needed yet really.

The important thing is the solenoids are running and pausing so thanks for getting me up and running.

Next job is to read a lot on how to do it properly! Haha

Next job is to read a lot on how to do it properly! Haha

In my opinion learning how to do this correctly is one of the most important steps you can make on your journey from novice to experienced programmer.

“ In my opinion learning how to do this correctly is one of the most important steps you can make on your journey from novice to experienced programmer.”

Agree, totally. The OP wanted to stop the loop. He has now done, and has seen the delicate nature of doing and the consequences.

And whatever the thing needed to do, it is doing, whilst waiting for improvements.

Sometimes there’s just a nail sticking up and grabbing a hammer is the way to go. Naturally after solving a number of problems in this manner one ends up with a real mess.

a7

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