More switches Day 6 Computer Programming through Robotics CPST 410 Summer 2009
7/1/09Harry Howard, CPST 410, Tulane University2 Course organization Course home page Course home page ( 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/09Harry Howard, CPST 410, Tulane University4 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/09Harry Howard, CPST 410, Tulane University5 Stop.rbt
7/1/09Harry Howard, CPST 410, Tulane University6 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/09Harry Howard, CPST 410, Tulane University7 The endless loop task main() { while (true) { // statements here }
7/1/09Harry Howard, CPST 410, Tulane University8 Stop.nxc first version task main() { while (true) { PlayTone(440,500); Wait(500); if (Sensor(S1) ????) { break; }
7/1/09Harry Howard, CPST 410, Tulane University9 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/09Harry Howard, CPST 410, Tulane University10 Protect your robot Tribot, move forward until the Touch sensor is released.
7/1/09Harry Howard, CPST 410, Tulane University11 TouchStop.rbt
Pick a card, any card Kelly §12
7/1/09Harry Howard, CPST 410, Tulane University13 The RANDOM block In the Complete palette under the Data icon. It generates a number between 0 and 100, randomly.
7/1/09Harry Howard, CPST 410, Tulane University14 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/09Harry Howard, CPST 410, Tulane University15 DisplayRandom.rbt
7/1/09Harry Howard, CPST 410, Tulane University16 Random numbers in NXC Random() Returns a single number chosen randomly between 0 and 2 16 or 65,536. Random(n) Returns a single number chosen randomly between 0 and n-1.
7/1/09Harry Howard, CPST 410, Tulane University17 Display a random number in NXC
7/1/09Harry Howard, CPST 410, Tulane University18 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 view Control > Value Type > Number Conditions 1. > 1 2. > 2 + 3. > 3
7/1/09Harry Howard, CPST 410, Tulane University19 Switch3.rbt
7/1/09Harry Howard, CPST 410, Tulane University20 Next time P2 Multiple switches in NXC. Comparison: Kelly §15. The RANGE and LOGIC blocks: Kelly §16-17.