Difference between revisions of "ARDUINO MEGA Functions Sensors"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with "<yambe:breadcrumb>ARDUINO_MEGA_Functions_and_Variables|Functions and Variables</yambe:breadcrumb> <br /> {| |- | ''For more examples and explanations, head over to [[VEX2_Sen...")
 
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<yambe:breadcrumb>ARDUINO_MEGA_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
+
{{DISPLAYTITLE:2560 (MEGA) Sensor Functions}}
<br />
+
<yambe:breadcrumb self="2560 (MEGA) Sensor Functions">ARDUINO_MEGA_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
 
+
{|
+
|-
+
|
+
''For more examples and explanations, head over to [[VEX2_Sensors_Overview|CORTEX Sensors Overview]] page!''
+
 
+
''For information and examples on using I2C sensors, head over to [[VEX2_I2C_Sensors|CORTEX I2C Digital Sensors]] page!''
+
|-
+
|}
+
 
<br />
 
<br />
  
Line 16: Line 7:
  
 
== Information ==
 
== Information ==
{| class="wikiText" border="0"
+
The Arduino MEGA has 16 analog sensor ports and 53 digital sensor ports.
 +
 
 +
=== Analog ===
 +
{|style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:center; font-size:100%; border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000;" cellpadding="5%"
 +
! style="background-color:#E3E3E3; text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Sensor Type:
 +
! style="background-color:#E3E3E3; text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Comments:
 
|-
 
|-
|The VEX 2.0 Cortex is equipped with 20 sensor ports. There are 8 Analog Sensor Ports and 12 Digital Sensor Ports.  Note that unlike the VEX - PIC microcontroller, all Analog sensors must be in Analog ports, while all Digital sensors must be in Digital ports.
+
| style="text-align:left; border-style: solid; border-width: 1px 0px 1px 0px"|'''No Sensor'''
 
+
| style="text-align:left; border-style: solid; border-width: 1px 0px 1px 0px"|No Sensor Attached. Pin is disabled.
There are a variety of functions and variables used for configuring these ports and accessing their values.
+
|-
 
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Analog'''
Configuring sensors can be complicated. ROBOTC has a built-in wizard that can be used to configure the VEX 2.0 Cortex sensors. The wizard contains a number of PC windows that allow you to set the following fields for the sensor:
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Standard Analog Input (0-1023), no modification to raw value.
 
+
|-
*The variable name that you want to assign to the sensor. Using a name like “leftBumper” makes for a more readable program than 'dgtl3'!
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Potentiometer'''
*The port that the sensor is connected to.
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Standard Analog Input (0-1023), no modification to raw value.
*The type of sensor – touch, quadrature encoder, sonar, line follower, etc.
+
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Light Sensor'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Standard Analog Input (0-1023), no modification to raw value.
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Line Follower'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Standard Analog Input (0-1023), no modification to raw value.
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Gyro Sensor'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Support for VEX Gyro Sensor. See documentation here.
 
|-
 
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Accelerometer'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Support for VEX Accelerometer. See documentation here.
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Digital In'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Use an analog pin as a digital input (not currently supported)
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 0px 0px"|'''Digital Out'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 0px 0px"|Use an analog pin as a digital output (not currently supported)
 
|}
 
|}
 
<br />
 
<br />
  
== SensorBoolean ==
+
=== Digital ===
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
{|style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:center; font-size:100%; border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000;" cellpadding="5%"
 +
!style="background-color: #E3E3E3;text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Sensor Type:
 +
!style="background-color: #E3E3E3;text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Comments:
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">const word </span><span class="bigKeywordB">SensorBoolean</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
+
|style="text-align:left; border-style: solid; border-width: 1px 0px 1px 0px"|'''No Sensor'''
 +
|style="text-align:left; border-style: solid; border-width: 1px 0px 1px 0px"|No Sensor Attached. Pin is disabled.
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_word|word]])  This returns a true or false reading from the sensor. (Mostly used by Touch sensors).
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Touch'''
 
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Digital Input for touch sensors. (reversed from "Digital In" command)
 
+
|-
{| class="parameterTable" cellpadding="5%" width="100%"
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Quadrature Encoder (Two Pins)'''
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Support for VEX Quadrature Encoder. See documentation here.
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
+
|-
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''LED to Vcc'''
  |-
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Pin configured as digital output with reversed logic (for LEDs) (0 = LED on, 1 = LED off)
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|''sensor''  
+
|-
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|A sensor port or name
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Digital In'''
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_tSensors|tSensors]]
+
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Pin configured as digital input with internal "Pull-Up" resistor enabled.
  |-
+
  |}
+
 
+
 
