270 likes | 393 Views
More switches, Comparison Day 7. Computer Programming through Robotics CPST 410 Summer 2009. Course organization. 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. Review.
E N D
More switches, ComparisonDay 7 Computer Programming through Robotics CPST 410 Summer 2009
Course organization • 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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
Switch3.rbt Harry Howard, CPST 410, Tulane University
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 Harry Howard, CPST 410, Tulane University
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 : Harry Howard, CPST 410, Tulane University
Examplethe 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; } Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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; } } Harry Howard, CPST 410, Tulane University
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; } } } Harry Howard, CPST 410, Tulane University
Comparison Kelly §15
The challenge • Tribot, • choose two numbers randomly between 0 and 9 (call them A and B), • and display them on the screen. • Then, if A is greater than B, display “true”; otherwise, display “false”. Harry Howard, CPST 410, Tulane University
ComparisonTest1.rbt Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
ComparisonTest2.rbt Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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. Harry Howard, CPST 410, Tulane University
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 Harry Howard, CPST 410, Tulane University
ComparisonTest3.rbt Harry Howard, CPST 410, Tulane University
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! Harry Howard, CPST 410, Tulane University
ComparisonTest4.rbt Harry Howard, CPST 410, Tulane University
Next time • Comparison in NXC. • The RANGE and LOGIC blocks. Harry Howard, CPST 410, Tulane University