Home / Botmag Article Companion Page

Botmag Article Companion Page

Here you will find the source code for the article in the Botmag titled “Pointers and data structures in ROBOTC”.  Click here to download the zip file with all four examples: [LINK].

Example 1:

// ptr tutorial example 1 
task main() 
{ 
  int i, j; 
  i = 10; 
  j = 51; 
  // This should print out 
  // i: 10, j: 51 
  writeDebugStreamLine("i: %d, j: %d", i, j);

  // Assign i's rvalue to j's 
  j = i; 
  // This should print out 
  // i: 10, j: 10 
  writeDebugStreamLine("i: %d, j: %d", i, j);

  i++; 
  // This should print out 
  // i: 11, j: 10 
  writeDebugStreamLine("i: %d, j: %d", i, j); 
}

Example 2:

// ptr tutorial example 2 
task main() 
{ 
  int i = 10; 
  int *iPtr; 
  iPtr = &i;  // iPtr now points at the address of i 
  // This should print out 
  // i: 10, iPtr: 656F6968, *iPtr: 10 
  // (value for iPtr may vary) 
  writeDebugStreamLine("i: %d, iPtr: %p, *iPtr: %d", i, iPtr, *iPtr);

  i++; 
  // This should print out 
  // i: 10, iPtr: 656F6968, *iPtr: 11 
  // (value for iPtr may vary) 
  writeDebugStreamLine("i: %d, iPtr: %p, *iPtr: %d", i, iPtr, *iPtr); 
}

Example 3:

// ptr tutorial example 3 
task main() 
{ 
  ubyte arr[3] = {1, 2, 3}; 
  ubyte *arrPtr;

  arrPtr = &arr[0];  // arrPtr now points to the address of arr[0]

  // This should print out 
  // arr[0]: 1, arrPtr: 656F6968, *arrPtr: 1 
  // (value for arrPtr may vary) 
  writeDebugStreamLine("arr[0]: %d, arrPtr: %p, *arrPtr: %d", arr[0], arrPtr, *arrPtr);

  arrPtr++;  // we're now pointing at arr[1]

  // This should print out 
  // arr[1]: 2, arrPtr: 656F6969, *arrPtr: 2 
  // (value for arrPtr may vary) 
  writeDebugStreamLine("arr[1]: %d, arrPtr: %p, *arrPtr: %d", arr[1], arrPtr, *arrPtr); 
}

Example 4:

#pragma config(Sensor, S1,     COLOUR,         sensorCOLORFULL) 
#pragma config(Sensor, S2,     TOUCH,          sensorTouch) 
#pragma config(Motor,  motorA,          MOTOR_A,       tmotorNXT, PIDControl, encoder) 
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Change this to however many data points you wish to log 
#define MAX_DATAPOINTS 50

struct 
{ 
  TColors colourNum; 
  bool touchSensorPressed; 
  long motorEncoder; 
  long timeStamp; 
} tDataEntry;

// Array of tDataEntry structs to hold our sensor and motor data 
tDataEntry dataEntries[MAX_DATAPOINTS];

void readData(tDataEntry *data) 
{ 
  data->colourNum = (TColors)SensorValue[COLOUR]; 
  data->touchSensorPressed = (bool)SensorBoolean[TOUCH]; 
  data->motorEncoder = nMotorEncoder[MOTOR_A]; 
  data->timeStamp = nPgmTime; 
}

void printData(int index, tDataEntry *data) 
{ 
  char *touchStatus;

  // Change the boolean value into touch sensor status 
  touchStatus = (data->touchSensorPressed) ? "pressed" : "released";

  // Display the values 
  eraseDisplay(); 
  nxtDisplayCenteredTextLine(0, "Data Entries"); 
  nxtDisplayTextLine(1, "-------------------"); 
  nxtDisplayTextLine(2, "Time: %d",   data->timeStamp); 
  nxtDisplayTextLine(3, "Colour: %d", data->colourNum); 
  nxtDisplayTextLine(4, "Touch: %s",  touchStatus); 
  nxtDisplayTextLine(5, "Motor: %d",  data->motorEncoder); 
  nxtDisplayTextLine(6, "-------------------"); 
  nxtDisplayTextLine(7, "<<     %2d      >>", index); 
}

task main() 
{ 
  int index = 0; 
  bool updateScreen = true;

  // Pointer to tDataEntry struct 
  tDataEntry *dataPtr;

  eraseDisplay();

  // Start the motor 
  motor[MOTOR_A] = 10;

  nxtDisplayCenteredTextLine(3, "Logging data"); 
  nxtDisplayCenteredTextLine(4, "Please wait..."); 
  for (index = 0; index < MAX_DATAPOINTS; index++) 
  { 
    // Point dataPtr to a fresh new data entry struct 
    dataPtr = &dataEntries[index];

    // Get the data from the sensors and motor 
    readData(dataPtr);

    // Wait a little bit 
    wait1Msec(100); 
  } 
  // Stop the motor again 
  motor[MOTOR_A] = 0;

  PlaySound(soundFastUpwardTones);

  // Reset the index 
  index = 0;

  while (true) 
  { 
    switch (nNxtButtonPressed) 
    { 
      case kLeftButton:  index--; 
                         updateScreen = true; 
                         break;

      case kRightButton: index++; 
                         updateScreen = true; 
                         break; 
    }

    // The user has pressed a button, so we need to fetch the 
    // new data entry and update the screen 
    if (updateScreen) 
    { 
      // Make sure we stay within the bounds of the array 
      index = (index > (MAX_DATAPOINTS - 1)) ? MAX_DATAPOINTS - 1 : index; 
      index = (index < 0) ? 0 : index;

      // Point dataPtr to the new data entry 
      dataPtr = &dataEntries[index];

      // Print out the new data entry 
      printData(index, dataPtr);

      // Make sure we don't update the screen again 
      updateScreen = false;

      // wait for button to not be pressed anymore 
      while(nNxtButtonPressed != kNoButton) 
        wait1Msec(10); 
    } 
    wait1Msec(50); 
  } 
}