0
Offline
Newbie
Karma: 0
Posts: 2
Arduino rocks
|
 |
« on: July 15, 2010, 01:52:00 pm » |
Hi All, My first message and question here and appreciate it if you could allocate some time for it. The thing is ------- |program starts |void loop() |{ |program runs |still running |if a button is pressed ------ loop| (1st command for this loop) | | another thing runs in a loop | | if another button is pressed the program starts again |loop in loop | from the beginning (2nd command for exiting from this loop)| |else program keeps on running ------ |} | -------
I hope I could explained my problem clearly , I am quite new in arduino and couldnt find the right (first and second) commands. Tried many things like goto , loop , return ... , either using wrong commands or using them wrong way. :-? Thanks
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35582
Seattle, WA USA
|
 |
« Reply #1 on: July 15, 2010, 02:08:56 pm » |
The loop function is called from main like this: for(;;) { loop(); } You could add a similar infinite loop in the loop function. Make the button press use the break; command to break out of the infinite loop. Control will branch to the next instruction (after your infinite loop).
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 2
Arduino rocks
|
 |
« Reply #2 on: July 16, 2010, 01:22:15 am » |
thanks so much , I am going to try this asap
|
|
|
|
|
Logged
|
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #3 on: July 16, 2010, 12:58:43 pm » |
you could have... void loop(void) { while(digitalRead(btnPin) == HIGH) { // Do some stuff until another thing happens. } }
Oorrr... you could use an inturupt, which is more complex but won't slow the rest of the program. The inturupt pin is Pin 2 or 3, so you could do some code like this: void setup(void) { attachInterrupt(0, Function, RISING); // 0 for pin 2, 1 for pin 3. }
void loop(void) { //Normal stuff to do }
void Function(void) { // Do this when the Pin 2 goes from low to High, it could trigger a loop, but this will kill the rest of // the program without a way to exit it, eg for(;;) { if(digitalRead, 2) == LOW) { break; } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: July 16, 2010, 01:11:37 pm » |
void Function(void) { // Do this when the Pin 2 goes from low to High, it could trigger a loop, but this will kill the rest of // the program without a way to exit it, eg for(;;) { if(digitalRead, 2) == LOW) { break; } } }
Yup, an infinite loop in an inturupt [sic] routine - that's the way not to do it.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #5 on: July 16, 2010, 01:30:43 pm » |
Why not? And it isn't infinite if a situation can break it, I agree a while statement would be more suitable though, as in void Function(void) { // Do this when the Pin 2 goes from low to High, it could trigger a loop, but this will kill the rest of // the program without a way to exit it, eg while(digitalRead(2) == HIGH) // bit of an error here, on the first { // Do stuff until pin 2 is low. } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: July 16, 2010, 01:34:33 pm » |
I agree a while statement would be more suitable though, as in In C "for ( ; ; )" == "while (1)" ISRs generally fall-through - if you're putting a "while" loop in there, you've almost certainly got it wrong.
|
|
|
|
« Last Edit: July 16, 2010, 01:34:49 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #7 on: July 16, 2010, 02:59:49 pm » |
Is it because of the nature of an inturupt or is it just a programing no-no?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: July 16, 2010, 03:25:16 pm » |
An interrupt is a signal to the processor that something has happened, and the response to it that stimulus had better be pretty d a m n quick and finished with, so the processor can get on with all the other things it had to do.
Any kind of loop that could take an indeterminate time (and potentially lock out other significant events) doesn't really fit into that picture.
|
|
|
|
« Last Edit: July 16, 2010, 03:35:40 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #9 on: July 16, 2010, 04:31:25 pm » |
But that is the desired result.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: July 16, 2010, 04:34:29 pm » |
But that is the desired result. I'm being kept awake by a neighbour's barking dog. I go out and shoot the dog dead with my shotgun. The dog becomes quiet. That's the desired result, but the method was not the generally accepted one.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #11 on: July 16, 2010, 04:41:48 pm » |
And by that analogy you would be breaking a law. Well in this situation such a law doesn't exist to be broken. Sorry it isn't your favored method your heinous, but it is irrelevant how he gets to the effect provided he gets there. Showing him as many ways to do it as possible will offer the greatest insight into Arduino coding (and C at large) and give him some more choice!
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #12 on: July 17, 2010, 02:28:21 am » |
If you know what you are doing, then loops in an interrupt may be the correct solution. However, the solution may lead to undesired side effects, like lost serial characters or even lost clock ticks. So, that 'if you know what you're doing' is a mighty big 'if'.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Devon, UK
Offline
Full Member
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
|
 |
« Reply #13 on: July 17, 2010, 01:38:11 pm » |
Oh, ok. That makes sense.
|
|
|
|
|
Logged
|
|
|
|
|
|