View unanswered posts | View active topics It is currently Sat May 28, 2016 5:48 pm

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Using two light sensors
Author Message
Rookie

Joined: Wed Jan 04, 2012 9:35 am
Posts: 2
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:

 Code:#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;   wait1Msec(1000);   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
Moderator

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
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:
 Code:void followLine() {    //line following code here}task main(){  while(sensorvalue[lightsensor2] < 40){    followLine();  }  while(something else){    followLine();  }}

_________________
sudo rm -rf /

Wed Jan 04, 2012 12:35 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning