View unanswered posts | View active topics It is currently Mon Sep 01, 2014 4:01 am






Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Light Sensor 
Author Message
Rookie

Joined: Thu Jan 27, 2011 2:23 pm
Posts: 8
Post Re: Light Sensor
Code:
#pragma config(Sensor, S1,     touchSensor,     sensorTouch)
#pragma config(Sensor, S2,     lightSensorL,    sensorLightActive)
#pragma config(Sensor, S3,     lightSensorM,    sensorLightActive)
#pragma config(Sensor, S4,     lightSensorR,    sensorLightActive)



task main()

{
  while(true)
  {
    motor[motorA] = 60;
    motor[motorB] = 60;
  } 
  if(SensorValue(touchSensor) == 1)
  {
    motor[motorA] = -60;
    motor[motorB] = 60;
    wait1Msec(1000);

    motor[motorA] = 60;
    motor[motorB] = 60;
  }

  int threshold;
  {
    //threshold = 30;

    /*
    bool lineDetected(tSensors lightSensorR)
    bool lineDetected(tSensors lightSensorM)     //<--- Didnt really understand what this does.
    bool lineDetected(tSensors lightSensorL)
    */

    Return (SensorValue[lightSensorR] > threshold);
    Return (SensorValue[lightSensorM] > threshold);
    Return (SensorValue[lightSensorL] > threshold);
  }

  if (threshold == 101) //Definitely an X
  {
    motor[motorA] = 0;
    motor[motorB] = 0;
    wait1Msec(3000);

    motor[motorA] = 60;
    motor[motorB] = 60;
  }


  if (threshold == 010) //means it is either a Cross or a T
  {

    if (threshold == 111) // <-- Cross
    {
      motor[motorA] = -60;  // <-- 180º rotation
      motor[motorB] = 60;
      wait1Msec(3000);

      motor[motorA] = 60;   // <-- Reverse Start
      motor[motorB] = 60;
    }

    Else         //Otherwise it's a T

    {
      PlaySound(soundBlip);
    }
  }

}


So the program is something like this, but i got some errors/ questions that i dont understand.

**Error**:Functions must be defined at main scope level
**Error**:No body declared for procedure
*Warning*:Unreferenced function 'lineDetected'
**Error**:Expected->'}'. Found 'bool'
**Error**:Functions must be defined at main scope level
**Error**:No body declared for procedure
*Warning*:Unreferenced function 'lineDetected'
**Error**:Expected->'}'. Found 'bool'
**Error**:Functions must be defined at main scope level
**Error**:No body declared for procedure
*Warning*:Unreferenced function 'lineDetected'
**Error**:Expected->'}'. Found 'Return'
**Error**:Undefined procedure 'Return'.
*Warning*:expecting '[' and found '{'. Typing error assumed and '[' used.
**Error**:Missing ']' before ';'
**Error**:Unexpected scanner token-> '}'

//bool lineDetected(tSensors _sensor) I didnt understand what this does, I already search and didnt find nothing to explain what it does. I already try to put this inside of a function, tried to read some topics so I could understand, but it only gives me more errors.

And I dont know how to use threshold, I dont know if I can put threshold receiving the 3 sensors value, and if threshold will actually works this way, I still havent complete my robot so cant really test this, but in my mind threshold can't receive 3different things. So I very lost in this code.


Thu Jul 05, 2012 10:07 pm
Profile
Rookie

Joined: Thu Jan 27, 2011 2:23 pm
Posts: 8
Post Re: Light Sensor
And how can i turn off the light sensors for a few secs?


Thu Jul 05, 2012 10:14 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 566
Post Re: Light Sensor
Couple of problems I'm seeing with the code (besides the error messages);

Code:

  while(true)
  {
    motor[motorA] = 60;
    motor[motorB] = 60;
  }
 


Your code will get stuck here. The "While" loop checks to see if a condition is true, and if it is it then runs the code within the brackets and rechecks to see if the condition is still true when it hits the end bracket. You've made what's called an infinite loop; "True" will always be "True", so the code will always run. The problem is that the only code within the brackets is the commands to move the motors forward so that's all the robot will do; move forward.

