Continue a loop after it's been broken?
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Continue a loop after it's been broken?
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?
Any ideas?
Is it even possible?
Noah Allen
Age: 15
"The computer was invented to solve problems that didn't exist before."
Age: 15
"The computer was invented to solve problems that didn't exist before."
Re: Continue a loop after it's been broken?
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?
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.
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Re: Continue a loop after it's been broken?
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?
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."
Age: 15
"The computer was invented to solve problems that didn't exist before."
-
- Posts: 220
- Joined: 23 Jan 2012, 17:07
- Location: Round Rock, TX
Re: Continue a loop after it's been broken?
Put the stuff you want to execute inside/outside the loop into a MyBlock and execute it that way.
McSummation aka James
http://www.mcsummation.com/Mindstorms/
http://www.mcsummation.com/Mindstorms/
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Re: Continue a loop after it's been broken?
Interesting. Could you embellish more please?
Noah Allen
Age: 15
"The computer was invented to solve problems that didn't exist before."
Age: 15
"The computer was invented to solve problems that didn't exist before."
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Re: Continue a loop after it's been broken?
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.
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."
Age: 15
"The computer was invented to solve problems that didn't exist before."
Re: Continue a loop after it's been broken?
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.
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
}
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Re: Continue a loop after it's been broken?
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.
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."
Age: 15
"The computer was invented to solve problems that didn't exist before."
-
- Posts: 220
- Joined: 23 Jan 2012, 17:07
- Location: Round Rock, TX
Re: Continue a loop after it's been broken?
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.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.
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.
McSummation aka James
http://www.mcsummation.com/Mindstorms/
http://www.mcsummation.com/Mindstorms/
-
- Posts: 19
- Joined: 12 Jul 2012, 17:48
Re: Continue a loop after it's been broken?
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."
Age: 15
"The computer was invented to solve problems that didn't exist before."
Who is online
Users browsing this forum: No registered users and 8 guests