Need help with converting RobotC Code to Arduino IDE

#pragma config(Sensor, in2, lineTracker, sensorLineFollower)
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed)
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop)

int I= 0; //initial counter variable
float num=0; // Create float, "num" to store a decimal number
string str=""; // Create string, to store message to be displayed

task main()
{
clearTimer(timer1) ; // Clears timer 1
clearDebugStream(); //Clears debug stream window
I = 0; // Initialize "count" to 0 (Loop count variable)
while(1 == 1)
{
if(SensorValue(lineTracker) > 1275)// Sensor sees black line
{ I++; // number is added to count
waitUntil(SensorValue(lineTracker) < 1275);// Wait until it sees white line to continue (Only adds it once)
}
if(time1[timer1] == 15000)//After 15 seconds
{
num = I * 0.25 - 0.25; // Math to get real inches value of foot
stringFormat(str, "%0.2f", num); // Format num into a float to 2 decimal places, then send it to string, "numstr"
writeDebugStreamLine(str); // Print on stream
waitUntil(time1[timer1] == 15001); // Waits until after 15 seconds (Only prints once)
}
}
}

can you explain what your program does and what the various functions do?

  • clearTimer()
  • clearDebugStream()
  • SensorValue()
  • waitUntil()
  • stringFormat()
  • writeDebugStream()

Here's my best guess at a translation. It appear that the motors aren't used. Motor operation will depend on the motor drivers and what pins they are connected to.

const byte LineTrackerPin = A2;
//#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed)
//#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop)

int I = 0;      //initial counter variable
float num = 0;  // Create float, "num" to store a decimal number
char str[80];   // Create string, to store message to be displayed

unsigned long Timer1;

// This can't be right for an Arduino UNO analog input
// which only goes up to 1023.
const int BlackLineThreshold = 1275;

void setup()
{
  Timer1 = millis();     // Clears timer 1
  Serial.begin(115200);  //Clears debug stream window
  I = 0;                 // Initialize "count" to 0 (Loop count variable)
}

void loop()
{
  if (analogRead(LineTrackerPin) > BlackLineThreshold)  // Sensor sees black line
  {
    I++;                                                        // number is added to count
    while (analogRead(LineTrackerPin) > BlackLineThreshold) {}  // Wait until it sees white line to continue (Only adds it once)
  }

  if (millis() - Timer1 >= 15000)  //After 15 seconds
  {
    num = I * 0.25 - 0.25;  // Math to get real inches value of foot
    Serial.println(num);    // Print on stream
    while (1) {}            // Waits forever (Only prints once)
  }
}

Thanks for the help! Our code is meant to use a line sensor and count up the total amount of lines past that pass it and print them out on a debug stream, which worked on the initial VEX program but wasn't viable as we intended to use more than 8 analog sensors, so we swapped to Arduino!

Thanks for responding! The basic function of the code above is meant to use a line sensor and count up the total amount of lines that pass it and print the total number out on a debug stream, which worked on the initial VEX program but wasn't viable as we intended to use more than 8 analog sensors, so we swapped to Arduino!
Here's the sensor and line setup:

Functions:

  • clearTimer() - resets internal timer on Vex Cortex to 0 ms
  • clearDebugStream() - resets and clears debug stream
  • SensorValue() - Input analog values from _____ sensor, in this case it's the line/IR sensor
  • waitUntil() - Program stops and waits until ______ event, in this case it's the line sensor input dropping below 1275, which is the value where the white line is under the sensor
  • stringFormat() - Format of the string printed out in the debug stream, "0.0f"
  • writeDebugStream() - Writes out the string on the debug stream

Note:
The Vex code also converted the inputs to inches as well, but that's not required for the code and can be processed after the debug stream. A lot of these functions seem unique to Vex but the general function seems attainable with arduino and an ir sensor, but I'm not familiar with Arduino as I am with VEX.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.