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

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

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

More switches, Comparison

Day 7

Computer Programming through Robotics

CPST 410

Summer 2009

7/1/09 Harry Howard, CPST 410, Tulane University

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.

Review

7/1/09 Harry Howard, CPST 410, Tulane University

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.

7/1/09 Harry Howard, CPST 410, Tulane University

5

Switch3.rbt

7/1/09 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

7/1/09 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 :

7/1/09 Harry Howard, CPST 410, Tulane University

8

Examplethe number of each case is a value

of xint 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;

}

7/1/09 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.

7/1/09 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.

7/1/09 Harry Howard, CPST 410, Tulane University

11

First try: just the switch

int num;task main(){

num = Random(3); // Random starts at 0switch(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;

} }

7/1/09 Harry Howard, CPST 410, Tulane University

12

The switch in a while loopint num;

task main(){

while (SensorBoolean(S1) == true) // touch sensor on port S1{

num = Random(3); switch(num){

// Random starts at 0case 0:

GraphicOut(“faceopen.ric”);Wait(1000); ResetScreen(); // otherwise graphic never goes awaybreak;

case 1: PlayTone(440, 500);Wait(500); break;

case 2: PlayFile(“Laughing 02.rso”);Wait(500); break;

}}

}

Comparison

Kelly §15

7/1/09 Harry Howard, CPST 410, Tulane University

14

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

7/1/09 Harry Howard, CPST 410, Tulane University

15

ComparisonTest1.rbt

7/1/09 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.

7/1/09 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.

7/1/09 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.

7/1/09 Harry Howard, CPST 410, Tulane University

19

ComparisonTest2.rbt

7/1/09 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.

7/1/09 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.

7/1/09 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

7/1/09 Harry Howard, CPST 410, Tulane University

23

ComparisonTest3.rbt

7/1/09 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!

7/1/09 Harry Howard, CPST 410, Tulane University

25

ComparisonTest4.rbt

Play with it

7/1/09 Harry Howard, CPST 410, Tulane University

27

Next time

Comparison in NXC.The RANGE and LOGIC blocks.