Basic Programs Lab 4

 

Standards and Benchmarks

 

Goals:

            To introduce dual RCX communication

            To determine other ways of using light sensors

Real World Applications:

Remote controls, simulations of bats

 

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

 

Dual RCX Communication

 

  1. Ask an assistant for a second RCX. Sometimes two different computers need to communicate with one another.  NQC provides for this via the same mechanism used to download programs, the infrared transmitter/receiver built into the yellow RCX brick.  Your RCX can send a message to other RCX’s via the SendMessage statement.  This statement allows you to send a value (0-255) over the infrared port.  For example

SendMessage(10);

Sends a value of 10 out over the infrared port.  Other computers can check this message and respond to it as they wish.  Using this capability requires that you program 2 RCX bricks.  These programs must be individually downloaded to the correct RCX. We will call one master and the other slave.  These are two separate programs and need to be two separate files.

task main() {  // This program is for the Master

          SendMessage(1); Wait(200);

          SendMessage(2); Wait(200);

          SendMessage(3);

      }

           

task main() { // This program is for the Slave

               while(true) {

                  ClearMessage();

                  until (Message() !=0);   // != symbolizes “not equal to”

                  if(Message ()== 1) {OnFwd(OUT_A + OUT_C);}

                  if(Message ()== 2) {OnRev(OUT_A + OUT_C);}

                  if(Message ()== 3) {Off(OUT_A + OUT_C);}

                  }

      }

           

Copy these two programs, compile them and download them.   What do you expect to happen?  Run the program.  Did it do what you expected?

Experiment with the infrared communication capability.  How far apart can the RCX’s be?  Does it make a difference what way they are facing? What happens if both RCX’s aren’t started at the same time?  Etc.

 

3.      Create a program that uses one RCX with two bump sensors to control another RCX.  This will be similar to Lab 2 Step 5. 

 

  1. Find another group or two in the class to work with for the next task.  Program the RCX’s to perform a square dance.  One RCX should be the caller and at least two more RCX’s act as dancers.  Use your own creativity regarding the calls and corresponding movements.  Each dancer need not respond in the same way but they should take the same amount of time in each of their responses to the call.  If you need help starting, have your cars move forward for two seconds then backwards for two seconds and go from there.

 

Proximity Sensor

 

  1. For the next exercise you will only need one RCX.  Please return the extra one you have been using.

 

We have just looked at how the infrared port can send a message to another computer.  We will now examine how a reflective sensor (blue light sensor) can be used in conjunction with this.  A reflective sensor can be used to tell when a computer is sending messages via the infrared port.  It can not tell what is being sent, only that something is being sent.  Set up the RCX to repeatedly send a message (via the SendMessage command).  Mount the reflective sensor above the infrared port so that it faces straight ahead.  At different distances the reflective sensor sees infrared light reflected back to it.  In this way, your car can detect an object ahead of it. 

 

 

Before seeing the infrared light the reflective sensor will see the other light in the room.  The numbers between these two different types of light are about 100-150 apart.

Create a program that will cause your car to move forward until it detects an object approximately 6 inches in front of it.  Following is some more programming information that may help you with this task.

 

There is a way to put the sensor in “raw” mode.  This will return a value between 0 and 1023 rather than 0-100.  This will give you a more precise sensor reading.  To do this, type:

SetSensorType(SENSOR_2,SENSOR_TYPE_LIGHT);

SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);

You do not need to have SetSensor(SENSOR_2, SENSOR_LIGHT); in your program.

 

If you have finished early see a lab assistant.


Back to Contents