Basic Programs Lab 3
Goals:
To
reinforce previous programming experiences
To
introduce the random construct and making sounds with the RCX
Real
World Applications:
Simulated
card games, remote control cars, music boxes, alarm systems
If
you haven’t finished the previous lab, go back and
do that now.
Working
with two light sensors
We
will place a black LEGO® piece somewhere on one of our palettes.
You are to program your car to stop when it finds this piece.
Your car should not fall off the palette at any time. You can program your car in any way you’d like in
order to do this. Below is some
more programming information that you might find useful.
Helpful Trick
It
is typical in computer programs to introduce “identifiers” to make things
easier. In prog3.nqc and prog4.nqc
we have used the identifier “threshold”; we have done this through the define
capability. In the case of the define construct, the identifier
represents a constant value that will not change during execution of the
program. If we want to change
it, we have to go into the program editor, make the desired change, and then
re-compile and re-download the program.
We
can also introduce identifiers whose values can change during program execution.
These identifiers are called variables.
In NQC, we introduce variables at the top of the program by
“declaring” them as representing integer values using a declaration
statement like: “int x;”
variable names must begin with a letter and cannot have any spaces.
We
can assign values to variables using an assignment statement and we can also
form arithmetic expressions.
Examples:
|
int
x; //define variable x=0;
// The variable is equal to 0. x=x
+ 1; //adds 1 to the
variable |
There are two different types of variables, local and global. A local variable can only be “seen” by the task that it is declared inside. A global variable can be seen by all tasks.
Examples:
|
//global
variable seen in main and //in addition int
x=0; task
main(){
start addition;
until(x>=200);
OnFwd(OUT_A);
Wait(100); } task
addition(){ while
(true){
x=x+1;
} } |
//local
variable ‘x’ seen in main //only task
main(){
int x=1;
repeat(x){
OnFwd(OUT_A);
Wait(100);
x=x+1; //what will
this do?
} }
|
Another feature used in programming is the random construct. This will produce a random number each time it is used.
Examples:
|
int
x; //define variable x=
random(5); //x
will be randomly assigned a value of 0,1,2,3,4 or 5 x=
(random(3)+1)*100; //x will be assigned either 100,200,300 or 400 Wait(x); //computer will run instruction x amount of time |
Helpful Tricks
Up
until this point we have only used the “until” condition statement.
This causes the program to internally pause until the condition is met,
once it is met, it continues on. Other
condition statements work differently. For
example, the if statement introduced in this program checks the sensor
and continues on, it then branches out when the condition is met.
The difference is hard to understand so following are flowcharts which
computer programmers use to visualize programs.
See the “Advanced Programming” section for more information on if
statements.

Sample program:
|
task
main () { PlayTone(262,40);
Wait(50); PlayTone(294,40);
Wait(50); PlayTone(330,40);
Wait(50); PlayTone(294,40);
Wait(50); PlayTone(262,40);
Wait(50);
} |
Musical Tones and their Frequencies in Hertz (Hz):
| Sound | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| G# | 52 | 104 | 208 | 415 | 831 | 1661 | 3322 | |
| G | 49 | 98 | 196 | 392 | 784 | 1568 | 3136 | |
| F# | 46 | 92 | 185 | 370 | 740 | 1480 | 2960 | |
| F | 44 | 87 | 175 | 349 | 698 | 1397 | 2794 | |
| E | 41 | 82 | 165 | 330 | 659 | 1319 | 2637 | |
| D# | 39 | 78 | 156 | 311 | 622 | 1245 | 2489 | |
| D | 37 | 73 | 147 | 294 | 587 | 1175 | 2349 | |
| C# | 25 | 69 | 139 | 277 | 554 | 1109 | 2217 | |
| C | 33 | 65 | 131 | 262 | 523 | 1047 | 2093 | 4186 |
| B | 31 | 62 | 123 | 247 | 494 | 988 | 1976 | 3951 |
| A# | 29 | 58 | 117 | 233 | 466 | 932 | 1865 | 3729 |
| A | 28 | 55 | 110 | 220 | 440 | 880 | 1760 | 3520 |
Create a program that has a car playing a short song
as it backs up. Control when it
backs up by using a bump sensor. If
preferred, there is a piano that can be used to create songs (rather
than typing the frequencies). The
piano is located in the Tools menu.
If you have finished all the tasks move on to Lab 4.