Home / Tutorials / Tutorial: Using ROBOTC’s new sensorCustom

Tutorial: Using ROBOTC’s new sensorCustom

With ROBOTC 3.59 came two new sensor types, the sensorCustom and the complimentary sensorCustom9V.  This sensor type was introduced to allow the fast toggling of the dig0 and dig1 lines on the NXT’s sensor ports.  Normally, these pins are either used for RS485, I2C or controlling the light on a LEGO Light Sensor.  Up until now, you could not control these pins directly, apart from, perhaps, switching between sensorLightActive and Inactive, but then you’d only really toggle dig0.  Not anymore!

By configuring a port as sensorCustom, you can control both the direction and value of the dig0 and dig1 lines:

// To set dig0, use mask 0x01 and 0 as input, 1 as output 
// To set dig1, use mask 0x02 and 0 as input, 1 as output 
DigitalPinDirection[CUSTOM] = 0x03; // that's both pins configured as output
DigitalPinDirection[CUSTOM] = 0x00; // that's both pins configured as input
DigitalPinDirection[CUSTOM] = 0x01; // that's pin 0 configured as output
DigitalPinDirection[CUSTOM] = 0x02; // that's pin 1 configured as output

To set the value of a pin, do the following:

// To set dig0, use mask 0x01, 1 is high, 0 is low 
// To set dig1, use mask 0x02, 1 is high, 0 is low 
DigitalPinValue[CUSTOM] = 0x01; // set dig0 as 1, dig1 as 0 
DigitalPinValue[CUSTOM] = 0x03; // set dig0 as 1, dig1 as 1

You can read the values of the pins, using the same mechanism:

pin1 = DigitalPinValue[CUSTOM]  & 0x01; // read pin 0 
pin2 = DigitalPinValue[CUSTOM]  & 0x02; // read pin 1

Here’s a simple example of how you could make a simple blinking light using a LEGO Light Sensor:

#pragma config(Sensor, S1,     CUSTOM,         sensorCustom) 
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

// Use sensorCustom to enable dig0/dig1 control. 
// Use sensorCustom9V to enable the 9V line as well.

task main() 
{ 
  // To set dig0, use mask 0x01 and 0 as input, 1 as output 
  // To set dig1, use mask 0x02 and 0 as input, 1 as output 
  DigitalPinDirection[CUSTOM] = 0x03; // that's both pins configured as output 
  while (true) 
  { 
    // To set dig0, use mask 0x01, 1 is high, 0 is low 
    // To set dig1, use mask 0x02, 1 is high, 0 is low 
    DigitalPinValue[CUSTOM] = 0x01; // set dig0 as 1, dig1 as 0 
    wait1Msec(50); 
    DigitalPinValue[CUSTOM] = 0x00; // set dig0 as 0, dig1 as 0 
    wait1Msec(100); 
  } 
}

You could make it blink much faster than that, if you’d like.  I use it to control the Mindsensor SensorMUX (which was the reason this stuff was added to ROBOTC in the first place).  The driver for that sensor will be part of the next Driver Suite.

About Xander

Xander Soldaat is a Software Engineer and former Infrastructure Architect. He loves building and programming robots. He recently had the opportunity to turn his robotics hobby into his profession and has started working for Robomatter, the makers of ROBOTC and Robot Virtual Words.