Page 1 of 1

Arduino Programming... Changing Function with button?

PostedCOLON Sat Mar 31, 2012 7:31 pm
by Almurray42
Hello! :D

Okay, so I'm really getting stuck in to some basic programming with my arduino but there's one thing I just cant seem do do efficiently.
What i want to achieve is to have a single tactile button. When you push it, it starts function 1, again will stop one and start 2, again will start 3 and stop 2, and the final time will stop 3. Basically I need it to be like this:

Imagine below that i have already set up the program and the functions are written out where stated "BLAH". In between the *** *** Is basically what i need to know how to do, but i also need it to be able to do that in the middle of a function; not just at the finish of each function loop.

Void loop()
if(***BUTTON PRESSED ONCE****);
{ Run function 1}
if (***During function 1 Button is pressed***);
{ Run function 2}
if (***During function 2 Button is pressed***);
{Run function 3}
if (***During function 3 Button is pressed***);
{End fuction 3}

void function_1()
{BLAH}

void function_2()
{BLAH}

void function_3()
{BLAH}



Its a mighty big ask, but it may also be a simple answer... One mans weakness is another mans strength!
Cheers, Al

Re: Arduino Programming... Changing Function with button?

PostedCOLON Wed Apr 04, 2012 10:54 pm
by bigal344
I'm sure that someone will be able to point you in the right direction with this. I just about started to get the hang of this by pinching bits of code and reading through the posts and examples at the arduino forum, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl.
I still found that alot of 'simple' things eluded me completly and i now haven't had any time for ages to do anything, my board is still hooked up to a servo motor run/controlled by an Xbox analog stick!!
As Ben reminded me awhile back i really should get back onto it and continue learning the basics. The Massimo Banzi Arduino book is a great starter and is explained and is great for starting off, although is doesn't go into to much detail.

Re: Arduino Programming... Changing Function with button?

PostedCOLON Thu Apr 05, 2012 11:08 pm
by Almurray42
bigal344 wroteColonI'm sure that someone will be able to point you in the right direction with this. I just about started to get the hang of this by pinching bits of code and reading through the posts and examples at the arduino forum, http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl.
I still found that alot of 'simple' things eluded me completly and i now haven't had any time for ages to do anything, my board is still hooked up to a servo motor run/controlled by an Xbox analog stick!!
As Ben reminded me awhile back i really should get back onto it and continue learning the basics. The Massimo Banzi Arduino book is a great starter and is explained and is great for starting off, although is doesn't go into to much detail.


Yeah, I've had a look at some posts on there and have read the Massimo Banzi book 2nd Edition... Coding is just something that you have to build on, you really can't try to jump in at the deep end as I did!
I am trying to understand how to use the Millis() Arduino clock instead of delays so the arduino can check states simultaneous to running a loop...

Re: Arduino Programming... Changing Function with button?

PostedCOLON Sat Apr 14, 2012 8:25 pm
by phenoptix
You can do it using interupts. But so far I've not managed to get them working on even the simplest example so cant comment from an expert point if view. I'll have another go if I get some time next week, could do with it for changing settings on the Larson scanner.

Re: Arduino Programming... Changing Function with button?

PostedCOLON Sat Apr 14, 2012 9:17 pm
by Almurray42
phenoptix wroteColonYou can do it using interupts. But so far I've not managed to get them working on even the simplest example so cant comment from an expert point if view. I'll have another go if I get some time next week, could do with it for changing settings on the Larson scanner.


I've found a sample program that uses an interrupt to blink an led with a button, but can't seem to incoorporate it into my program, very frustrating! :S I'm still trying though!

Re: Arduino Programming... Changing Function with button?

PostedCOLON Tue Feb 17, 2015 12:11 pm
by faarigia
Thanks for letting us know, I'm sure that will help others out in the future too