Difference between revisions of "VEX2 Sensors Overview"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with " {| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0" |- | ''Main >> [[VEX2_Ma...")
 
(Sonar)
 
(37 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
<yambe:breadcrumb self="Sensors Overview">VEX2|CORTEX</yambe:breadcrumb>
 +
<br />
  
{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
+
{|
 
|-
 
|-
|  
+
|''For ROBOTC CORTEX Sensor ''functions'', check out the [[VEX2_Functions_Sensors|CORTEX Sensor Functions]] page!''
''[[Main_Page|Main]] >> [[VEX2_Main|CORTEX]] >> [[VEX2:_Sensors_Overview|Sensors Overview]] ''
+
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 +
{{tl|1|1}}
 +
<br />
  
{| class="wikiText"
+
== Analog Sensors ==
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|''For ROBOTC CORTEX Sensor ''functions'', check out the [[VEX2:_Functions_-_Sensors|CORTEX Sensor Functions]] page!''
+
|There are 5 main types of Analog Sensors for the VEX CORTEX:
 
+
 
|-
 
|-
|}
+
|
 
+
=== Light ===
 +
[[File:Vex light sensor.png]]
 +
<br />
 +
The Light sensor, also known as the Reflection sensor, returns values ranging between 0 and 4095.  0 is the lightest reading and 4095 is the darkest.
 +
<br />
  
 
{|
 
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, in1,    lightSensor,    sensorReflection)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
 +
task main()
 +
{
 +
  wait1Msec(2000);                // wait 2 seconds before exectuing following code
 +
  bMotorReflected[port2] = true;  // reflects direction of motor on port 2
 +
 
 +
  while(true)                    // infinite loop:
 +
  {
 +
    clearLCDLine(0);                                // clear the top VEX LCD line
 +
    clearLCDLine(1);                                // clear the bottom VEX LCD line
 +
   
 +
    setLCDPosition(0,0);                            // set the VEX LCD cursor the first line, first space
 +
    displayNextLCDString("Light Sensor:");          // display "Light Sensor:" on the top line
 +
   
 +
    setLCDPosition(1,0);                            // set the VEX LCD cursor the second line, first space
 +
    displayNextLCDNumber(SensorValue(lightSensor)); // display the reading of the lightSensor sensor
 +
   
 +
    wait1Msec(50);                                  // wait 50 milliseconds to help display properly
 +
  }
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|style="vertical-align:top"| __TOC__
+
|
|-
+
|}
+
  
 +
=== Potentiometer ===
 +
[[File:Vex pot.png]]
 +
<br />
 +
Returns an analog value between 0 and 4095 ''(although mechanical stops my limit the values to between 5 and 4092)''.
 +
<br />
  
== Touch Sensor ==
+
{|
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, in1,   potentiometer,  sensorPotentiometer)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
 
 +
task main()
 +
{
 +
  wait1Msec(2000);               // wait 2 seconds before exectuing following code
 +
  bMotorReflected[port2] = true; // reflects direction of motor on port 2
 +
 
 +
  while(true)                    // infinite loop:
 +
  {
 +
    clearLCDLine(0);                                 // clear the top VEX LCD line
 +
    clearLCDLine(1);                                 // clear the bottom VEX LCD line
 +
   
 +
    setLCDPosition(0,0);                             // set the VEX LCD cursor the first line, first space
 +
    displayNextLCDString("Potentiometer:");          // display "Potentiometer:" on the top line
 +
   
 +
    setLCDPosition(1,0);                              // set the VEX LCD cursor the second line, first space
 +
    displayNextLCDNumber(SensorValue(potentiometer)); // display the reading of the potentiometer sensor
 +
   
 +
    wait1Msec(50);                                    // wait 50 milliseconds to help display properly
 +
  }
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|The NXT Touch Sensor has 2 states, pressed or unpressed. In code this is represented with a 1 for pressed, and a 0 for unpressedThere are no other states for this sensor.  Below is a simple program that keeps your program from running until you press the Touch Sensor.
+
|
|-
+
=== Line Follower ===
|<syntaxhighlight lang="ROBOTC">
+
[[File:vex_linefollower.png]]
#pragma config(Sensor, S1,     touchSensor,         sensorTouch)
+
<br />
 +
Returns values ranging between 0 and 40950 is the lightest reading and 4095 is the darkest.
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, in1,   lineFollower,   sensorLineFollower)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
  
 
task main()
 
task main()
 
{
 
{
   while(SensorValue(touchSensor) == 0) // while the Touch Sensor is inactive (hasn't been pressed):
+
   wait1Msec(2000);                // wait 2 seconds before exectuing following code
 +
  bMotorReflected[port2] = true; // reflects direction of motor on port 2
 +
 
 +
  while(true)                     // infinite loop:
 
   {
 
   {
     // DO NOTHING (wait for press)
+
     if(SensorValue(lineFollower) < 950) // if the lineFollower sensor reads a value less than 950:
 +
    {
 +
      // turn left:
 +
      motor[port2] = 50;  // motor on port 2 is run at power level 50
 +
      motor[port3] = 0;  // motor on port 3 is stopped at power level 0
 +
    }
 +
    else  // lineFollower sensor reads a value greater than or equal to 950:
 +
    {
 +
      motor[port2] = 0;  // motor on port 2 is stopped at power level 0
 +
      motor[port3] = 50;  // motor on port 3 is run at power level 50
 +
    }
 
   }
 
   }
   while(SensorValue(touchSensor) == 1) // while the Touch Sensor is active (pressed):
+
}
 +
</syntaxhighlight>
 +
   |-
 +
  |}
 +
|-
 +
|
 +
 
 +
=== Gyro ===
 +
[[File:vex_gyro.png]]
 +
<br />
 +
As the Gyro is turned, the values it returns are in '''tenths of degrees''',  positive and negative. Thus, a SensorValue of 3600 equals 360 degrees,  or one full rotation. When the sensor is mounted horizontally,  counter-clockwise movements will return values from 0 to -3600;  clockwise movements will return values from 0 to 3600. Once the gyro  completes one full revolution, the sensor value will “roll-over” to 0 by  default (for example: …3597, 3598, 3599, 3600, 0, 1, 2, 3,…). To change  the “roll-over” point, un-comment line 33 (from the code below) and change the value of  “SensorFullCount” from 3600 to the desired value (7200, 18000, ect)
 +
 
 +
''For more information on the Gyro sensor, check out the ROBOTC blog post: [http://www.robotc.net/blog/2011/10/13/programming-the-vex-gyro-in-robotc/ Programming the VEX Gyro in ROBOTC].''
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop)
 +
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop, reversed)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
 +
 
 +
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++
 +
Gyro Based Turns - Basic
 +
-This program instructs your robot to turn for the specified number of degrees in "degrees10".
 +
-For best sensor results, clear out the gyro and manually configure it at the begging of the code.
 +
-The Gyro is configured by default to provide values from 0 to -3600 for clockwise rotation,
 +
and 0 to 3600 for counter-clockwise rotation
 +
 
 +
Robot Model(s): Swervebot
 +
 
 +
[I/O Port] [Name] [Type] [Description]
 +
Motor Port 2 rightMotor VEX Motor Right side motor
 +
Motor Port 3 leftMotor VEX Motor Left side motor
 +
Analog Port 8 in8 VEX Gyro Top-center mounted,
 +
away from the Cortex
 +
--------------------------------------------------------------------------------------------------*/
 +
 
 +
task main()
 +
{
 +
  //Completely clear out any previous sensor readings by setting the port to "sensorNone"
 +
  SensorType[in8] = sensorNone;
 +
  wait1Msec(1000);
 +
  //Reconfigure Analog Port 8 as a Gyro sensor and allow time for ROBOTC to calibrate it
 +
  SensorType[in8] = sensorGyro;
 +
  wait1Msec(2000);
 +
 
 +
  //Adjust SensorScale to correct the scaling for your gyro
 +
  //SensorScale[in8] = 260;
 +
  //Adjust SensorFullCount to set the "rollover" point. 3600 sets the rollover point to +/-3600
 +
  //SensorFullCount[in8] = 3600;
 +
 
 +
  //Specify the number of degrees for the robot to turn (1 degree = 10, or 900 = 90 degrees)
 +
  int degrees10 = 900;
 +
 
 +
  //While the absolute value of the gyro is less than the desired rotation...
 +
  while(abs(SensorValue[in8]) < degrees10)
 
   {
 
   {
     // DO NOTHING (wait for release)
+
     //...continue turning
 +
    motor[rightMotor] = 25;
 +
    motor[leftMotor] = -25;
 
   }
 
   }
  
   // YOUR CODE GOES HERE
+
   //Brief brake to stop some drift
                                        // otherwise (the touch sensor has been activated [pressed] ):
+
   motor[rightMotor] = -5;
   motor[motorB] = 75;                   /* run motors B and C forwards */
+
   motor[leftMotor] = 5;
   motor[motorC] = 75;                   /* with a power level of 75    */
+
   wait1Msec(250);
   wait1Msec(1000);                     // wait 1000 milliseconds (1 second) before moving to further code
+
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|}
+
|
 +
 
 +
=== Accelerometer ===
 +
[[File:vex_accel.png]]
 +
<br />
 +
Returns an analog value between 0 and 4095.  Each Accelerometer Sensor has 3 cables, X, Y, and Z.  The ports you attach them to don't have to be in any particular order ''(however you WILL need to use 3 ports)''.
 
<br />
 
<br />
  
== Light Sensor ==
+
{|
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
  |-
|-
+
  |
|The NXT Light Sensor has a range of states between 0 and 100.  The lower the number, the darker the reading is.  The higher the number, the lighter the reading is.  Below is a simple line-following program that uses only one NXT Light Sensor.
+
<syntaxhighlight lang="ROBOTC">
|-
+
#pragma config(Sensor, in1,    xAxis,          sensorAccelerometer)
|<syntaxhighlight lang="ROBOTC">
+
#pragma config(Sensor, in2,    yAxis,          sensorAccelerometer)
#pragma config(Sensor, S3,     lightSensor,         sensorLightActive)
+
#pragma config(Sensor, in3,   zAxis,         sensorAccelerometer)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
  
 
task main()
 
task main()
 
{
 
{
  wait1Msec(50);                       // The program waits 50 milliseconds to initialize the light sensor.
+
  int nBiasValues[3];
  while(true)                          // Infinite loop
+
 
   {
+
  int X_Accel;
      if(SensorValue(lightSensor) < 45)  // If the Light Sensor reads a value less than 45:
+
  int Y_Accel;
      {
+
  int Z_Accel;
        motor[motorB] = 60;                 // Motor B is run at a 60 power level.
+
 
        motor[motorC] = 20;                 // Motor C is run at a 20 power level.
+
  wait1Msec(500); // bias values are being calculated
      }
+
 
      else                              // If the Light Sensor reads a value greater than or equal to 45:
+
  /* store the bias values in an array so that they can be 
      {
+
   * displayed in the ROBOTC global variables debug window */
        motor[motorB] = 20;                 // Motor B is run at a 20 power level.
+
  nBiasValues[0] = SensorBias[xAxis];
        motor[motorC] = 60;                 // Motor C is run at a 60 power level.
+
  nBiasValues[1] = SensorBias[yAxis];
      }
+
  nBiasValues[2] = SensorBias[zAxis];
  }
+
 
 +
  while(true)
 +
  {
 +
    /* also store the actual sensor values so that they can be
 +
    * easily displayed in the ROBOTC global variables debug window */
 +
    X_Accel = SensorValue[xAxis];
 +
    Y_Accel = SensorValue[yAxis];
 +
    Z_Accel = SensorValue[zAxis];
 +
   
 +
    wait1Msec(100);
 +
  }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|
 
|-
 
|-
 
|}
 
|}
 
<br />
 
<br />
  
== Sound Sensor ==
+
== Digital Sensors ==
 
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|The NXT Sound Sensor has a range of states between 0 and 100.  The lower the number, the quieter the reading is.  The higher the number, the louder the reading is.  Below is a simple program that maps the Sound Sensor reading to the motor speeds.
+
|There are 6 main types of Analog Sensors for the VEX CORTEX:
 
|-
 
|-
|<syntaxhighlight lang="ROBOTC">
+
|
#pragma config(Sensor, S2,     soundSensor,        sensorSoundDB)
+
 
 +
=== Touch ===
 +
[[File:vex_touch_sensor.png]]
 +
<br />
 +
Returns a digital value.  "1" means a closed circuit and "0" means an open circuit.
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, dgtl6, touchSensor,        sensorTouch)
 +
#pragma config(Motor,  port2,          rightMotor,    tmotorNormal, openLoop, reversed)
 +
#pragma config(Motor,  port3,          leftMotor,    tmotorNormal, openLoop)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
 
task main()
 
task main()
 
{
 
{
   wait1Msec(1000);       // A one-second wait is required to cleanly initialize the Sound Sensor.
+
   wait1Msec(2000); // Robot waits for 2000 milliseconds before executing program
   while(true)           // Infinite loop
+
 
 +
   while(SensorValue(touchSensor) == 0)  // Loop while robot's bumper/touch sensor isn't pressed in
 
   {
 
   {
     motor[motorB] = SensorValue[soundSensor];   /* Motors B and C are run at a power level equal */
+
     motor[rightMotor] = 63; // Motor on port2 is run at half (63) power forward
     motor[motorC] = SensorValue[soundSensor];   /* to the value read in by the Sound Sensor.    */
+
     motor[leftMotor] = 63; // Motor on port3 is run at half (63) power forward
 
   }
 
   }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|}
+
|
 +
 
 +
=== Sonar ===
 +
[[File:vex_sonar_sensor.png]]
 +
<br />
 +
Returns an analog value in centimeters (i.e. a value of 20 means 20 centimeters away), millimeters, inches, or raw data. A value of "-1" means the sensor does not receive a "reflection". The sonar sensor requires the "Input" wire to be attached the digital port directly following the main port of the Sonar Sensor (i.e. dgtl4 and dgtl5, ROBOTC will automatically fill the following port with the correct information).
 
<br />
 
<br />
  
== Ultrasonic (sonar) Sensor ==
+
{|
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, dgtl8,  sonarSensor,        sensorSONAR_cm)
 +
#pragma config(Motor,  port2,          rightMotor,    tmotorNormal, openLoop, reversed)
 +
#pragma config(Motor,  port3,          leftMotor,    tmotorNormal, openLoop)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
 
 +
task main()
 +
{
 +
  wait1Msec(2000);  // Robot waits for 2000 milliseconds before executing program
 +
 
 +
  while(SensorValue(sonarSensor) > 20  || SensorValue(sonarSensor) == -1)  // Loop while robot's Ultrasonic sensor is further than 20 cm away from an object
 +
  {                                                                       // || (or) it is '-1'.  (-1 is the value returned when nothing is in it's visable range)
 +
    motor[rightMotor] = 63; // Motor on port2 is run at half (63) power forward
 +
    motor[leftMotor]  = 63; // Motor on port3 is run at half (63) power forward
 +
  }
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|The NXT Ultrasonic Sensor has a range of states between 0 and 255. This number is representative of the current reading in centimeters. However, a reading of 255 means that the current sensor reading is out of range. This is a "range error" and means that the echo is not being read back (looking down a long hall for example). A reading outside of these numbers indicates that there is no sensor attached or some error with the connection.
+
|
 +
 
 +
=== Quadrature Encoder ===
 +
[[File:vex_quad_enc.png]]
 +
<br />
 +
A digital sensor that returns a counter value keeping track of how many "counts" the encoder has seen. This sensor will increment when traveling in the forward of direction and decrement when traveling in the reverse direction.The Quadrature Encoder requires one of the input wires be attached the interrupt port "int3" through "int6" to function properly.
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, dgtl1,  rightEncoder,        sensorQuadEncoder)
 +
#pragma config(Sensor, dgtl3, leftEncoder,        sensorQuadEncoder)
 +
#pragma config(Motor,  port2,          rightMotor,    tmotorNormal, openLoop, reversed)
 +
#pragma config(Motor,  port3,          leftMotor,    tmotorNormal, openLoop)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
 
 +
task main()
 +
{
 +
  wait1Msec(2000);  // 2 Second Delay
 +
 
 +
  //Clear Encoders
 +
  SensorValue[rightEncoder] = 0;
 +
  SensorValue[leftEncoder] = 0;
 +
 
 +
  while(SensorValue[leftEncoder] < 1800)  // While less than 5 rotations on the leftEncoder...
 +
  {
 +
    //...Move Forward
 +
    motor[rightMotor] = 63;
 +
    motor[leftMotor] = 63;
 +
  }
 +
 
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
|<syntaxhighlight lang="ROBOTC">
+
|
#pragma config(Sensor, S4,     sonarSensor,         sensorSONAR)
+
 
 +
=== Digital In ===
 +
This will make the sensor port act as just a digital input, very similar to a touch sensor.
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
// No sample code here yet!  Somebody supply some!
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|
 +
 
 +
=== Digital Out ===
 +
This will cause the sensor port to send out a digital high ("1") signal. Useful for using the controller to power 5V devices.
 +
<br />
 +
 
 +
{|
 +
  |-
 +
  |
 +
<syntaxhighlight lang="ROBOTC">
 +
#pragma config(Sensor, dgtl7, solenoid,           sensorDigitalOut)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
  
 
task main()
 
task main()
 
{
 
{
   int distance_in_cm = 20;  // Create variable 'distance_in_cm' and initialize it to 20(cm).
+
   while(true)                     // Loop Forever
   while(SensorValue[sonarSensor] > distance_in_cm)   /* While the Sonar Sensor readings are greater */
+
   {
  }                                                  /* than the specified, 'distance_in_cm':       */
+
    if(vexRT[Btn6U] == 1)         // If button 6U (upper right shoulder button) is pressed:
     motor[motorB] = 75;                     // Motor B is run at a 75 power level
+
     {
     motor[motorC] = 75;                     // Motor C is run at a 75 power level
+
      SensorValue[solenoid] = 1; // ...activate the solenoid.
 +
    }
 +
    else                          // If button 6U (upper right shoulder button) is NOT pressed:
 +
     {
 +
      SensorValue[solenoid] = 0; // ..deactivate the solenoid.
 +
    }
 
   }
 
   }
  motor[motorB] = 75;    // Motor B is stopped at a 0 power level
 
  motor[motorC] = 75;    // Motor C is stopped at a 0 power level
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
 
|}
 
|}
 
<br />
 
<br />

Latest revision as of 15:28, 25 July 2012

CORTEX → Sensors Overview


For ROBOTC CORTEX Sensor functions, check out the CORTEX Sensor Functions page!


Color Key
Function:
Variable:


Analog Sensors

There are 5 main types of Analog Sensors for the VEX CORTEX:

Light

Vex light sensor.png
The Light sensor, also known as the Reflection sensor, returns values ranging between 0 and 4095. 0 is the lightest reading and 4095 is the darkest.

#pragma config(Sensor, in1,    lightSensor,    sensorReflection)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);                // wait 2 seconds before exectuing following code
  bMotorReflected[port2] = true;  // reflects direction of motor on port 2
 
  while(true)                     // infinite loop:
  {
    clearLCDLine(0);                                // clear the top VEX LCD line
    clearLCDLine(1);                                // clear the bottom VEX LCD line
 
    setLCDPosition(0,0);                            // set the VEX LCD cursor the first line, first space
    displayNextLCDString("Light Sensor:");          // display "Light Sensor:" on the top line
 
    setLCDPosition(1,0);                            // set the VEX LCD cursor the second line, first space
    displayNextLCDNumber(SensorValue(lightSensor)); // display the reading of the lightSensor sensor
 
    wait1Msec(50);                                  // wait 50 milliseconds to help display properly
  }
}

Potentiometer

Vex pot.png
Returns an analog value between 0 and 4095 (although mechanical stops my limit the values to between 5 and 4092).

#pragma config(Sensor, in1,    potentiometer,  sensorPotentiometer)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);                // wait 2 seconds before exectuing following code
  bMotorReflected[port2] = true;  // reflects direction of motor on port 2
 
  while(true)                     // infinite loop:
  {
    clearLCDLine(0);                                  // clear the top VEX LCD line
    clearLCDLine(1);                                  // clear the bottom VEX LCD line
 
    setLCDPosition(0,0);                              // set the VEX LCD cursor the first line, first space
    displayNextLCDString("Potentiometer:");           // display "Potentiometer:" on the top line
 
    setLCDPosition(1,0);                              // set the VEX LCD cursor the second line, first space
    displayNextLCDNumber(SensorValue(potentiometer)); // display the reading of the potentiometer sensor
 
    wait1Msec(50);                                    // wait 50 milliseconds to help display properly
  }
}

Line Follower

Vex linefollower.png
Returns values ranging between 0 and 4095. 0 is the lightest reading and 4095 is the darkest.

#pragma config(Sensor, in1,    lineFollower,   sensorLineFollower)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);                // wait 2 seconds before exectuing following code
  bMotorReflected[port2] = true;  // reflects direction of motor on port 2
 
  while(true)                     // infinite loop:
  {
    if(SensorValue(lineFollower) < 950) // if the lineFollower sensor reads a value less than 950:
    {
      // turn left:
      motor[port2] = 50;  // motor on port 2 is run at power level 50
      motor[port3] = 0;   // motor on port 3 is stopped at power level 0
    }
    else  // lineFollower sensor reads a value greater than or equal to 950:
    {
      motor[port2] = 0;   // motor on port 2 is stopped at power level 0
      motor[port3] = 50;  // motor on port 3 is run at power level 50
    }
  }
}

Gyro

Vex gyro.png
As the Gyro is turned, the values it returns are in tenths of degrees, positive and negative. Thus, a SensorValue of 3600 equals 360 degrees, or one full rotation. When the sensor is mounted horizontally, counter-clockwise movements will return values from 0 to -3600; clockwise movements will return values from 0 to 3600. Once the gyro completes one full revolution, the sensor value will “roll-over” to 0 by default (for example: …3597, 3598, 3599, 3600, 0, 1, 2, 3,…). To change the “roll-over” point, un-comment line 33 (from the code below) and change the value of “SensorFullCount” from 3600 to the desired value (7200, 18000, ect)

For more information on the Gyro sensor, check out the ROBOTC blog post: Programming the VEX Gyro in ROBOTC.

#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop)
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
 
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++
Gyro Based Turns - Basic
-This program instructs your robot to turn for the specified number of degrees in "degrees10".
-For best sensor results, clear out the gyro and manually configure it at the begging of the code.
-The Gyro is configured by default to provide values from 0 to -3600 for clockwise rotation,
and 0 to 3600 for counter-clockwise rotation
 
Robot Model(s): Swervebot
 
[I/O Port] [Name] [Type] [Description]
Motor Port 2 rightMotor VEX Motor Right side motor
Motor Port 3 leftMotor VEX Motor Left side motor
Analog Port 8 in8 VEX Gyro Top-center mounted,
away from the Cortex
--------------------------------------------------------------------------------------------------*/
 
task main()
{
  //Completely clear out any previous sensor readings by setting the port to "sensorNone"
  SensorType[in8] = sensorNone;
  wait1Msec(1000);
  //Reconfigure Analog Port 8 as a Gyro sensor and allow time for ROBOTC to calibrate it
  SensorType[in8] = sensorGyro;
  wait1Msec(2000);
 
  //Adjust SensorScale to correct the scaling for your gyro
  //SensorScale[in8] = 260;
  //Adjust SensorFullCount to set the "rollover" point. 3600 sets the rollover point to +/-3600
  //SensorFullCount[in8] = 3600;
 
  //Specify the number of degrees for the robot to turn (1 degree = 10, or 900 = 90 degrees)
  int degrees10 = 900;
 
  //While the absolute value of the gyro is less than the desired rotation...
  while(abs(SensorValue[in8]) < degrees10)
  {
    //...continue turning
    motor[rightMotor] = 25;
    motor[leftMotor] = -25;
  }
 
  //Brief brake to stop some drift
  motor[rightMotor] = -5;
  motor[leftMotor] = 5;
  wait1Msec(250);
}

Accelerometer

Vex accel.png
Returns an analog value between 0 and 4095. Each Accelerometer Sensor has 3 cables, X, Y, and Z. The ports you attach them to don't have to be in any particular order (however you WILL need to use 3 ports).

#pragma config(Sensor, in1,    xAxis,          sensorAccelerometer)
#pragma config(Sensor, in2,    yAxis,          sensorAccelerometer)
#pragma config(Sensor, in3,    zAxis,          sensorAccelerometer)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  int nBiasValues[3];
 
  int X_Accel;
  int Y_Accel;
  int Z_Accel;
 
  wait1Msec(500); // bias values are being calculated
 
  /* store the bias values in an array so that they can be  
   * displayed in the ROBOTC global variables debug window */
  nBiasValues[0] = SensorBias[xAxis];
  nBiasValues[1] = SensorBias[yAxis];
  nBiasValues[2] = SensorBias[zAxis];
 
  while(true)
  {
    /* also store the actual sensor values so that they can be
     * easily displayed in the ROBOTC global variables debug window */
    X_Accel = SensorValue[xAxis];
    Y_Accel = SensorValue[yAxis];
    Z_Accel = SensorValue[zAxis];
 
    wait1Msec(100);
  }
}


