Continue a loop after it's been broken?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Continue a loop after it's been broken?

Post by legostormrobot »

Ok, working on programming the robot I built... ( https://sourceforge.net/apps/phpbb/mind ... f=2&t=1521 ) And I've come across a problem. Basically, I need to have the loop break, preform an action (outside of the loop) and then, return directly to the point at which the loop left off. I can't figure out how to make it work.

Any ideas?

Is it even possible?
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
h-g-t
Posts: 552
Joined: 07 Jan 2011, 08:59
Location: Albania

Re: Continue a loop after it's been broken?

Post by h-g-t »

I am not an expert but have never seen that done (with NXT-G anyway) and it risks all sorts of problems.

Why not put a second loop inside the first one, let it do whatever you want, then continue the first loop?
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Re: Continue a loop after it's been broken?

Post by legostormrobot »

Mmm.... I don't thing it would work. I need the loop to stop running when the ultrasonic sensor senses something within 10 cm. Then I need it to do the stuff outside the loop. Then I'd like it to continue running the loop at the point where it left off... so say it was three blocks through the loop when the ultrasonic sensor went off, it would start at the fourth block after it completed the outside task and continue running the loop.

Oh, and what would the "all sorts of problems" be?
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Continue a loop after it's been broken?

Post by mcsummation »

Put the stuff you want to execute inside/outside the loop into a MyBlock and execute it that way.
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Re: Continue a loop after it's been broken?

Post by legostormrobot »

Interesting. Could you embellish more please?
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Re: Continue a loop after it's been broken?

Post by legostormrobot »

I just thought of a BRILLIANT idea! (a little arrogant, perhaps...) Anyways, here goes.

My line of thinking is on variables. If you were to put sensors down on different lines running at the same time in the same loop and wire them to variables and put both the sensor and the variable in a loop, you could have a variable with the exact information the sensor carried to wire in to another loop after you completed the outside "task". In effect, you could continue your loop. The loop might be a little different, but it would do the same thing. It should work for what I'm trying to achieve. I'll test it in the morning.
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
iplewis
Posts: 25
Joined: 22 Mar 2012, 08:48

Re: Continue a loop after it's been broken?

Post by iplewis »

I'm not sure if this will help you or not, but in a text based language, (C, or NXC for example) you would write something like this:
I've never used NXT-G, but I would hope that there's something analagous to a function call and return.

Ian.

Code: Select all

for (i=0 ; i<20 ; i++)  //Loop 20 times
{
  //Do the stuff inside the loop

  if (ReadUltrasonic() < 10)  //Read the value from the ultrasonic. If it's < 10cm.....
  {
    SomethingIsClose();        //.... call this function to deal with it. Otherwise, carry on with the loop.
  }
}

SomethingIsClose()  //this is the function that acts when the ultrasonic detects something
{
  //Deal with close object

  return;  //Go back to where you were
}
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Re: Continue a loop after it's been broken?

Post by legostormrobot »

As far as I know, there isn't a call and return function in NXT - G. I believe you are somehow able to make your own blocks. I might try to do that after I learn some programming language.... ;) So that code might work to do some of that..... There may even be something like that available on the web! Thanks for the "call and return", I had no idea what to call it! :)

I can see where you're going with that code... it should work with a block you'd create. I don't think there's any way to type in raw code directly to nxt-G.
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: Continue a loop after it's been broken?

Post by mcsummation »

legostormrobot wrote:As far as I know, there isn't a call and return function in NXT - G. I believe you are somehow able to make your own blocks.
That is what I was referring to in my post about a MyBlock. It is, essentially, a subroutine that you can call from anywhere. It runs, then returns back to where it came from. You can pass in variables to it.

Take a look at http://www.youtube.com/watch?v=tyaVxnBTsfs. You'll have to mute the music or you won't be able to understand the things written in the video. :shock:
legostormrobot
Posts: 19
Joined: 12 Jul 2012, 17:48

Re: Continue a loop after it's been broken?

Post by legostormrobot »

That was interesting... but I still don't know how I'd get it to work with the loop...
Noah Allen

Age: 15

"The computer was invented to solve problems that didn't exist before."
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests