UniDev Reference
Class for controlling sensors, addressable LEDs (Light Ring), and other peripherals.
Port Definitions
The badges on each port show its available capabilities: analog input (ADC), digital I/O (DIG), and servo/PWM (PWM). Below is the default usage.
The GPIO numbers below are for board V1.
Summary:
- Analog sensors (
analogSensor,lineSensor): only P1–P4 and P7. P5, P6, P8 have no ADC — reads return garbage. - Digital I/O (
digitalSensor, button): any port P1–P8. - Servo / PWM (
servo): P1–P7. P8 technically supports PWM but is occupied by the light ring — avoid it. - P7 is the most versatile port: the only analog input on ADC1 (no Wi-Fi conflict), with no boot-time quirks.
For hardware nuances of individual pins (strapping pins, ADC2/Wi-Fi conflict) see the Limitations page.
Initialization
Initializes the UniDev instance, creating the NeoPixel object and setting pin modes. It is
not necessary to call this function: it runs automatically on the first call of any UniDev
function and in robot.begin().
Example
UniDev module;
void setup() {
module.begin();
}
Sensors
Reads distance from an ultrasonic sensor (HC-SR04).
Parameters
trig: Trigger pin (P1-P8)echo: Echo pin (P1-P8)
Returns
Distance in millimeters.
Example
int dist = module.ultraSonic(P6, P7);
if (dist < 100) {
// Obstacle detected
}
Reads an analog value from a line sensor.
Parameters
port: Sensor port definition (P1-P8).
Returns
Analog value (0-4095).
Example
int val = module.lineSensor(P3);
Reads a generic digital value from a port.
Parameters
port: Port definition (P1-P8).
Returns
0 (LOW) or 1 (HIGH).
Example
if (module.digitalSensor(P5) == 1) {
// Sensor Active
}
Reads a generic analog value from a port.
Parameters
port: Port definition (P1-P8).
Returns
Analog value (0-4095).
Example
int lightLevel = module.analogSensor(P4);
Returns the current configured mode of a GPIO pin.
Returns
OUTPUT, INPUT, INPUT_PULLUP,
INPUT_PULLDOWN, or -1 on error.
Example
int mode = module.getPinMode(P2);
Inputs
Blocking function that halts program execution until the specified button is pressed and released.
Parameters
port: Port where button is connected (e.g.,P5).
Example
// Wait for button press
module.waitButton(P5);
Checks if a button is currently pressed.
Returns
true if pressed, false otherwise.
Example
if (module.getButtonState(P5)) {
Serial.println("Button Pressed!");
}
Servo
Sets the angle of a servo motor connected to the specified port.
Parameters
port: The port the servo is connected to (e.g.P1).angle: Target angle in degrees (0–180).
Example
module.servo(P3, 0); // Home position
module.servo(P3, 90); // Centre position
module.servo(P3, 180); // End position
Pre-attaches a servo to a port and reserves a PWM channel without setting an angle. Optional — servo() attaches automatically on first call. Useful when you want to claim the channel before the first movement.
Parameters
port: The port the servo is connected to (e.g.P3).
Example
module.servoAttach(P3); // Reserve PWM channel early
module.servo(P3, 90); // Then use it
Detaches a servo from its port — PWM pulses stop and the motor goes limp. Use this to protect the servo when the mechanism is forced against a stop or carrying a load. The next servo() call re-attaches it automatically.
Parameters
port: The port the servo is on (e.g.P3).
Example
module.servo(P3, 90); // Move to position
delay(1000);
module.servoDetach(P3); // Release — servo goes limp
Light Ring (Basic)
Sets the color of a single pixel.
Parameters
index: LED index (0-23).r, g, b: Color components (0-255).
Example
// Set 1st LED to Blue
module.pixel(0, 0, 0, 255);
Sets all LEDs in the ring to the specified RGB color. Implicitly calls show().
Example
module.pixelsAll(255, 0, 0); // All Red
Turns off all LEDs.
Example
module.pixelsClear();
Sends the current buffer to the LED strip.
Example
module.pixelsShow();
Sets the global brightness scaling.
Parameters
brightness: from 0 to 255; 100 is set at start-up.
Example
module.pixelsBrightness(50); // Dim the LEDs
Light Ring (Effects)
These are blocking animations.
Displays a rotating rainbow animation.
Parameters
speed: Speed of rotation (0-100). Higher values mean faster rotation.duration: Total duration of the animation in milliseconds.
Example
module.pixelsRainbow(50, 2000); // 2 seconds
Running light effect: a single pixel moves around the ring once.
Parameters
r, g, b: Color of the running pixel (0-255).duration: Time in milliseconds to complete one full circle.
Example
module.pixelsRunning(255, 0, 0, 1000); // Red pixel circles in 1s
Smoothly fades the LEDs up and down (breathing effect).
Parameters
r, g, b: Target color at maximum brightness (0-255).duration: Total duration of one full breath cycle (fade in + fade out) in milliseconds.
Example
module.pixelsBreathing(0, 0, 255, 3000); // Blue breath over 3s
Gradually fills the ring with color, then gradually clears it.
Parameters
r, g, b: Fill color (0-255).duration: Total duration for the fill AND clear sequence in milliseconds.
Example
module.pixelsFill(0, 255, 0, 1000); // Green fill+clear in 1s
Randomly flashes individual pixels.
Parameters
r, g, b: Color of the sparkles (0-255).duration: Total duration of the effect in milliseconds.count: Number of sparkles to generate within that duration.
Example
module.pixelsSparkle(255, 255, 255, 2000, 20); // 20 white flashes in 2s
Rotates a segment of lit LEDs around the ring multiple times.
Parameters
r, g, b: Color of the segment (0-255).duration: Total duration for ALL rotations in milliseconds.segmentLength: Number of LEDs in the rotating segment (e.g., 5).rotations: Number of full 360-degree turns to complete.
Example
// Rotate a segment of 5 red LEDs 3 times over 2 seconds
module.pixelsRotating(255, 0, 0, 2000, 5, 3);
A single lit pixel runs once around the ring — the same effect as pixelsRunning().
Parameters
r, g, b: Color of the spinner (0-255).duration: Time in milliseconds for one full rotation.
Example
module.pixelsSpinner(255, 255, 0, 1000);
Traffic Light
Sets the traffic light module state.
Parameters
color:TRAFFIC_RED,TRAFFIC_YELLOW,TRAFFIC_GREEN, orTRAFFIC_OFF.
Example
module.setTrafficLight(TRAFFIC_GREEN);
Runs the traffic light sequence: red, yellow, green — one second each — then switches every signal off. Blocks the program for three seconds.
Example
module.trafficLightSequence();