Stopping a forever loop

Hi, I have a sprite (wheel) that spins and I want it to stop! It gets stuck in the forever loop (not surprisingly)

I want to set the motor spinning with and then be able to stop it or change the speed/direction.

Any ideas?

There are a lot of ways. Probably the most sensible -- the easiest to understand when you reread your code a month after you wrote it -- is to replace the FOREVER with a REPEAT UNTIL that specifies the condition under which you want to stop. There are more complicated things you can do with the STOP block. Another way would be to make SPEED a sprite-local variable instead of an input, and then you could say SET SPEED TO 0 or whatever.

You mean that?

untitled script pic (11)


untitled script pic (12)

I am struggling to the condition that will stop the repeat until loop. If I was just writing normal code, it would be no problem, however I am making a simulator for Hummingbird. So I have made blocks that look like the Hummingbird blocks. My problem is that the code gets stuck in the forever loop. As you say, I could replace it with a repeat until loop, but until what? I want it to continue until it is told to do something else.

Well for example you could just have a global variable that's set by whichever other part of the program wants to stop it. Since I don't know anything about your program, I can't tell you what the condition is! You have to tell me.

I tried the global variable but could not make it work. Is it possible to tell a user created block to run, but continue on with the other code rather than stop and wait for it to be completed?

when do you want the motor to stop?

I suppose You need to "remotely" control and simulate motor of Your robot kit.
There is no need to stop loop. Motor sprite can have "this sprite only" variable "speed" and an "forever" loop.


Procedure can set "Motor.speed" value to desired rotation (stop at 0).
The same way real motor speed can be controlled - by turning control knob, variable resistor or time share of PWM signal.

I don't use Hummingbird robots, but I guess the clue is @dardoro post. Stopping Snap! means to stop sending messages to your robot, not to stop the robot. Then you must send to robots all the actions you need before stopping.

To help this situation and have it better integrated into Snap, you have

Then, also red stop button can trigger extra final actions. Using it, you can define a "reset state" and stop your program and your robot when you want.


… I could replace it with a repeat until loop, but until what? I want it to continue until it is told to do something else.

You answered your own question and specified the "what" of the repeat until loop. What exactly "told to do something else" is and how it is implemented is up to you. If I were doing this, I'd probably keep a local variable that denotes the state of the motor, and do something like this:

  • 0: No spin
  • 1: Spin

Then tell the motor to set this local variable to 1 to have it spin, or 0 to stop it.

I would recommend using false andtrue rather than 0 and 1 and maybe in the other order, so you can just put the variable in the REPEAT UNTIL predicate slot.

Yes. Use the LAUNCH block with your block as its input.

Wow, you guys are great. So helpful. I will get back onto it tomorrow Australian time. It looks like there are a couple of good solutions there.


It worked! Using True and False and Launching the block did it. I did have to put a sneaky Wait 0.01 in there to make sure the StopCode True worked. Here it is:

Try moving the set-to-false block into the Secret Motor block, after the REPEAT UNTIL. That'll make sure it stays true long enough to stop the REPEAT UNTIL.

But I'm having trouble understanding your program logic. The calls to the blue motor block all have motor# 1, but that block soesn't do anything unless motor# is 2.

Your suggestions work well. The only difference was that I put the set-to-false block before the REPEAT UNTIL

On the question of motor #, teh #1 sets the default value to 1. The reason I did that is the motor number can be 1 or 2 on the actual Hummingbird board. In the picture you can see the motor is plugged into port 2. I did it this way so the students have to change the motor port to 2 for it to work. This will help when they code the actual Hummingbird because they will think about which port each component is plugged into.

It works better without the wait 0.01sec.

Ah. "Debug this code." Got it!

Without "wait 0.01" it should not work at all.
"wait 0.01" allows other threads to do a single iteration of their loops. So "until" condition can be fulfilled.

You can say WAIT 0 SECS to cause a context switch without an actual pause.