Basic Programs Lab 3

Standards and Benchmarks

 

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

              

  1. If you haven’t finished the previous lab, go back and do that now.

 

 Working with two light sensors

 

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

 

Working with two bump sensors

  1. Create a program that allows you to control your car with two bump sensors.  The car should go forward when both sensors are pressed and stop when neither one is pressed.  If only one is pressed the car should turn in that direction. If you need help, refer to prog4.nqc

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.  

  1. Ask an assistant to time you on our obstacle course.  Each team member should be controlling one of the bump sensors for this run through the course.  Try to do this in the quickest time possible.  You have only one try.  If you fall off the edge, you are disqualified. 
  1. You will run the obstacle course again only this time one person is blindfolded.  The blindfolded person controls the car; the other person gives directions to navigate the course.  Again, try to do this in the quickest time possible.  You have only one try.  If you fall off the edge, you are disqualified.

Making Sound

  1. In addition to running the motors or acknowledging sensors, the RCX can also make sounds.   It does so via the PlayTone command.  For example, the command PlayTone(262,100); will cause note 262 (Middle C) to be played for 1 second.  The 262 is actually the note’s frequency in Hertz (Hz), 100 is how long the note plays in centiseconds.  A centisecond is 1/100th of a second (a centimeter is to a meter just as a centisecond is to a second) .  The PlayTone command is executed immediately.  So, if you want to play a second note, you should insert a Wait after the first PlayTone so the second note doesn’t overrun the first.  Copy the following program, compile it, download it and run it.

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. The piano can be used by placing the RCX in front of the IR tower and pressing the piano keys.  The sound can then be heard through the RCX.  If the sound is not correct press 'Clear' and start over.  Otherwise press 'Copy', go to a program, and 'Paste' the music through the Edit menu.

If you have finished all the tasks move on to Lab 4.

Back to Contents