//PROGRAM # 5

// scanner2-// Scanner2- Same as scanner program (above) with BUMP A LOT incorporated

// (FOR FUN, set light threshold to local cat color: rename program CAT CHASER)

 

// sensors- Calls the sensors at port 1 “EYE”, port 2 “BUMP”, & port 3 “ANGLE”

#define EYE   SENSOR_1

#define ANGLE SENSOR_3

#define BUMP SENSOR_2

 

// motors- Calls the motors “LEFT”, “RIGHT”, and “HEAD”

#define LEFT OUT_A

#define HEAD OUT_B

#define RIGHT OUT_C

 

// other constants

#define SLOP 2                //buffer to prevent unnecessary zig-zags

#define CENTER 53             //center of rotation sensor sweep (in steps)

#define SWEEP 48               // 135 degrees

#define ALIGN_TIME   300       // 3 seconds

#define TURN_TIME    75        //3/4 second

#define THRESHOLD    35        //for light sensor

#define REV_TIME     50        //1/2 second

#define SPIN_TIME    70        //7/10 second

 

void align() //sets rotation sensor to a starting position

{

       Rev(HEAD);                 //sets motor in backward direction

       OnFor(HEAD, ALIGN_TIME);  // turns light sensor to left

       Off(HEAD);                 // stop turning

       Fwd(HEAD);                 // prepare for later activation

       ClearSensor(ANGLE);        // make this position "0"

}

 

task main()

{

       // configure sensors

       SetSensor(EYE, SENSOR_LIGHT);        //sensor 1 is light sensor

       SetSensor(ANGLE, SENSOR_ROTATION);   //sensor 3 is rotation sensor

       SetSensor(BUMP, SENSOR_TOUCH);       //sensor 2 is touch sensor

 

       align();                             //calls inline function “align”

      

       // look and go...

       On(LEFT+RIGHT+HEAD);                 //all three motors on

      

       // begin task “look” & “bump”

       start look;                             

       start bump;

 

       // keep looking back and forth

       while(true)             //necessary or RCX will only perform task once

       {

              if (ANGLE < CENTER-SWEEP) //if position of motor “HEAD” is all

              {                         //the way to the left…

                     Fwd(HEAD);         //turn the other way

              }

              else if (ANGLE > CENTER+SWEEP) //if position of motor “HEAD” is

              {                              //all the way to the right…

                     Rev(HEAD);               // turn the other way

              }

                         

       }//while

}//task

 

//if light sensor detects a value greater than the threshold value, the motor

//turning the light sensor will stop and the robot will turn toward the light

task look()                      

{

       while(true)                     //necessary to perform more then once

       {

              if (EYE >= THRESHOLD)     //light sensor detects bright light

              {

                     Off(HEAD);    // stop looking

                     PlaySound(SOUND_CLICK);     //makes clicking noise

                    

                     if (ANGLE < CENTER-SLOP)    //if light is to the left…

                     {

                           // turn left

                           Rev(LEFT);

                           Wait(TURN_TIME);

                           Fwd(LEFT);

                     }

                     else if (ANGLE > CENTER+SLOP)   //if light is to the right…

                     {

                           // turn right

                           Rev(RIGHT);

                            Wait(TURN_TIME);

                           Fwd(RIGHT);

                     }

                    

                     On(HEAD);     // resume looking

              }//if

       }//while

}//task

 

//this task has the robot back up, turn (random right or left) and move forward

//if the touch sensor is pressed. It also plays a sound before turning

task bump()

{

       while(true)         //necessary or RCX will only perform task once

       {

         until(BUMP==1);           //touch sensor pressed

         {         

              int x = Random(2);   //x is random variable (either 0 or 1)

              if (x<1)           //if x is 0, play “right” sound and turn right

              {

                sound();           //Calls inline function “sound”

             // back up

              Rev(LEFT+RIGHT);

              Wait(REV_TIME);

             

              // spin around

              Fwd(LEFT);

              Wait(TURN_TIME);

             

              // resume

              Fwd(RIGHT);

 

              }

              else           //if x is 1, play “left” sound and turn left

              {

              sound2();         //calls inline function “playsoundL”

             // back up

              Rev(LEFT+RIGHT);

              Wait(REV_TIME);

             

              // spin around

              Fwd(RIGHT);

              Wait(TURN_TIME);

             

              // resume

              Fwd(LEFT);

              }

                    

             

       }

}

//inline function plays note at frequency 440 for 1/2 second

void sound()

{

   PlayTone(440,50);

}

//inline function plays note at frequency 660 for 1/2 second

void sound2()

{

   PlayTone(660,50);

}