ROBOTC.net forums
http://www.robotc.net/forums/

How do you use sensors inside functions
http://www.robotc.net/forums/viewtopic.php?f=1&t=111
Page 1 of 1

Author:  wherman [ Sun Apr 01, 2007 9:05 pm ]
Post subject:  How do you use sensors inside functions

When I copy line following code to a function, it doesn't work, but when I put it in main, it works.

Do you have to do the motor and sensors wizard inside the functions.c file that I #include into the main program or do you need that code indivdually in each function?
Do I need to pass the address to the lightSensor variable to the function?

What do other people do? :?

Bill Herman

Author:  Dick Swan [ Tue Apr 03, 2007 8:53 am ]
Post subject: 

It's a little difficult to understand what you're requesting. Please post your "sample" code so that I can comment.

When you put the code in a function, are you calling this function within "main". If not, the function will not get executed and of course the code won't run.

You only need to do the motor / sensor setup once in your single main user file.

You could pass the 'lightSensor' index (i.e. S1, S2, S3 or S4) as a parameter to your function or you could define it as a global variable / constant known throughout your program including all the include files. The second method is recommended. And of course you should assign a meaningful name to it. If you do want to pass as a parameter then you will need at least RobotC version 1.04.

Author:  HoTWiReZ [ Tue Apr 03, 2007 10:03 pm ]
Post subject: 

Hi, I'm not sure if this is what you're looking for, but I declare these as globals before any tasks or main() in my main.c:

Code:
const tSensors sonarSensor          = (tSensors) S4; 
const tSensors lightSensor          = (tSensors) S3; 


and then, I put this inside of the start of each function where I'm going to use the sensor in question.
Code:
  SensorType[sonarSensor]=sensorSONAR9V;


or,

Code:
SensorType[lightSensor]=sensorLightActive;


As an example..

You could also declare the first lines at the start of your function also but I believe you'll be unlikely to need to change them, so why not just put them in once..

Here's a task that uses the light sensor as an example of the sensor being used..
Code:
task edgeDet()
{
   nSchedulePriority=kDefaultTaskPriority;
   SensorType[lightSensor]=sensorLightActive;
   while(true)
   {
   if (SensorValue[lightSensor]<15)
      StopAllTasks();
   else EndTimeSlice();
   }
}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/