Switching sensors in a program
Page 1 of 1

Author:  chris garrett [ Mon Jul 15, 2013 6:57 pm ]
Post subject:  Switching sensors in a program

I have the bot following a line using the light sensor. When it reaches the end of the line, I would like the sonar sensor to take over. I have the bot in a while loop to follow the line, but when it reaches the end of the line, it just goes in circles. How can I get the light sensor program to end and the sonar sensor take over? I can't post the program because it's on a students computer at school.

Author:  parkway [ Mon Jul 15, 2013 8:01 pm ]
Post subject:  Re: Switching sensors in a program

What is the condition in your while loop? For example, look at the code below. The condition in the while loop is an encoder count for the right motor. As long as the encoder count is less than 1800, the robot will do what is contained inside of the while loop.

#pragma config(Sensor, S1,     touchSensor,              sensorTouch)
#pragma config(Sensor, S2,     compassSensor,            sensorVirtualCompass)
#pragma config(Sensor, S3,     lightSensor,              sensorLightActive)
#pragma config(Sensor, S4,     sonarSensor,              sensorSONAR)
#pragma config(Motor,  motorA,          gripperMotor,       tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          rightMotor,         tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          leftMotor,          tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++++
Line Track for Distance
This program uses 1 NXT Line Follower Sensor to track a black line on a light(er) surface
for 5 rotations of the encoders. There is a two second pause at the beginning of the program.

The threshold variable defined in this program may need to be changed for the line/surface your
robot is tracking and the lighting conditions.

Robot Model(s): NXT REMBOT

[I/O Port]          [Name]              [Type]                [Description]
Motor Port 2        rightMotor          NXT Motor             Right side motor
Motor Port 3        leftMotor           NXT Motor             Left side motor
Sensor Port S3      lightSensor         NXT Line Follower     Front-center, facing down


//+++++++++++++++++++++++++++++++++++++++++++++| MAIN |+++++++++++++++++++++++++++++++++++++++++++++++
task main()
  wait1Msec(2000);          // The program waits for 2000 milliseconds before continuing.

  int threshold = 45;       /* found by taking a reading on both DARK and LIGHT    */
                            /* surfaces, adding them together, then dividing by 2. */

  nMotorEncoder[rightMotor] = 0;  // Reset the right motor encoder to 0.

  //Line Track for 5 rotations...
  while(nMotorEncoder(rightMotor) < 1800)
    // sensor sees light:
    if(SensorValue(lightSensor) < threshold)
      // counter-steer left:
      motor[leftMotor]  = 55;
      motor[rightMotor] = 25;
    // sensor sees dark:
      // counter-steer right:
      motor[leftMotor]  = 25;
      motor[rightMotor] = 55;

Author:  chris garrett [ Tue Jul 16, 2013 8:05 am ]
Post subject:  Re: Switching sensors in a program

Thanks for responding. I'm at school, so here is the program.

#pragma config(Sensor, S1, sonarSensor, sensorSONAR)
#pragma config(Sensor, S2, lightSensor, sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
while(SensorValue(sonarSensor) > 30)
while(SensorValue(lightSensor) < 62.5)
motor[motorB] = 50;
motor[motorC] = 0;

while(SensorValue(lightSensor) > 62.5)
motor[motorB] = 0;
motor[motorC] = 50;

motor [motorB] = 0;
motor [motorC] = 0;
wait1Msec (2000);

Author:  parkway [ Tue Jul 16, 2013 12:58 pm ]
Post subject:  Re: Switching sensors in a program

Hi Chris,

Everything you need can be found in this video:

Let me know if you have any questions after viewing the video.

Author:  chris garrett [ Wed Jul 17, 2013 8:43 am ]
Post subject:  Re: Switching sensors in a program

I used the encoders, as you suggested, and it worked out very well.
Thank you for your help.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group