Tuesday, December 13, 2011

Tip #2 for Toop 77 Robotics Merit Badge

Justin attacked his program tonight and he got most of it.  One thing about programming, it teaches you patience and problem solving.  A sensor can be thought of as eyes.  They are constantly providing a "feedback loop" to the brain.  We see a ball coming right at us, the body moves due to the eyes or "sensors" telling the brain "controller" what to do like "duck" or "stop".

In programming speak this means we must use a "loop" which is constantly getting feedback from the sensor.  Like 2000 time per second!  We are wanting the bot to sense if it is getting close to something.  Think of this as a Yes, I'm near or No, I'm far away.  This is also known as a switch, on or off.  Or, close or not close.  So, we put a switch inside the loop.  So, 2000 time per second the program is asking this switch are you close or not?  When it gets a YES, I'm close, stop the loop and wait for the next command.  In my sample, I use a sound block saying "object detected".  Why not, it is one of the sound options!

Here is where the tricky part comes.  How do you exit the loop or "get off the merry go round?  The only way I know how is to use a "logic loop" and a "variable".  Ya, not very intuitive but like any language, you have to learn something new.  And once you read this, you will completely get it and use it often in programming.

I create a variable called EndLoop.  Set the end loop to false outside the loop.  Put a "write to" variable right after you stop the motors in the switch which will instantly change the EndLoop variable from FALSE to TRUE. (remember, you set it to FALSE outside the loop)  Then, in the loop, have the EndLoop variable write the value to the logic loop using a wire.  So, when the EndLoop is true, stop the loop.  We can cover this Wednesday night for those of you who have had little to no NXT-G experience.


No comments:

Post a Comment