View unanswered posts | View active topics It is currently Sun May 27, 2018 4:15 am

Reply to topic  [ 2 posts ] 
Using two light sensors 
Author Message

Joined: Wed Jan 04, 2012 9:35 am
Posts: 2
Post Using two light sensors
I am very new to this...

I am trying to get my robot to line track using lightsensor 1 and then use lightsensor 2 to detect a second line, stop for 1 sec and then drive again. I thought this code would have that effect:

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

task main()
   wait1Msec(50);                        // The program waits 50 milliseconds to initialize the light sensor.

   while(SensorValue[lightSensor2] > 42)                           // Infinite loop
      if(SensorValue[lightSensor1] < 43)  // If the Light Sensor reads a value less than 43:
        motor[motorC] = 60;                  // Motor C is run at a 60 power level.
      motor[motorB] = 20;                  // Motor B is run at a 20 power level.
      else                               // If the Light Sensor reads a value greater than or equal to 43:
        motor[motorC] = 20;                  // Motor C is run at a 20 power level.
      motor[motorB] = 60;                  // Motor B is run at a 60 power level.

   motor[motorC] = 0;
   motor[motorB] = 0;
   motor[motorC] = 50;
   motor[motorB] = 50;

When I run the code. The robot follows the line with lightsensor1 then when lightsensor2 detects the line, the robot stops, but it doesn't goes again.

Could you also help me with how to teach the robot to resume line tracking with lightsensor 1 after passing the line detected by lightsensor2?

Thank you!

Wed Jan 04, 2012 9:40 am
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Using two light sensors
The condition statement in the while loop is what determines when the come inside the curly braces will stop executing. Since you have that condition based on the second line sensor, when you detect that line with it, the line following code will stop. Perhaps the simplest thing to do would be to put the line following in a function, and arrange the code like this:
void followLine() {
    //line following code here
task main(){
  while(sensorvalue[lightsensor2] < 40){
  while(something else){

sudo rm -rf /

Wed Jan 04, 2012 12:35 pm
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.