Pages: [1]   Go Down
Author Topic: loop in loop question  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi All,

My first message and question here and appreciate it if you could allocate some time for it.

The thing is
Code:
    -------
    |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 Online
Brattain Member
*****
Karma: 547
Posts: 45979
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The loop function is called from main like this:
Code:
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 Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks so much , I am going to try this asap
Logged

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

you could have...
Code:
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:
Code:
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 Offline
Brattain Member
*****
Karma: 238
Posts: 24287
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 Offline
Brattain Member
*****
Karma: 238
Posts: 24287
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it because of the nature of an inturupt or is it just a programing no-no?
Logged

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

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But that is the desired result.
Logged

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

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, ok. That makes sense.
Logged

Pages: [1]   Go Up
Jump to: