View unanswered posts | View active topics It is currently Tue Jul 29, 2014 2:50 am






Reply to topic  [ 3 posts ] 
How do you use sensors inside functions 
Author Message
Rookie

Joined: Sun Mar 04, 2007 3:02 am
Posts: 9
Post 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


Sun Apr 01, 2007 9:05 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 
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.


Tue Apr 03, 2007 8:53 am
Profile
Rookie

Joined: Fri Mar 16, 2007 9:12 am
Posts: 26
Post 
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();
   }
}


Tue Apr 03, 2007 10:03 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 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.