Go Down

Topic: Running a set programme (Read 303 times) previous topic - next topic

jaddion82052

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 :)

Quick5pnt0

#1
Feb 10, 2013, 03:52 pm Last Edit: Feb 10, 2013, 04:02 pm by Quick5pnt0 Reason: 1
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.
Code: [Select]

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.
Code: [Select]

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.

AWOL

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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
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.

Go Up