more switches day 6 computer programming through robotics cpst 410 summer 2009

20
More switches Day 6 Computer Programming through Robotics CPST 410 Summer 2009

Upload: ashley-owen

Post on 18-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

More switchesDay 6

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.

Stop it

Kelly §13

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

The STOP block

The STOP block, in the Complete palette under the Flow icon, stops a program from continuing.It is a good way to break out of loops.

Tribot, beep until the Touch sensor is pressed.

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

Stop.rbt

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

The NXC analog

NXC has a constant ‘break’ that breaks a loop whenever it is encountered in a program.

How would you formulate the previous program for NXC?

First you need an endless loop.

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

The endless loop

task main()

{

while (true)

{

// statements here

}

}

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

Stop.nxc first version

task main(){

while (true){

PlayTone(440,500);Wait(500);if (Sensor(S1) ????){

break;}

}}

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

Stop.nxc final version

task main(){

SetSensorMode(S1, SENSOR_MODE_PERCENT);while (true){

PlayTone(440,500);Wait(500);if (Sensor(S1) < 98) // unpressed = 100!{

break;}

}}

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

Protect your robot

Tribot, move forward until the Touch sensor is released.

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

TouchStop.rbt

Pick a card, any card

Kelly §12

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

The RANDOM block

In the Complete palette under the Data icon.It generates a number between 0 and 100,

randomly.

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

Display a random number

Tribot, display a random number between 20 and 80 every time the left NXT button is pressed and then beep, until the right button is pressed.

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

DisplayRandom.rbt

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

Random numbers in NXC

Random()Returns a single number chosen randomly

between 0 and 216 or 65,536.

Random(n)Returns a single number chosen randomly

between 0 and n-1.

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

Display a random number in NXC

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

More than two choices

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

Drag out a SWITCH block and turn off Flat viewControl > ValueType > NumberConditions

1. > 12. > 2+ 3. > 3

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

Switch3.rbt

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

Next time

P2Multiple switches in NXC.Comparison: Kelly §15.The RANGE and LOGIC blocks: Kelly

§16-17.