Blocks that Repeat

Sometimes we need to be able to program our Propeller to repeat certain operations.  This is useful for simple things, such as blinking a light, and more complex tasks, such as robotic sensor navigation.

Blocks of repeating code are called loops.  A loop can be set up to repeat a set of blocks forever, or while some contdition is true, or for a certain number of times.

Test Code

Open a new project and build the blocks as shown.  You can find the conditional repeat block under the CONTROL menu.

  • Make sure the BlocklyProp client is running and your board is selected.
  • Load to RAM.
  • The terminal will open.  After a few seconds, it should look like this:

The program above first prints the text “This loop repeats forever”, and then moves the cursor to the next line.  Then, the program sets up a loop.  Inside the loop, there is a block that prints the text “...and ever…” with a new line and a 1 second pause.  The program will continue running forever.  The only way to stop the program is to reset the Propeller microcontroller.

Try This - Break

Many programs need to use loops that run forever, but sometimes it is helpful to break out of a loop. That is where the break block comes in handy. 

In the next example, we will set up a new variable and give it a value of 0.  Inside of the loop, we will add 1 to the variable using the de/increment block from the VALUES > NUMBERS  menu. We will set it to increment, which is just a fancy way of saying "add 1 to the variable."  Then, we will use an if...do block to see if our variable is bigger than 5, and if it is, break out of the loop.

Modify your program to look like this:

  • Make sure the BlocklyProp client is running and your board is selected.
  • Load to RAM.
  • The terminal will open.  After a few seconds, it should look like this:

Try This - Conditional Repeat

The conditional repeat block has some additional options:

  • Try simplifying your program by using one of the options in the conditional repeat block instead of using if...do and break.
  • Try asking the user how many times to repeat using the Terminal receive number block.



Try This - Counted Loops

Sometimes, it is helpful for your loop to skip-count.  Imagine that you need to turn a motor all of the way around, but you want to stop every ten degrees - the repeat item block lets you do just that:

Now, try building a program that skip-counts:

  • Open a new project and build the blocks as shown:

  • Make sure the BlocklyProp client is running and your board's COM port is selected.
  • Load to RAM.
  • The Terminal will open and begin counting to 100 by 10’s.

Your Turn - Counted Loops

You can use the repeat item block to count down as well.  By changing the “by” value in the block, and starting with a higher number than you end with, the repeat item block will count down (decrement) instead of up.

  • Modify your program to count down.
  • Try creating a program that first counts up and then counts down.