Digital Sensors

There are 6 main types of Analog Sensors for the VEX CORTEX:

Touch

Vex touch sensor.png
Returns a digital value. "1" means a closed circuit and "0" means an open circuit.

#pragma config(Sensor, dgtl6,  touchSensor,         sensorTouch)
#pragma config(Motor,  port2,           rightMotor,    tmotorNormal, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);  // Robot waits for 2000 milliseconds before executing program
 
  while(SensorValue(touchSensor) == 0)  // Loop while robot's bumper/touch sensor isn't pressed in
  {
    motor[rightMotor] = 63;	 // Motor on port2 is run at half (63) power forward
    motor[leftMotor]  = 63;	 // Motor on port3 is run at half (63) power forward
  }
}

Sonar

Vex sonar sensor.png
Returns an analog value in centimeters (i.e. a value of 20 means 20 centimeters away), millimeters, inches, or raw data. A value of "-1" means the sensor does not receive a "reflection". The sonar sensor requires the "Input" wire to be attached the digital port directly following the main port of the Sonar Sensor (i.e. dgtl4 and dgtl5, ROBOTC will automatically fill the following port with the correct information).

#pragma config(Sensor, dgtl8,  sonarSensor,         sensorSONAR_cm)
#pragma config(Motor,  port2,           rightMotor,    tmotorNormal, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);  // Robot waits for 2000 milliseconds before executing program
 
  while(SensorValue(sonarSensor) > 20  || SensorValue(sonarSensor) == -1)  // Loop while robot's Ultrasonic sensor is further than 20 cm away from an object
  {                                                                        // || (or) it is '-1'.  (-1 is the value returned when nothing is in it's visable range)
    motor[rightMotor] = 63;  // Motor on port2 is run at half (63) power forward
    motor[leftMotor]  = 63;  // Motor on port3 is run at half (63) power forward
  }
}

