Arduino Programming... Changing Function with button?

Using and Programming Arduino and other microcontrollers
Almurray42
Competent
PostsCOLON 293
JoinedCOLON Wed Jun 30, 2010 11:42 pm
LocationCOLON Bridgwater, Somerset, England
CONTACTCOLON

Arduino Programming... Changing Function with button?

Postby Almurray42 » Sat Mar 31, 2012 7:31 pm

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
Image
[MOD IT] [OR BIN IT]

bigal344
Competent
PostsCOLON 426
JoinedCOLON Thu Jun 09, 2011 11:21 pm
LocationCOLON Crewkerne,Somerset

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

Postby bigal344 » Wed Apr 04, 2012 10:54 pm

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

Today I have been mostly Nyaning.

Almurray42
Competent
PostsCOLON 293
JoinedCOLON Wed Jun 30, 2010 11:42 pm
LocationCOLON Bridgwater, Somerset, England
CONTACTCOLON

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

Postby Almurray42 » Thu Apr 05, 2012 11:08 pm

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...
Image
[MOD IT] [OR BIN IT]

phenoptix
Competent
PostsCOLON 744
JoinedCOLON Sat Apr 17, 2010 11:25 am

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

Postby phenoptix » Sat Apr 14, 2012 8:25 pm

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.

Almurray42
Competent
PostsCOLON 293
JoinedCOLON Wed Jun 30, 2010 11:42 pm
LocationCOLON Bridgwater, Somerset, England
CONTACTCOLON

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

Postby Almurray42 » Sat Apr 14, 2012 9:17 pm

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!
Image
[MOD IT] [OR BIN IT]

faarigia
Harmless
PostsCOLON 1
JoinedCOLON Tue Feb 17, 2015 12:03 pm

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

Postby faarigia » Tue Feb 17, 2015 12:11 pm

Thanks for letting us know, I'm sure that will help others out in the future too


Return to

Who is online

Users browsing this forum: 3 and 0 guests