Presentation is loading. Please wait.

Presentation is loading. Please wait.

More switches, Comparison Day 7 Computer Programming through Robotics CPST 410 Summer 2009.

Similar presentations


Presentation on theme: "More switches, Comparison Day 7 Computer Programming through Robotics CPST 410 Summer 2009."— Presentation transcript:

1 More switches, Comparison Day 7 Computer Programming through Robotics CPST 410 Summer 2009

2 7/1/09Harry Howard, CPST 410, Tulane University2 Course organization  Course home page Course home page  (http://robolab.tulane.edu/CPST410/)  Lab (Newcomb 442) will be open for practice with 3-4 Macs, but you can bring your own laptop and all robots.

3 Review

4 7/1/09Harry Howard, CPST 410, Tulane University4 More than two choices  Tribot, when I press the left NXT button, pick a number from 1 to 3.  If the number is 1, display an image.  If the number is 2, beep.  If the number is 3, play a sound.

5 7/1/09Harry Howard, CPST 410, Tulane University5 Switch3.rbt

6 7/1/09Harry Howard, CPST 410, Tulane University6 Switches in NXC  A switch statement can be used to execute one of several different blocks of code depending on the value of an expression.  It has the following syntax:  switch (expression) body

7 7/1/09Harry Howard, CPST 410, Tulane University7 Case labels  One or more case labels precede each block of code.  The labels are not statements in themselves - they are labels that precede statements.  They have the following syntax:  case constant_expression :  default :

8 7/1/09Harry Howard, CPST 410, Tulane University8 Example the number of each case is a value of x int x; … switch(x) { case 1: // do something when x is 1 break; case 2: case 3: // do something else when x is 2 or 3 break; default: // do this when x is not 1, 2, or 3 break; }

9 7/1/09Harry Howard, CPST 410, Tulane University9 More on case labels  Each case must be a constant and unique within the switch statement.  The switch statement evaluates the expression then looks for a matching case label.  It will then execute any statements following the matching case until either a break statement or the end of the switch is reached.  A single default label may also be used - it will match any value not already appearing in a case label.

10 7/1/09Harry Howard, CPST 410, Tulane University10 So now do the task in NXC  Tribot, when I press the left NXT button, pick a number from 1 to 3.  If the number is 1, display an image.  If the number is 2, beep.  If the number is 3, play a sound.

11 7/1/09Harry Howard, CPST 410, Tulane University11 First try: just the switch int num; task main() { num = Random(3); // Random starts at 0 switch(num) { case 0: GraphicOut( “ Smile 01.ric ” ); Wait(1000); break; case 1: PlayTone(440, 500): Wait(500); break; case 2: PlayFile( “ Laughing 02.rso ” ): Wait(500); break; }

12 7/1/09Harry Howard, CPST 410, Tulane University12 The switch in a while loop int num; task main() { while (SensorBoolean(S1) == true) // touch sensor on port S1 { num = Random(3); switch(num) { // Random starts at 0 case 0: GraphicOut( “ faceopen.ric ” ); Wait(1000); ResetScreen(); // otherwise graphic never goes away break; case 1: PlayTone(440, 500); Wait(500); break; case 2: PlayFile( “ Laughing 02.rso ” ); Wait(500); break; }

13 Comparison Kelly §15

14 7/1/09Harry Howard, CPST 410, Tulane University14 The challenge  Tribot, 1. choose two numbers randomly between 0 and 9 (call them A and B), 2. and display them on the screen. 3. Then, if A is greater than B, display “true”; otherwise, display “false”.

15 7/1/09Harry Howard, CPST 410, Tulane University15 ComparisonTest1.rbt

16 7/1/09Harry Howard, CPST 410, Tulane University16 Combining text  We now have two numbers to display, but a DISPLAY block can only display one at a time.  Thus the two numbers have to be combined or concatenated into a single bit of text.  The TEXT block does this.

17 7/1/09Harry Howard, CPST 410, Tulane University17 The TEXT block  Drag a TEXT block out of the Advanced palette - it has the icon “a”.  Pull out its hub all the way:  It has inputs for 3 pieces of text,  or text can be written in the settings windows.

18 7/1/09Harry Howard, CPST 410, Tulane University18 Display the two numbers  Drop a TEXT block at the end.  Plug textualized number A into TEXT plug A.  Plug textualized number B into TEXT plug C.  Enter ” > " into the text box of TEXT plug B.  Drop a DISPLAY block after the TEXT block.  Connect the TEXT block to it via the Text plugs.  Raise the text to line 3.  Don’t forget to WAIT for the display!  Test the program.

19 7/1/09Harry Howard, CPST 410, Tulane University19 ComparisonTest2.rbt

20 7/1/09Harry Howard, CPST 410, Tulane University20 Now, the comparison  This program gives a dumb result whenever A is less than or equal to B.  As a first step towards making it less dumb, let us examine the COMPARE block.

21 7/1/09Harry Howard, CPST 410, Tulane University21 The COMPARE block  Pull out a COMPARE block (it has > = <) from the Data part of the Complete palette.  It has three operations:  less than,  greater than,  equals.  And two input hubs.

22 7/1/09Harry Howard, CPST 410, Tulane University22 Adding the comparison  Drop a COMPARE block at the end of the sequence bar.  Set it to ‘>’.  Where should its inputs come from?  From the number outputs of the two Number to Text blocks.  see the next slide

23 7/1/09Harry Howard, CPST 410, Tulane University23 ComparisonTest3.rbt

24 7/1/09Harry Howard, CPST 410, Tulane University24 Now, a decision  But we want to display "True" if the comparison is true, and "False" otherwise.  How do we do that?  Drop a SWITCH block at the end of the sequence bar.  Set it to tabbed view, for convenience.  Set Control to Value.  Run a wire from the COMPARE Logic plug to the SWITCH input plug.  Now fill in each condition with a DISPLAY block with the appropriate text on line 6, and don’t clear the screen.  Don’t forget to add a WAIT at the end!

25 7/1/09Harry Howard, CPST 410, Tulane University25 ComparisonTest4.rbt

26 Play with it

27 7/1/09Harry Howard, CPST 410, Tulane University27 Next time  Comparison in NXC.  The RANGE and LOGIC blocks.


Download ppt "More switches, Comparison Day 7 Computer Programming through Robotics CPST 410 Summer 2009."

Similar presentations


Ads by Google