Quadrature Encoder

Vex quad enc.png
A digital sensor that returns a counter value keeping track of how many "counts" the encoder has seen. This sensor will increment when traveling in the forward of direction and decrement when traveling in the reverse direction.The Quadrature Encoder requires one of the input wires be attached the interrupt port "int3" through "int6" to function properly.

#pragma config(Sensor, dgtl1,  rightEncoder,        sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,         sensorQuadEncoder)
#pragma config(Motor,  port2,           rightMotor,    tmotorNormal, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  wait1Msec(2000);  // 2 Second Delay
 
  //Clear Encoders
  SensorValue[rightEncoder] = 0;
  SensorValue[leftEncoder] = 0;
 
  while(SensorValue[leftEncoder] < 1800)  // While less than 5 rotations on the leftEncoder...
  {
    //...Move Forward
    motor[rightMotor] = 63;
    motor[leftMotor] = 63;
  }
 
}

Digital In

This will make the sensor port act as just a digital input, very similar to a touch sensor.

// No sample code here yet!  Somebody supply some!

Digital Out

This will cause the sensor port to send out a digital high ("1") signal. Useful for using the controller to power 5V devices.

#pragma config(Sensor, dgtl7,  solenoid,            sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  while(true)                     // Loop Forever
  {
    if(vexRT[Btn6U] == 1)         // If button 6U (upper right shoulder button) is pressed:
    {
      SensorValue[solenoid] = 1;  // ...activate the solenoid.
    }
    else                          // If button 6U (upper right shoulder button) is  NOT pressed:
    {
      SensorValue[solenoid] = 0;  // ..deactivate the solenoid.
    }
  }
}