Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors.

Similar presentations


Presentation on theme: "Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors."— Presentation transcript:

1 Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors

2 Learning objectives: 1. Introduce the forever loop. 2. Introduce ifs.

3

4 Looping forever Repeats whatever is inside of the loop forever. (It can also optionally repeat something a specific number of times.)

5 Looping forever What does this program do?

6 Looping forever 1. Remove the first pause and run it. Q1: What do you observe? 2. Put the first pause back in and remove the second one. Run it. Q2: What do you observe? 3. Remove all pauses. Run it. Q3: What do you observe? 4. 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)?

7

8 Program P1: Artificial Life Program P1 artificial life (?) Write a Scribbler program that does the following: 1. Move the Scribbler forward for about ¼ second. 2. 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.

9

10 What can we read/observe? 1. Line sensors 2. Obstacle sensors 3. Crash (stall) 4. Light sensors 5. Coin toss

11 What can we read/observe? Obstacle sensors: 1. Left 2. Right

12

13 IF - ELSE 1. Observe a sensor. 2. Conditional tiles are used to create a fork in the road of your program's execution. 3. Depending on the outcome of the chosen condition, your program may continue in one of two different directions (but not both).

14 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.

15 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.

16 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.

17 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.

18 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.

19 IF - ELSE When a conditional is placed, it creates several locations where additional tiles may be inserted.

20 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.

21 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.

22 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.

23

24 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.

25 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.

26

27 What can we read/observe? Obstacle sensors

28 Obstacle sensor condition 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.

29

30 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.

31

32 Lab assignment deliverables The Subject of your single email must be: CSC 120 IF lab (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.


Download ppt "Looping forever Reading/observing sensors (inputs) Reacting to sensors Obstacle sensors."

Similar presentations


Ads by Google