+
  {|
+
  |-
+
  |<syntaxhighlight lang="robotc">
+
while(SensorBoolean[touchSensor]) // Loop while robot's bumper/touch sensor is pressed in
+
{
+
  motor[rightMotor] = 63;   // Right motor is run at half (63) power forward
+
  motor[leftMotor]  = 63;   // Left motor is run at half (63) power forward
+
}
+
</syntaxhighlight>
+
  |-
+
  |}
+
 
|-
 
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Digital Out'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Pin configured as digital output.
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Digital High Inpedance'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Pin configured as digital input without the "Pull-Up" resistor enabled.
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Sonar [Ping]'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Support for Parallax Ping Sonar Sensor (returns distance in cm, mm, inches, raw)
 +
|-
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 0px 0px"|'''Sonar [SRP-04] (Two Pins)'''
 +
|style="text-align:left; border-style: solid; border-width: 0px 0px 0px 0px"|Support for VEX Robotics Sonar Sensor (returns distance in cm, mm, inches, raw)
 
|}
 
|}
<br />
+
<br /><br />
  
== SensorRaw ==
+
== SensorType ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">word </span><span class="bigKeywordB">SensorRaw</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
+
| class="variableType"| <span class="bigKeywordB">TSensorTypes </span><span class="bigKeywordB">SensorType</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_word|word]])  This array value will return the "raw" (un-normalized) value of a sensor. Usually this is the raw A-D converted value, which is an analog value between 0 to 1023.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TSensorTypes|TSensorTypes]])  The SensorType array is used to manually specify what type of sensor is connected to a specific pin. Most users should not have to use this functionality and should use the Motors and Sensor Setup instead.
  
  
Line 89: Line 100:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="robotc">
 
   |<syntaxhighlight lang="robotc">
if(SensorRaw[lightSensor] > 512) // If the Raw Value of the Light Sensor is greater than 512:
+
SensorType[dgtl3] = sensorTouch;  // set digital pin #3 to be of type sensorTouch
{
+
  motor[rightMotor] = 63;   // Right motor is run at half (63) power forward
+
  motor[leftMotor] = 63;  // Left motor is run at half (63) power forward
+
}
+
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 101: Line 108:
 
<br />
 
<br />
  
== SensorType ==
+
== SensorValue ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordB">TSensorTypes </span><span class="bigKeywordB">SensorType</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
+
| class="variableType"| <span class="bigKeywordBI">word </span><span class="bigKeywordB">SensorValue</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TSensorTypes|TSensorTypes]])  The SensorType array is used to specify what type of sensor is connected to a certain port. Most users should not have to use this functionality and should use the Motors and Sensor Setup instead.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_word|word]])  This array value will return and modify the value of the sensor. When a pin is set as an "input" the SensorValue array command will return the value from the sensor attached to that pin. When a pin is set as an "output", the SensorValue array can be written to set the output value of the pin (typically Digital Outs will be set to 0 for Low and 1 for High).
  
  
Line 124: Line 131:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="robotc">
 
   |<syntaxhighlight lang="robotc">
SensorType[sonarSensor] = sensorSonar;  // set 'SonarSensor' to be of type sensorSonar (the Motors and
+
while(SensorValue[dgtl6] == 0) //Loop while digital pin #6 is low (set to zero)
                                        // sensors Setup screen does this for you in the background)
+
{
 +
  motor[motor_2] = 63;  // Motor pin #2 is run at half (63) power forward
 +
  motor[motor_3]  = 63;  // Motor pin #3 is run at half (63) power forward
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
 
   |}
 
   |}
|-
 
|}
 
<br />
 
  
== SensorValue ==
+
  {|
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
   ! Writing to digitalOut
|-
+
| class="functionType"| <span class="bigKeywordBI">word </span><span class="bigKeywordB">SensorValue</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tSensors sensor</span><span class="bigCodePunc">]</span>
+
|-
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_word|word]]) This array value returns the value of the sensor in a normalized fashion. Rather than returning a raw value of 0 to 1023, ROBOTC will interpret the data from the "SensorType" and return a more accurate representation of the sensor's data. An example of this is the Light Sensor, which will return a percentage value from 0 to 100.
+
 
+
 
+
{| class="parameterTable" cellpadding="5%" width="100%"
+
   ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
+
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
+
 
   |-
 
   |-
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|''sensor''  
+
   |<syntaxhighlight lang="robotc">
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|A sensor port or name
+
#pragma config(Sensor, dgtl24, dOut,          sensorDigitalOut)
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_tSensors|tSensors]]
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
task main()
 +
{
 +
   while(true)              // infinite loop to keep the program running
 +
   {
 +
    SensorValue[dOut] = 1; // set the value of 'dOut' to 1
 +
    wait1Msec(1000);        // wait 1 second
 +
    SensorValue[dOut] = 0;  // set the value of 'dOut' to 0
 +
    wait1Msec(1000);        // wait 1 second
 +
  }
 +
}
 +
