1 / 27

More switches, Comparison Day 7

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.

Download Presentation

More switches, Comparison Day 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More switches, ComparisonDay 7 Computer Programming through Robotics CPST 410 Summer 2009

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

  3. Review

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

  5. Switch3.rbt Harry Howard, CPST 410, Tulane University

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

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

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

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

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

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

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

  13. Comparison Kelly §15

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

  15. ComparisonTest1.rbt Harry Howard, CPST 410, Tulane University

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

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

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

  19. ComparisonTest2.rbt Harry Howard, CPST 410, Tulane University

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

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

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

  23. ComparisonTest3.rbt Harry Howard, CPST 410, Tulane University

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

  25. ComparisonTest4.rbt Harry Howard, CPST 410, Tulane University

  26. Play with it

  27. Next time • Comparison in NXC. • The RANGE and LOGIC blocks. Harry Howard, CPST 410, Tulane University

More Related