Code:

  int threshold;
  {
    //threshold = 30;

    /*
    bool lineDetected(tSensors lightSensorR)
    bool lineDetected(tSensors lightSensorM)     //<--- Didnt really understand what this does.
    bool lineDetected(tSensors lightSensorL)
    */

    Return (SensorValue[lightSensorR] > threshold);
    Return (SensorValue[lightSensorM] > threshold);
    Return (SensorValue[lightSensorL] > threshold);
  }



This is a bit more complex. Generally the threshold value is a value that you pre-set that tells the robot if a sensor value is between two conditions. In this case, anything above the threshold would be considered "light" and anything below the threshold would be considered "dark". I believe for this particular set of code is trying to find a function called "lineDetected", send up the individual sensor values, and get a boolean (1 or 0) value back depending if the sensor is above or below the threshold value. Although more efficient, it's a bit more complex going this route. There are two alternative routes that come to mind right this moment (there are probably many, many other options);

One option is to use multiple multi-condition if/else statements:
Code:
 
if (sensorValue[lightSensorR]< threshold && SensorValue[lightSensorM]< threshold && SensorValue[lightSensorL]<threshold)
{
  //Code for "111" result
}

This will basically check for "111" and then you can code accordingly. Changing any of the conditions to a '>' value (meaning, the sensor is reading light, aka not a line) will change the comparison to a 0) and then code the robot to perform actions accordingly. The problem with this is that you will have a good bit of code to go through, and the more code you have the more places you have to make an error.

Another option (and this is a bit more complex) is to use if statements to read the values from the sensors, append them to a string, and then read the string directly and compare it in a switch statement. This approach is a bit more complex but cuts down the amount of code by a good bit.

The absolute best way to do this, though, is the way that is most comfortable for you :) When you make the code your own, you truly master it and can start tapping into the deeper powers of the programming languages.

Watch the "Forward Until Dark" videos under the "Sensing" tab to learn a bit more about threshold values:
http://www.education.rec.ri.cmu.edu/pre ... x_preview/

If you decide to go the string/switch route, you can learn about the 'strcat' command at our wiki:
http://www.robotc.net/wiki/General/Strings

And you can learn about If/Else and Switch statements a bit more in "Line Tracking" under "Sensing":
http://www.education.rec.ri.cmu.edu/pre ... x_preview/

The good news is that this knowledge will help you in not only other ROBOTC programs but also in most other programming languages, as the general principles (string manipulation, comparative operators, conditional statements) are shared across languages.

The absolute best thing I can recommend is to start from scratch and make your own code. That way you go through the trials-and-tribulations, you learn more from it, and you can debug it better if it goes wrong. Plus, you avoid using code that is overly complex for what you are trying to do or contains extraneous code.

Good luck, and keep us updated!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Wed Jul 11, 2012 9:18 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Light Sensor
A couple of other obvious problems (I say obvious because this code does not compile or run, and that would be critical before anything could work or not)

Code:
int threshold;
  {
    //threshold = 30;

    /*
    bool lineDetected(tSensors lightSensorR)
    bool lineDetected(tSensors lightSensorM)     //<--- Didnt really understand what this does.
    bool lineDetected(tSensors lightSensorL)
    */

    Return (SensorValue[lightSensorR] > threshold);
    Return (SensorValue[lightSensorM] > threshold);
    Return (SensorValue[lightSensorL] > threshold);


'int threshold' should not be followed by a semi-colon. 'Return' should be all lower case.

I'm guessing from 'Didnt really understand what this does' comment you are very new to programming. As such, you need to start with a much more simple program until you get comfortable. learn to flowchart (http://en.wikipedia.org/wiki/Flowchart) your program, then write code to match your flowcharts. It doesn't take long to learn how to do this and is well worth the effort.

Programmers just starting out need a FAQ, and indicate they have read an applied it before posting, IMHO. It should contain things like 'will it compile?', 'have you formatted your code and verified the indentation appears as you imagined?' 'does your comments match your code or did you just copy lines, change code and leave the old comment?'

Am I being too cynical?

_________________
Mike aka Spiked3
http://www.spiked3.com


Wed Jul 11, 2012 12:44 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 19 posts ]  Go to page Previous  1, 2

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.