Running a set programme

Hi all,

Im a bit of a noob so be nice!

I do understand the for while and if loops, but what if you had a programme that you only wanted to start once a button had been pressed. So it can be pressed for an instant, but that then triggers the code to run.

I thought about doing a 'for while button is high run programme' but that will then stop when the button is released?

Any help would be appreciated :slight_smile:

It really depends on wheather you want the code to run only once or forever after the button press.

If you only want it to run once then you could just look for the button press like normal. Pseudo code would be something like this, but you'd need to debounce the button.

If button equals HIGH
Then run code

If you wanted it to run forever after the button press you could create a boolean variable that is false until you press the button.

RunCode equals false

If button equals HIGH
Then RunCode equals TRUE

If RunCode equals TRUE
Then run code

There are probably more elegant ways of doing his but it should get you started.

Please don't start new threads on the same topic in different parts of the forum; it's called cross-posting, because it makes people cross. :0

how do i get the small bit of code to run with just an instantaneous press if the button.

There really is no such thing as an instantaneous press of a switch. The switch will be pressed for a while. What you need to do is make sure that you get around to reading the switch while it is pressed. That means no delay() calls anywhere in this mysterious code of yours.