UNI.h Reference
Every function of the library on one page. Click a function to open its description with parameters and an example.
The library is included with one line and provides two classes: UniBase drives the platform itself, UniDev the ports P1–P8 and the modules on them.
#include <UNI.h>
UniBase robot; // the platform: motors, movement, odometry, screen
UniDev module; // ports P1–P8: sensors, servos, light ring, traffic light
UniBase
The platform: motors, movement, odometry, screen. In the examples the object is called robot. Constants are on the UniBase class page.
Initialization
Motor Control
void motors(int powerLeft, int powerRight)Sets the power for both motors directly.void motorLeft(int power)Sets power for the left motor only.void motorRight(int power)Sets power for the right motor only.void motorsArc(int power, float angle)Moves the robot along an arc with a specific turn intensity.void motorsSync(int powerLeft, int powerRight)Like motors() but with encoder-based synchronisation — maintains the set speed ratio between left and right wheels.void motorsSpeed(float leftMMperSec, float rightMMperSec)Same as motorsSync(), but the wheel speeds are given in mm/s.void holdPosition()Actively holds the current wheel position using PID.void stop(int stopType = HARD)Stops both motors.void stopLeft(int stopType = HARD)Stops the left motor only.void stopRight(int stopType = HARD)Stops the right motor only.
Movement (Blocking)
bool moveDist(int power, int millimeters)Moves the robot specific distance using PID control on encoders.bool moveTime(int power, int milliseconds)Moves the motor for a specific duration of time.bool moveArcDist(int power, int angle, int millimeters)Moves along a curved path for a specific distance.bool moveArcTime(int power, int angle, int milliseconds)Moves along a curved path for a specific duration.bool rotate(int power, int angle)Rotates the robot in place by a specific angle.bool rotateTo(int power, float angleDeg)Rotates to an absolute odometry heading via the shortest path.bool moveTo(int power, float x, float y)Drives to a target odometry coordinate: turns to face the target then moves in a straight line.bool moveArcRadius(int power, float radiusMM, float angleDeg)Arc with precise geometry: specify the turning radius and arc angle.
Async Movement
void moveDistAsync(int power, int millimeters)Async version of moveDist().void moveTimeAsync(int power, int milliseconds)Async version of moveTime().void moveArcDistAsync(int power, int angle, int millimeters)Async version of moveArcDist().void moveArcTimeAsync(int power, int angle, int milliseconds)Async version of moveArcTime().void moveArcRadiusAsync(int power, float radiusMM, float angleDeg)Async version of moveArcRadius().void rotateAsync(int power, int angle)Async version of rotate().void rotateToAsync(int power, float angleDeg)Async version of rotateTo().void moveToAsync(int power, float x, float y)Async version of moveTo().bool isMoving()Returns true while an async movement task is active.bool waitMove(unsigned long timeoutMs = 0)Blocks until the current async movement command finishes.
Odometry
OdometryData getOdometry()Returns the current estimated position and orientation struct.float getAbsX()Returns the absolute X coordinate in millimeters.float getAbsY()Returns the absolute Y coordinate in millimeters.float getAbsAngle()Returns the absolute heading angle in degrees.void resetDistance()Resets the accumulated distance counter to 0.float getDistance()Returns the distance traveled since the last reset in millimeters.void resetAngle()Resets the accumulated angle counter to 0.float getAngle()Returns the angle turned since the last reset in degrees.void setPosition(float x, float y, float angleDeg)Sets the odometry pose.long getLeftTicks() / long getRightTicks()Returns the raw encoder tick count for the respective motor.void printOdometry()Prints the current X, Y, and Theta values to the Serial Monitor.
Speed
int speedToPower(float mmPerSec)Converts a speed in mm/s into a power value for movement commands.int turnRateToPower(float degPerSec)Converts a turn rate in degrees per second into a power value for rotate() and rotateTo().float getSpeed()Returns the measured speed of the robot centre in mm/s.float getTurnRate()Returns the measured turn rate in degrees per second.
Display
void displayPrint(ValueType value)Clears the OLED screen and prints the provided value in the center.void displayPrint(const char* name, ValueType value)Prints a name label at the top of the screen and a large value in the center.void displayText(const char* text)Displays up to 8 lines of small text, 21 characters each.void displayClear()Clears the OLED screen.
Utility
int getBatteryPower()Returns current battery level percentage (0-100).float getBatteryVoltage()Returns the battery voltage in volts.void blinkLED(int interval)Sets the background task to blink the onboard LED.void UniBaseControl()Starts listening for UART commands from an external controller (Arduino Nano).
UniDev
Ports P1–P8 and the modules on them: sensors, buttons, servos, the light ring, the traffic light. In the examples the object is called module. Port names are on the UniDev class page.
Initialization
Sensors
int ultraSonic(int trig, int echo)Reads distance from an ultrasonic sensor (HC-SR04).int lineSensor(int port)Reads an analog value from a line sensor.int digitalSensor(int port)Reads a generic digital value from a port.int analogSensor(int port)Reads a generic analog value from a port.int getPinMode(uint8_t pin)Returns the current configured mode of a GPIO pin.
Inputs
Servo
void servo(int port, int angle)Sets the angle of a servo motor connected to the specified port.void servoAttach(int port)Pre-attaches a servo to a port and reserves a PWM channel without setting an angle.void servoDetach(int port)Detaches a servo from its port — PWM pulses stop and the motor goes limp.
Light Ring (Basic)
void pixel(int index, int r, int g, int b)Sets the color of a single pixel.void pixelsAll(int r, int g, int b)Sets all LEDs in the ring to the specified RGB color.void pixelsClear()Turns off all LEDs.void pixelsShow()Sends the current buffer to the LED strip.void pixelsBrightness(int brightness)Sets the global brightness scaling.
Light Ring (Effects)
void pixelsRainbow(int speed, int duration)Displays a rotating rainbow animation.void pixelsRunning(int r, int g, int b, int duration)Running light effect: a single pixel moves around the ring once.void pixelsBreathing(int r, int g, int b, int duration)Smoothly fades the LEDs up and down (breathing effect).void pixelsFill(int r, int g, int b, int duration)Gradually fills the ring with color, then gradually clears it.void pixelsSparkle(int r, int g, int b, int duration, int count)Randomly flashes individual pixels.void pixelsRotating(int r, int g, int b, int duration, int segmentLength, int rotations)Rotates a segment of lit LEDs around the ring multiple times.void pixelsSpinner(int r, int g, int b, int duration)A spinner animation that lights up one pixel at a time until the ring is full, then restarts.