</syntaxhighlight>
 
   |-
 
   |-
 
   |}
 
   |}
  
 
+
{|
  {|
+
  ! Reading from digitalIn
 
   |-
 
   |-
 
   |<syntaxhighlight lang="robotc">
 
   |<syntaxhighlight lang="robotc">
while(SensorValue(touchSensor) == 0) //Loop while robot's bumper/touch sensor isn't pressed in
+
#pragma config(Sensor, dgtl25, dIn,            sensorDigitalIn)
 +
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 +
task main()
 
{
 
{
  motor[rightMotor] = 63;   // Right motor is run at half (63) power forward
+
   int incomingSensor;                    // create int variable, 'incomingSensor'
  motor[leftMotor] = 63;   // Left motor is run at half (63) power forward
+
  while(true)                             // infinite loop to keep the program running
 +
  {
 +
    incomingSensor = SensorValue[dIn]; // set the value of 'incomingSensor' to the value of 'dIn'
 +
  }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 20:44, 11 May 2012

ARDUINOFunctions and Variables → 2560 (MEGA) Sensor Functions


Color Key
Function:
Variable:


Information

The Arduino MEGA has 16 analog sensor ports and 53 digital sensor ports.

Analog

Sensor Type: Comments:
No Sensor No Sensor Attached. Pin is disabled.
Analog Standard Analog Input (0-1023), no modification to raw value.
Potentiometer Standard Analog Input (0-1023), no modification to raw value.
Light Sensor Standard Analog Input (0-1023), no modification to raw value.
Line Follower Standard Analog Input (0-1023), no modification to raw value.
Gyro Sensor Support for VEX Gyro Sensor. See documentation here.
Accelerometer Support for VEX Accelerometer. See documentation here.
Digital In Use an analog pin as a digital input (not currently supported)
Digital Out Use an analog pin as a digital output (not currently supported)


Digital

Sensor Type: Comments:
No Sensor No Sensor Attached. Pin is disabled.
Touch Digital Input for touch sensors. (reversed from "Digital In" command)
Quadrature Encoder (Two Pins) Support for VEX Quadrature Encoder. See documentation here.
LED to Vcc Pin configured as digital output with reversed logic (for LEDs) (0 = LED on, 1 = LED off)
Digital In Pin configured as digital input with internal "Pull-Up" resistor enabled.
Digital Out Pin configured as digital output.
Digital High Inpedance Pin configured as digital input without the "Pull-Up" resistor enabled.
Sonar [Ping] Support for Parallax Ping Sonar Sensor (returns distance in cm, mm, inches, raw)
Sonar [SRP-04] (Two Pins) Support for VEX Robotics Sonar Sensor (returns distance in cm, mm, inches, raw)



SensorType

TSensorTypes SensorType[tSensors sensor]
(TSensorTypes) The SensorType array is used to manually specify what type of sensor is connected to a specific pin. Most users should not have to use this functionality and should use the Motors and Sensor Setup instead.


Parameter Explanation Data Type
sensor A sensor port or name tSensors


SensorType[dgtl3] = sensorTouch;  // set digital pin #3 to be of type sensorTouch


SensorValue

word SensorValue[tSensors sensor]
(word) This array value will return and modify the value of the sensor. When a pin is set as an "input" the SensorValue array command will return the value from the sensor attached to that pin. When a pin is set as an "output", the SensorValue array can be written to set the output value of the pin (typically Digital Outs will be set to 0 for Low and 1 for High).


Parameter Explanation Data Type
sensor A sensor port or name tSensors


while(SensorValue[dgtl6] == 0) //Loop while digital pin #6 is low (set to zero)
{
   motor[motor_2] = 63;   // Motor pin #2 is run at half (63) power forward
   motor[motor_3]  = 63;   // Motor pin #3 is run at half (63) power forward
}
Writing to digitalOut
#pragma config(Sensor, dgtl24, dOut,           sensorDigitalOut)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
task main()
{
  while(true)               // infinite loop to keep the program running
  {
    SensorValue[dOut] = 1;  // set the value of 'dOut' to 1
    wait1Msec(1000);        // wait 1 second
    SensorValue[dOut] = 0;  // set the value of 'dOut' to 0
    wait1Msec(1000);        // wait 1 second
  }
}
Reading from digitalIn
#pragma config(Sensor, dgtl25, dIn,            sensorDigitalIn)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
task main()
{
  int incomingSensor;                     // create int variable, 'incomingSensor'
  while(true)                             // infinite loop to keep the program running
  {
    incomingSensor = SensorValue[dIn];  // set the value of 'incomingSensor' to the value of 'dIn'
  }
}