January 6, 2011

Again, and again, and again... the never ending loop for beginners

Have you ever wanted to make a car that went in circles forever? Or perhaps you want it to repeat the same pattern for eternity? Well, that is impossible. Eventually the battery will run out and you'll have to recharge or replace it. But how do you program something that would go on forever if not for the batteries dying? The impossible can be made semi possible with the loop block! The loop block enables you to repeat the same block or series of blocks over and over, either for eternity or for a certain number of times. Open your NXT programming program and take a look at the loop block. As you can see, the loop block itself looks to be never ending; the orange arrow pointing back to the beginning of the orange arrow...

How to use the loop block
When you first place the loop block in your programming project, it's already in the "forever" mode. All you have to do is drag and drop blocks into the loop, and when you download and run it, the block(s) will be repeated until the battery runs out- or you end the program. That was easy, but what are the other settings?

Sensor
This is arguably the most useful setting on the loop block. It allows you to have the block(s) inside the loop repeat until it gets a "true" message from the sensor/nxt buttons/bluetooth. This helpful for making robots that don't run into walls... for example, using it to go straight until it sees a wall 15 cms in front of it, then turn right.

Time
Do you want your NXT to run a series of blocks for 30 seconds? This block is for setting a block to end in exactly the time needed, it will even stop in the middle of the loop to end on time! To tell you the truth, I actually haven't even used this block before... I'm not sure what it would be helpful with.

Count
I've used this on the most often. It is used to make the loop block run a certain number of times. After it has run the specified number of times, it will move on to whatever blocks you have in line after the loop. I used the "count" setting in my project "The Enterprise" to slowly ramp up the speed. In the last paragraph of this post, there is a small description of how that works. If you would like more information about how acceleration works, please go here. Or you can check out "The Enterprise" on the NXTLog here. 

Logic
To put it simply, the logic loop is good for having a block(s) repeated until it gets a message through a logic wire sending the correct value.

Finally, what happens when you check the "show" box at the bottom of all the loop settings?
When you check the "show" box, it makes it so the loop will count how many times it has been completed. You can use that as input for a number data wire, and transfer as a number to another block. That's what I used in "The Enterprise"; I had the speed of the wheel equal the number of times the loop had been completed. That created a realistic acceleration look.

I hope you enjoyed this post as much as I enjoyed writing it. If you have any questions, I would love to hear them. Thanks!

No comments:

Post a Comment

Your comment will be moderated as soon as possible. Thank you!

TopOfBlogs