Pages: [1]   Go Down
Author Topic: Running a set programme  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
« Last Edit: February 10, 2013, 10:02:21 am by Quick5pnt0 » Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25782
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-mad
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: