The Scribbler GUI Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors
Learning objectives: • Introduce the forever loop. • Introduce ifs.
Looping forever • Repeats whatever is inside of the loop forever. • (It can also optionally repeat something a specific number of times.)
Looping forever • What does this program do?
Looping forever • Remove the first pause and run it. Q1: What do you observe? • Put the first pause back in and remove the second one. Run it. Q2: What do you observe? • Remove all pauses. Run it. Q3: What do you observe? • Using the Scribbler programming GUI, remove the start of the forever loop. Don’t run it! Q4: What happened (when you removed the start of the forever loop)?
Program P1: Artificial Life Program P1 artificial life (?) • Write a Scribbler program that does the following: • Move the Scribbler forward for about ¼ second. • Pause for about one second. • It should repeat this sequence of operations forever. • You should also cause one or more of the LEDs to turn on and off (sort of like a heartbeat) as well. • Send a screen shot and the .scb file to me.
What can we read/observe? • Line sensors • Obstacle sensors • Crash (stall) • Light sensors • Coin toss
What can we read/observe? Obstacle sensors: • Left • Right
IF - ELSE • Observe a sensor. • Conditional tiles are used to create a fork in the road of your program's execution. • Depending on the outcome of the chosen condition, your program may continue in one of two different directions (but not both).
IF - ELSE • The simplest conditional is the IF-THEN-ELSE tile (yellow). • This tile checks for the presence of an obstruction on the left and none on the right. • If this condition is met the path indicated by the green check mark is taken. • If not, the path indicated by the red "X" is taken. • So, overall, what this particular program does is turn the left LED on if there's a left obstruction and not a right one, and turns it off otherwise.
IF - ELSE • Conditionals are inserted into your program the same as other tiles. Just click on the conditional button and a cursor will appear as you mouse over the worksheet, showing you where the conditional can be inserted. When you click it into place, the conditional edit box will appear, as shown to the left.
IF - ELSE • Here you can select what condition you want to test by clicking one of the five multiple-choice boxes until that condition appears.
IF - ELSE • For most conditions, the slider at the bottom can be used to place further restrictions on when the condition is TRUE, such as a required number of consecutive observations of that condition.
IF - ELSE • The remaining button is used to switch the TRUE and FALSE directions. • Notice that the reversed directions are highlighted in black to make them more obvious in your program.
IF - ELSE • When a conditional is placed, it creates several locations where additional tiles may be inserted.
IF - ELSE • When a conditional is placed, it creates several locations where additional tiles may be inserted. • One is immediately to the bottom of the tile and is normally the program fork corresponding to a TRUE condition.
IF - ELSE • When a conditional is placed, it creates several locations where additional tiles may be inserted. • Another insertion point is the arrow to the right of the tile. This is normally the path taken by the FALSE condition.
IF - ELSE • When a conditional is placed, it creates several locations where additional tiles may be inserted. • A third insertion point is the arrow below the conditional tile. This occurs where the forking program paths rejoin. A tile place here will execute regardless of whether the condition was TRUE or FALSE.
ELSE - IF • There is yet another insertion point available only if you happen to be inserting another conditional tile. It's just to the right of the conditional tile and is indicated by a vertical cursor.
ELSE - IF • By inserting additional conditionals in this fashion, you can create multi-way branching in your program, as the example illustrates. Here, all possible obstruction conditions are being tested with a motion response for each one. • Notice that the last condition (no obstruction) doesn't need an explicit test, since it will be the one that's TRUE if, and only if, the other three are FALSE.
What can we read/observe? • Obstacle sensors
Obstacle sensorcondition • The obstacle sensor uses the two infrared LEDs on the left- and right-front of the Scribbler, along with the IR detector in the middle-front to detect light reflected from obstacles in its path. Each of the four possible combinations of left and right obstacles represents a separate condition. You can also restrict how many consecutive times the condition must be observed for it to be TRUE. • In the example above, the condition of “left obstacle present and right obstacle absent” must hold for four or more successive observations for the condition to be TRUE.
Program P2: Fear • Make a copy of program P1, and use it as the basis for P2. • Add the following functionality: • Read the obstacle sensors. • Program the robot to react by moving away from the obstacle, if present (simulating fear). • Q5: Experiment and determine the range (closest and farthest) of the obstacle sensors. Describe your experiment in a paragraph and report your results.
Lab assignment deliverables • The Subject of your single email must be: • CSC 120 IF lab <your name> • (I will deduct points if you fail to do this.) • Include answers to questions Q1 through Q5. • Include the P1 .scb file and screen shot of P1. • Include the P2 .scb file and screen shot of P2.