Library Functions Reference
Functions for controlling the robot and its sensors. How to Install Libraries.
Results: 57
LittleRobot
26 functionsVersion 1.0.3 · Arduino Mega 2560 commands
Getting started
Install LittleRobot and Barigadam Motion Sensor, which supplies Barigadam_MotionSensor.h. Commands below use the object named robot. Movement calls block; MotorStart/Power only set outputs. Wheel degrees and chassis angle are different units.
#include <LittleRobot.h>
LittleRobot robot;
void setup() {
robot.begin();
robot.waitButtonPressRelease();
}
void loop() {}The declaration panel lists all supported overloads in argument order, including types, return type and optional defaults. A value after = may be omitted; other arguments are required. Recommendations are not a promise that every invalid input is rejected.
robot.begin()
Initializes the motor pins, button D9 (INPUT_PULLUP), LED D43, encoders and Wire. Call once in setup(). Encoder inversion changes readings, not motor direction. All three arguments are optional: true, true, 400000UL Hz.
Signatures and overloads
void begin(bool invertEncoder1 = true, bool invertEncoder2 = true, unsigned long wireClock = 400000UL);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
invertEncoder1 | bool | Invert encoder 1 readings; true by default. |
invertEncoder2 | bool | Invert encoder 2 readings; true by default. |
wireClock | unsigned long | I²C clock in Hz, default 400000UL; all devices must support it. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.begin(true, true, 400000UL);Motors and stopping
robot.MotorStart(motor, speed)
Sets a signed motor command immediately, without waiting or measuring speed. Motor 1 or 2; commands are clamped to ±100. One driver input stays at 255 and the other uses complementary PWM. At abs(speed)<0.5 both inputs are 255: electrical braking. The command remains until another motor command changes it.
Signatures and overloads
void MotorStart(int motor, float speed);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
motor | int | Motor 1 or 2. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.MotorStart(1, 60);
delay(500);
robot.MotorStart(1, 20);
delay(500);
robot.MotorStart(1, 0); robot.MotorPower(motor, speed)
Sets a signed command immediately, without encoder feedback or speed synchronization. Motor 1 or 2; commands are clamped to ±100. One driver input receives PWM, the other is zero. At abs(speed)<0.5 both inputs are zero: power is removed and the wheel may coast. Equal commands do not guarantee equal wheel speeds.
Signatures and overloads
void MotorPower(int motor, float speed);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
motor | int | Motor 1 or 2. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.MotorPower(1, 60);
delay(500);
robot.MotorPower(1, 0); robot.MotorsBrake()
Applies electrical braking to both motors immediately (255/255). Does not wait for the wheels to settle. Braking remains until another motor command.
Signatures and overloads
void MotorsBrake();Returns void (no result). See declarations for each overload and optional arguments.
robot.MotorStop(motor)
Corrects the selected wheel around its position at entry using its encoder, waits for it to settle, then leaves electrical braking applied. No background position controller runs after return.
Signatures and overloads
void MotorStop(int motor);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
motor | int | Motor 1 or 2. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.MotorStop(1);robot.MotorsHold()
Corrects both wheels around their positions at entry using encoders. Each wheel must stay strictly within degree_error (default 1.0°) for longer than degree_time (default 100 ms). The time is a confirmation interval, not total call duration. Leaves electrical braking after return, with no background controller and no Yaw reading. This is not a guarantee of chassis position accuracy.
Signatures and overloads
void MotorsHold();
void MotorsHold(float degree_error, unsigned long degree_time);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
degree_error | float | Strict wheel-position tolerance in degrees relative to entry, default 1.0. |
degree_time | unsigned long | Continuous settling confirmation in ms, default 100; not total call duration. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
MotorsHold_Settings.degree_error | 1 | Strict wheel-position tolerance in degrees relative to entry, default 1.0. |
MotorsHold_Settings.degree_time | 100 | Continuous settling confirmation in ms, default 100; not total call duration. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
robot.MotorsHold_Settings.degree_error = 1.5;
robot.MotorsHold_Settings.degree_time = 100;
robot.MotorsHold();robot.MotorsStop(speed, speed_2)
Applies MotorPower with the opposite of the two supplied commands for ms_1 (120 ms), brakes both channels for ms_2 (30 ms), then releases both with MotorPower(...,0). Tune the counter-command to inertia. No holding continues after return.
Signatures and overloads
void MotorsStop(float speed, float speed_2);
void MotorsStop(float speed, float speed_2, unsigned long ms_1, unsigned long ms_2);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
speed_2 | float | Signed command of motor 2 before counter-braking. |
ms_1 | unsigned long | Counter-command interval in ms, default 120. |
ms_2 | unsigned long | Electrical braking interval in ms, default 30. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
MotorsStop_Settings.ms_1 | 120 | Counter-command interval in ms, default 120. |
MotorsStop_Settings.ms_2 | 30 | Electrical braking interval in ms, default 30. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
robot.MotorsStop_Settings.ms_1 = 80;
robot.MotorsStop(30, 30);Movement and turns
robot.MotorsMoveSync(speed_0, speed, enc_1, enc_2, enc_3)
Moves with encoder synchronization through three wheel-distance segments: from speed_0 to speed over enc_1, at speed over enc_2, then back to speed_0 over enc_3. Total distance is their sum. Zero skips a segment. Progress is the mean of encoder angles multiplied by direction. The short form takes 4 or 5 arguments (enc_3=0). stop=1 finishes with MotorsHold. Use a positive initial command when starting a ramp from rest.
Signatures and overloads
void MotorsMoveSync(float speed_0, float speed, float enc_1, float enc_2, float enc_3 = 0);
void MotorsMoveSync(float speed_0, float speed, float enc_1, float enc_2, int stop, int direction);
void MotorsMoveSync(float speed_0, float speed, float enc_1, float enc_2, float enc_3, int stop, int direction);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed_0 | float | Initial/final profile command; choose a positive value for a ramp from rest. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
enc_1 | float | First wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle. |
enc_2 | float | Second wheel-distance segment in degrees, at working command. |
enc_3 | float | Third wheel-distance segment in degrees, returning to speed_0. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
direction | int | 1 forward, -1 reverse. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
MotorsMoveSync_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
MotorsMoveSync_Settings.direction | 1 | 1 forward, -1 reverse. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
robot.MotorsMoveSync_Settings.direction = 1;
robot.MotorsMoveSync(20, 60, 200, 400, 200);robot.Rotate(speed_0, speed, enc_1, enc_2, enc_3)
Turns with opposite wheel directions. Progress is the mean of absolute wheel angles, with the same three-segment profile. The short form takes 3–5 arguments: enc_2=0 and enc_3=0. stop=1 finishes with MotorsHold. yaw_mode=1 treats abs(enc_1) as an approximate chassis angle, multiplies it by 145/40 and splits the wheel path 25%/50%/25%; supplied enc_2/enc_3 are ignored. This mode does not read a gyroscope or target an absolute heading.
Signatures and overloads
void Rotate(float speed_0, float speed, float enc_1, float enc_2 = 0, float enc_3 = 0);
void Rotate(float speed_0, float speed, float enc_1, float enc_2, int stop, int clockwise, int to_line);
void Rotate(float speed_0, float speed, float enc_1, float enc_2, float enc_3, int stop, int clockwise, int to_line, int yaw_mode = 0);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed_0 | float | Initial/final profile command; choose a positive value for a ramp from rest. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
enc_1 | float | First wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle. |
enc_2 | float | Second wheel-distance segment in degrees, at working command. |
enc_3 | float | Third wheel-distance segment in degrees, returning to speed_0. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
clockwise | int | 1 clockwise, -1 opposite direction. |
to_line | int | 1 continues rotation through a line transition after the profile; 0 disables it. |
yaw_mode | int | 1 converts approximate chassis angle from enc_1; 0 uses wheel segments. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
Rotate_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
Rotate_Settings.clockwise | 1 | 1 clockwise, -1 opposite direction. |
Rotate_Settings.to_line | 0 | 1 continues rotation through a line transition after the profile; 0 disables it. |
Rotate_Settings.yaw_mode | 0 | 1 converts approximate chassis angle from enc_1; 0 uses wheel segments. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
After the profile, to_line=1 keeps the last motor command and waits on sensor 3 when clockwise=1, otherwise sensor 2, for readings >=200, then <=100, then >=120. Calibrate first; there is no timeout. These fixed thresholds are independent of PD_Cross threshold. The stop setting controls the subsequent stop.
robot.Rotate_Settings.clockwise = 1;
robot.Rotate(20, 60, 100, 200, 100);robot.Rotate_Settings.yaw_mode = 1;
robot.Rotate(20, 60, 90);robot.Turn_One(motor, speed_0, speed, enc_1, enc_2, enc_3)
Stops the other wheel with MotorStop before moving the selected motor, even when stop=0. Uses three profile segments and the absolute encoder angle of the moving wheel. The short form takes 5 or 6 arguments, with enc_3=0. stop=1 finishes with MotorStop on the selected wheel.
Signatures and overloads
void Turn_One(int motor, float speed_0, float speed, float enc_1, float enc_2, float enc_3 = 0);
void Turn_One(int motor, float speed_0, float speed, float enc_1, float enc_2, int stop, int direction);
void Turn_One(int motor, float speed_0, float speed, float enc_1, float enc_2, float enc_3, int stop, int direction);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
motor | int | Motor 1 or 2. |
speed_0 | float | Initial/final profile command; choose a positive value for a ramp from rest. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
enc_1 | float | First wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle. |
enc_2 | float | Second wheel-distance segment in degrees, at working command. |
enc_3 | float | Third wheel-distance segment in degrees, returning to speed_0. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
direction | int | 1 forward, -1 reverse. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
Turn_One_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
Turn_One_Settings.direction | 1 | 1 forward, -1 reverse. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
robot.Turn_One_Settings.direction = 1;
robot.Turn_One(1, 20, 50, 100, 200, 100);robot.Go_Gyro(speed_0, speed, enc_1, enc_2, enc_3)
Records Yaw at entry and follows that heading with PD correction (kp=5, kd=50) plus encoder synchronization. Five arguments define the three-segment wheel path; the explicit overload takes ten. Speeds use absolute values clamped to 100, with initial speed capped by working speed. Negative segments become zero. A zero initial command, zero total path or direction other than ±1 brakes and returns. Final wheel commands are clamped to ±100. stop=1 finishes with MotorsHold.
Signatures and overloads
void Go_Gyro(float speed_0, float speed, float enc_1, float enc_2, float enc_3);
void Go_Gyro(float speed_0, float speed, float enc_1, float enc_2, float enc_3, float kp, float kd, int stop, int direction, uint8_t addr);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed_0 | float | Initial/final profile command; choose a positive value for a ramp from rest. |
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
enc_1 | float | First wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle. |
enc_2 | float | Second wheel-distance segment in degrees, at working command. |
enc_3 | float | Third wheel-distance segment in degrees, returning to speed_0. |
kp | float | Main proportional gain. |
kd | float | Main derivative gain per loop, not per second. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
direction | int | 1 forward, -1 reverse. |
addr | uint8_t | Motion sensor address used by this command, default 0x60. Independent of an external sensor object. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
Go_Gyro_Settings.kp | 5 | Main proportional gain. |
Go_Gyro_Settings.kd | 50 | Main derivative gain per loop, not per second. |
Go_Gyro_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
Go_Gyro_Settings.direction | 1 | 1 forward, -1 reverse. |
Go_Gyro_Settings.addr | 0x60 | Motion sensor address used by this command, default 0x60. Independent of an external sensor object. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
Check readYPR before starting, but that does not detect a disconnection inside the blocking movement. getYaw returns 0 on I²C failure; neither command has a timeout or normalizes the Yaw error across range boundaries. Confirm the sensor range and motor/Yaw feedback sign on your module. Changing the target from +90 to -90 changes the requested direction, not feedback polarity. The constructor and Settings addresses do not change the physical module address.
robot.Go_Gyro_Settings.direction = 1;
robot.Go_Gyro(20, 60, 200, 400, 200);robot.Turn_Gyro(speed, degrees)
Turns by degrees relative to Yaw at entry. The short form takes two arguments; explicit forms take five or six. Internal PD gains are 0.1/0.1 with encoder synchronization. Outside the 33° finishing zone the base command is bounded by restriction*speed and speed; inside it the algorithm chooses its own positive bounds. Near zero the minimum is 7 or 9 for more_power=0/1, and both use MotorStart. speed=0 is not a motor-off command. It requires abs(error)<error_ok for more than ten consecutive iterations, then always calls MotorsHold by encoders. There is no stop field. Use positive error_ok; this tolerance is not a guarantee of final chassis accuracy.
Signatures and overloads
void Turn_Gyro(float speed, float degrees);
void Turn_Gyro(float speed, float degrees, uint8_t addr, float restriction, float error_ok, int more_power = 0);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
degrees | float | For PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float). |
addr | uint8_t | Motion sensor address used by this command, default 0x60. Independent of an external sensor object. |
restriction | float | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
error_ok | float | Strict positive Yaw error tolerance in degrees, default 0.9. |
more_power | int | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
Turn_Gyro_Settings.addr | 0x60 | Motion sensor address used by this command, default 0x60. Independent of an external sensor object. |
Turn_Gyro_Settings.restriction | 0.9 | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
Turn_Gyro_Settings.error_ok | 0.9 | Strict positive Yaw error tolerance in degrees, default 0.9. |
Turn_Gyro_Settings.more_power | 0 | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
Check readYPR before starting, but that does not detect a disconnection inside the blocking movement. getYaw returns 0 on I²C failure; neither command has a timeout or normalizes the Yaw error across range boundaries. Confirm the sensor range and motor/Yaw feedback sign on your module. Changing the target from +90 to -90 changes the requested direction, not feedback polarity. The constructor and Settings addresses do not change the physical module address.
robot.Turn_Gyro_Settings.error_ok = 1.0;
robot.Turn_Gyro(60, 90);Line following and crossings
robot.PD_Enc(speed, degrees, kp, kd)
Follows the line using a selected sensor pair. First aligns with independent gains and speed, then uses speed/kp/kd from the call. degrees is int and measures the mean of absolute wheel angles from entry, including alignment. Alignment is not shortened when degrees is smaller: degrees=0 does not disable it. stop=1 calls MotorsStop(speed,speed), then releases the motors. Subsequent delay() does not hold position.
Signatures and overloads
void PD_Enc(float speed, int degrees, float kp, float kd);
void PD_Enc(float speed, int degrees, float kp, float kd, int stop, float restriction, int sensors, float s_align, float degrees_align, float kp_align, float kd_align, float restrict_align, int more_power = 1);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
degrees | int | For PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float). |
kp | float | Main proportional gain. |
kd | float | Main derivative gain per loop, not per second. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
restriction | float | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
sensors | int | Line-following pair: 23 (default), 12 or 34. |
s_align | float | Alignment command, default 25. |
degrees_align | float | Full alignment distance in wheel degrees, default 150; 0 disables alignment. |
kp_align | float | Alignment proportional gain, default 0.3. |
kd_align | float | Alignment derivative gain, default 1. |
restrict_align | float | Alignment lower-command fraction, default -0.2 (allows reversal). |
more_power | int | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
PD_Enc_Settings.more_power | 1 | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
PD_Enc_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
PD_Enc_Settings.restriction | 0.1 | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
PD_Enc_Settings.sensors | 23 | Line-following pair: 23 (default), 12 or 34. |
PD_Enc_Settings.s_align | 25 | Alignment command, default 25. |
PD_Enc_Settings.degrees_align | 150 | Full alignment distance in wheel degrees, default 150; 0 disables alignment. |
PD_Enc_Settings.kp_align | 0.3 | Alignment proportional gain, default 0.3. |
PD_Enc_Settings.kd_align | 1 | Alignment derivative gain, default 1. |
PD_Enc_Settings.restrict_align | -0.2 | Alignment lower-command fraction, default -0.2 (allows reversal). |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
Alignment uses s_align=25, kp_align=0.3, kd_align=1 and restrict_align=-0.2. kd=0 in the short call only disables D in the main phase. For tuning, set degrees_align=0 or tune the alignment gains separately. Command limits are [s_align*restrict_align,s_align] then [speed*restriction,speed]; a negative floor allows reversal. Use positive working commands. MotorsStop has independent default intervals 120/30 ms.
robot.PD_Enc_Settings.more_power = 0;
robot.PD_Enc_Settings.degrees_align = 0;
robot.PD_Enc(20, 300, 0.3, 2);robot.PD_Cross(speed, degrees, kp, kd)
Follows the line until at least degrees have been travelled and the selected crossing sensors are below threshold. Alignment counts towards distance; crossing detection starts after alignment. A detection is used by the next loop condition, after another motor command and 2 ms delay. degrees is a minimum, with no maximum distance or timeout. An invalid go_to brakes and returns without driving. stop=1 calls MotorsStop(speed,speed), then releases the motors.
Signatures and overloads
void PD_Cross(float speed, int degrees, float kp, float kd);
void PD_Cross(float speed, int degrees, float kp, float kd, int stop, float restriction, int sensors, int go_to, float s_align, float degrees_align, float kp_align, float kd_align, float restrict_align, int threshold = 150, int more_power = 1);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
speed | float | Working motor command on the library scale, not RPM. For profiles/PD/gyro exercises choose a positive value up to 100; MotorStart/Power use a signed value. |
degrees | int | For PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float). |
kp | float | Main proportional gain. |
kd | float | Main derivative gain per loop, not per second. |
stop | int | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
restriction | float | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
sensors | int | Line-following pair: 23 (default), 12 or 34. |
go_to | int | Distinct digits 1–4 selecting crossing sensors; default 1234. |
s_align | float | Alignment command, default 25. |
degrees_align | float | Full alignment distance in wheel degrees, default 150; 0 disables alignment. |
kp_align | float | Alignment proportional gain, default 0.3. |
kd_align | float | Alignment derivative gain, default 1. |
restrict_align | float | Alignment lower-command fraction, default -0.2 (allows reversal). |
threshold | int | Strict upper threshold for crossing detection, default 150. |
more_power | int | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
Returns void (no result). See declarations for each overload and optional arguments.
Scroll sideways to see all columns.
| Setting | Default | Meaning |
|---|---|---|
PD_Cross_Settings.more_power | 1 | PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart. |
PD_Cross_Settings.stop | 1 | 1 enables the finishing stop described for this command; 0 leaves the last motor command. |
PD_Cross_Settings.restriction | 0.1 | PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9. |
PD_Cross_Settings.sensors | 23 | Line-following pair: 23 (default), 12 or 34. |
PD_Cross_Settings.go_to | 1234 | Distinct digits 1–4 selecting crossing sensors; default 1234. |
PD_Cross_Settings.threshold | 150 | Strict upper threshold for crossing detection, default 150. |
PD_Cross_Settings.s_align | 25 | Alignment command, default 25. |
PD_Cross_Settings.degrees_align | 150 | Full alignment distance in wheel degrees, default 150; 0 disables alignment. |
PD_Cross_Settings.kp_align | 0.3 | Alignment proportional gain, default 0.3. |
PD_Cross_Settings.kd_align | 1 | Alignment derivative gain, default 1. |
PD_Cross_Settings.restrict_align | -0.2 | Alignment lower-command fraction, default -0.2 (allows reversal). |
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
Alignment uses s_align=25, kp_align=0.3, kd_align=1 and restrict_align=-0.2. kd=0 in the short call only disables D in the main phase. For tuning, set degrees_align=0 or tune the alignment gains separately. Command limits are [s_align*restrict_align,s_align] then [speed*restriction,speed]; a negative floor allows reversal. Use positive working commands. MotorsStop has independent default intervals 120/30 ms.
robot.PD_Cross_Settings.go_to = 14;
robot.PD_Cross_Settings.threshold = 200;
robot.PD_Cross_Settings.degrees_align = 0;
robot.PD_Cross(20, 100, 0.3, 2);robot.Intersection_Check(go_to, threshold)
Checks once and returns int 1 only when every selected sensor is strictly below threshold (default 150); otherwise 0. go_to is any nonempty combination of distinct digits 1–4: 1, 14, 23, 4312. Order is irrelevant; unselected sensors are ignored. Zero, negatives, repeated digits and other digits return 0. Does not move or wait.
Signatures and overloads
int Intersection_Check(int go_to, int threshold = 150);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
go_to | int | Distinct digits 1–4 selecting crossing sensors; default 1234. |
threshold | int | Strict upper threshold for crossing detection, default 150. |
Returns int as described above. See declarations for each overload and optional arguments.
if (robot.Intersection_Check(14, 200) == 1) {
robot.MotorsBrake();
}Line sensors and calibration
robot.readLightRaw(sensor)
Returns an int raw ADC reading (0–1023) for logical sensor 1–4. Invalid sensor numbers return 0; that does not prove a black surface.
Signatures and overloads
int readLightRaw(int sensor);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
sensor | int | Logical line sensor 1=A8, 2=A9, 3=A6, 4=A7. |
Returns int as described above. See declarations for each overload and optional arguments.
int raw = robot.readLightRaw(1);robot.readLight(sensor)
Returns float on the calibrated 0–255 scale, but conversion uses integer map, without fractional resolution. Equal black/white endpoints return the raw reading clamped to 0–255, not a valid calibration. Invalid sensor numbers return 0.
Signatures and overloads
float readLight(int sensor);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
sensor | int | Logical line sensor 1=A8, 2=A9, 3=A6, 4=A7. |
Returns float as described above. See declarations for each overload and optional arguments.
float light = robot.readLight(2);robot.setLightCalibration(black1, black2, black3, black4, white1, white2, white3, white4)
Sets eight int endpoints: black1..black4, then white1..white4. Both groups use logical order 1=A8, 2=A9, 3=A6, 4=A7. Black and white must differ for each sensor. Use your measured values in setup() after begin().
Signatures and overloads
void setLightCalibration(int black1, int black2, int black3, int black4, int white1, int white2, int white3, int white4);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
black1 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
black2 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
black3 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
black4 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
white1 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
white2 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
white3 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
white4 | int | Measured raw endpoint for this logical sensor (A8/A9/A6/A7). |
Returns void (no result). See declarations for each overload and optional arguments.
robot.setLightCalibration(
410, 360, 440, 385,
959, 939, 959, 950
);Open File → Examples → LittleRobot → LightCalibration. At 9600 baud record all four black readings and all four white readings, then use them in setLightCalibration in your program.
robot.resetLightCalibration()
Restores black=400 and white=950 for all four line sensors. Takes no arguments.
Signatures and overloads
void resetLightCalibration();Returns void (no result). See declarations for each overload and optional arguments.
Encoder readings
robot.getDegrees(m)
Returns float wheel degrees since the last encoder reset, with sign determined by direction and encoder inversion. Motor number is uint8_t, 1 or 2. Does not move the wheel.
Signatures and overloads
float getDegrees(uint8_t m);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
m | uint8_t | Encoder 1 or 2. |
Returns float as described above. See declarations for each overload and optional arguments.
float degrees = robot.getDegrees(1);robot.getCounts(m)
Returns long encoder counts since the last reset for motor 1 or 2. Counts are converted to degrees using CPR; 360° is one wheel revolution with the correct CPR.
Signatures and overloads
long getCounts(uint8_t m);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
m | uint8_t | Encoder 1 or 2. |
Returns long as described above. See declarations for each overload and optional arguments.
long counts = robot.getCounts(1);robot.resetEncoder(m)
Resets the selected encoder (uint8_t motor 1 or 2). Does not stop its motor.
Signatures and overloads
void resetEncoder(uint8_t m);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
m | uint8_t | Encoder 1 or 2. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.resetAllEncoders()
Resets both encoder counters without stopping the motors. Takes no arguments.
Signatures and overloads
void resetAllEncoders();Returns void (no result). See declarations for each overload and optional arguments.
robot.setCPR(cpr)
Sets long counts per wheel revolution for both encoders; default 1415. A nonpositive value restores 1415. Calibration and CPR are independent of AllSettings_Save/resetAllSettings.
Signatures and overloads
void setCPR(long cpr);Scroll sideways to see all columns.
| Argument | Type | Meaning |
|---|---|---|
cpr | long | Counts per revolution, default 1415. |
Returns void (no result). See declarations for each overload and optional arguments.
robot.setCPR(1415);Saving and restoring settings
Settings_Save saves the whole group in object RAM until restart, not EEPROM. A short call uses the current group and restores its saved copy after execution. reset restores that saved copy, not necessarily factory defaults. Explicit overloads do not read/reset their own group. Nested MotorsHold/MotorsStop independently use and restore their stopping group. AllSettings covers nine groups, excluding light calibration and CPR.
void resetMotorsStop_Settings();
void resetMotorsHold_Settings();
void resetMotorsMoveSync_Settings();
void resetRotate_Settings();
void resetTurn_One_Settings();
void resetPD_Enc_Settings();
void resetPD_Cross_Settings();
void resetTurn_Gyro_Settings();
void resetGo_Gyro_Settings();
void resetAllSettings();
void MotorsStop_Settings_Save();
void MotorsHold_Settings_Save();
void MotorsMoveSync_Settings_Save();
void Rotate_Settings_Save();
void Turn_One_Settings_Save();
void PD_Enc_Settings_Save();
void PD_Cross_Settings_Save();
void Turn_Gyro_Settings_Save();
void Go_Gyro_Settings_Save();
void AllSettings_Save();robot.MotorsMoveSync_Settings.direction = -1;
robot.MotorsMoveSync(20, 40, 60, 200); // Restores saved direction=1.
robot.MotorsMoveSync_Settings.direction = -1;
robot.MotorsMoveSync_Settings_Save(); // Save the whole group in RAM.
robot.MotorsMoveSync(20, 40, 60, 200); // Restores saved direction=-1.
robot.resetMotorsMoveSync_Settings(); // Restores -1, not factory 1.Barigadam_MotionSensor
19 functionsOrientation, calibration, and motion sensor settings
Getting started
The library reads the motion sensor's rotation and tilt. Here, sensor is the sensor object at address 0x60. Call Wire.begin() once in setup() to start communication; when using LittleRobot, robot.begin() starts it.
#include <Wire.h>
#include <Barigadam_MotionSensor.h>
BarigadamMotionSensor sensor(0x60);
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
float yaw, pitch, roll;
if (sensor.readYPR(yaw, pitch, roll)) {
Serial.print(yaw);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
} else {
Serial.println("I2C error");
}
delay(100);
}BarigadamMotionSensor sensor(address)create a sensor object
Creates an object that communicates with the sensor at the specified address. The device's address is unchanged. Declare the object before setup().
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
address | The connected sensor's address. | From 0x40 to 0x60, inclusive. Default: 0x60. |
BarigadamMotionSensor sensor(0x60);Angles in degrees
sensor.readYPR(yaw, pitch, roll)read three angles
Reads three angles in one request and writes them to the variables. Check the result to distinguish zero angles from a communication error.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
yaw | Output variable: rotation around the vertical axis. | A float variable; angle in degrees. |
pitch | Output variable: forward/backward tilt. | A float variable; angle in degrees. |
roll | Output variable: side-to-side tilt. | A float variable; angle in degrees. |
| Result | Meaning |
|---|---|
true | The readings were written to the variables. |
false | Reading failed; all output variables are zero. |
float yaw, pitch, roll;
if (sensor.readYPR(yaw, pitch, roll)) {
Serial.println(yaw);
}sensor.getYaw()rotation around the vertical axis
Returns rotation around the vertical axis in degrees. A communication error also returns 0; use readYPR() when you need to check whether reading succeeded.
float yaw = sensor.getYaw();
Serial.println(yaw);sensor.getPitch()forward/backward tilt
Returns forward/backward tilt in degrees. A communication error also returns 0; use readYPR() when you need to check whether reading succeeded.
float pitch = sensor.getPitch();
Serial.println(pitch);sensor.getRoll()side-to-side tilt
Returns side-to-side tilt in degrees. A communication error also returns 0; use readYPR() when you need to check whether reading succeeded.
float roll = sensor.getRoll();
Serial.println(roll);Zero reset and calibration
sensor.zeroReset()reset angles to zero
Sends a command to use the current orientation as zero. The result confirms transmission, not completion of the reset.
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.zeroReset()) {
Serial.println("I2C error");
}sensor.calibrate100()quick calibration
Starts quick gyroscope calibration. Keep the sensor still until it finishes. The call does not wait for calibration; true only confirms transmission.
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.calibrate100()) {
Serial.println("I2C error");
}sensor.calibrate500()full calibration
Starts full gyroscope calibration. Keep the sensor still until it finishes. The call does not wait for calibration; true only confirms transmission.
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.calibrate500()) {
Serial.println("I2C error");
}Raw angles and registers
sensor.readYPRRaw(yaw, pitch, roll)three angles in tenths of a degree
Reads three angles in one request without converting them to fractional degrees. For example, 125 means 12.5°. Returns false and clears the variables on failure.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
yaw | Output variable: rotation around the vertical axis. | An int16_t variable; each unit is 0.1°. |
pitch | Output variable: forward/backward tilt. | An int16_t variable; each unit is 0.1°. |
roll | Output variable: side-to-side tilt. | An int16_t variable; each unit is 0.1°. |
| Result | Meaning |
|---|---|
true | The readings were written to the variables. |
false | Reading failed; all output variables are zero. |
int16_t yaw, pitch, roll;
if (sensor.readYPRRaw(yaw, pitch, roll)) {
Serial.println(yaw);
}sensor.getYawRaw()rotation around the vertical axis in tenths of a degree
Returns a signed integer: 10 represents 1°. An error also returns 0; use readYPRRaw() to check whether reading succeeded.
int16_t yaw = sensor.getYawRaw();
Serial.println(yaw);sensor.getPitchRaw()forward/backward tilt in tenths of a degree
Returns a signed integer: 10 represents 1°. An error also returns 0; use readYPRRaw() to check whether reading succeeded.
int16_t pitch = sensor.getPitchRaw();
Serial.println(pitch);sensor.getRollRaw()side-to-side tilt in tenths of a degree
Returns a signed integer: 10 represents 1°. An error also returns 0; use readYPRRaw() to check whether reading succeeded.
int16_t roll = sensor.getRollRaw();
Serial.println(roll);sensor.readRegister(reg)read a register
Reads one byte from the specified sensor register. Returns a number from 0 to 255; an error also returns 0.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
reg | Register number. | 0x00–0xFF; the sensor firmware defines each register's meaning. |
To read a complete angle, use getYaw(), getPitch(), getRoll(), or readYPR().
Address, mode, and correction
sensor.getAddress()object address
Returns the address this object uses to communicate with the sensor. Does not check the connection.
Serial.println(sensor.getAddress(), HEX);sensor.changeAddress(newAddress)change the sensor address
Sends a new address to the sensor. After successful transmission, the object uses that address without checking for a response there. Connect only the sensor being configured and choose an unused address.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
newAddress | The sensor's I²C address. | 0x40–0x60; must differ from the current address and be unused. |
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (sensor.changeAddress(0x41)) {
Serial.println(sensor.getAddress(), HEX);
}sensor.setMode(mode)display and button mode
Sets how the sensor's display and button work. I²C angle readings remain available in all three modes. Returns the command transmission result.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
mode | Operating mode. | 0, 1, 2. |
| Mode | Behavior |
|---|---|
0 | Display off. |
1 | Display shows angles; button enabled. |
2 | Display shows angles; button disabled. |
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.setMode(1)) {
Serial.println("I2C error");
}sensor.setCourseCorrection(correction)course correction
Sends a coefficient that adjusts the measured rotation. Call saveCourseCorrection() to store it in the sensor's memory.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
correction | Correction coefficient. | From 0.5 to 1.5, inclusive; dimensionless. |
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.setCourseCorrection(1.02f)) {
Serial.println("I2C error");
}sensor.saveCourseCorrection()save course correction
Sends a command to store the correction coefficient in the sensor's memory. Returns true if transmission succeeds; it does not read the stored value back.
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (sensor.setCourseCorrection(1.02f)) {
if (!sensor.saveCourseCorrection()) {
Serial.println("I2C error");
}
}Diagnostics
BarigadamMotionSensor::scanI2C()find I²C devices
Prints responding device addresses to the Serial Monitor. Checks addresses from 0x01 to 0x7E, then pauses for 5 seconds before repeating. The call never returns; use a separate diagnostic sketch.
#include <Wire.h>
#include <Barigadam_MotionSensor.h>
void setup() {
Serial.begin(9600);
Wire.begin();
BarigadamMotionSensor::scanI2C();
}
void loop() {
}Barigadam_ColorSensor
10 functionsColor recognition, RGBC, HSV, and sensor settings
Getting started
The library reads recognized colors and the sensor's light channels. Here, sensor is an object at address 0x40. Call Wire.begin() once in setup(); with LittleRobot, robot.begin() starts communication.
#include <Wire.h>
#include <Barigadam_ColorSensor.h>
BarigadamColorSensor sensor(0x40);
bool sensorReady = false;
void setup() {
Serial.begin(9600);
Wire.begin();
delay(20);
sensorReady = sensor.setMode(1);
if (!sensorReady) {
Serial.println("I2C error");
}
}
void loop() {
if (!sensorReady) return;
int color = sensor.readColor();
if (color == -2) {
Serial.println("I2C error");
} else {
Serial.println(color);
}
delay(200);
}Create a separate object for each sensor. Devices on the same bus must have different addresses. Assign addresses with one sensor connected at a time.
BarigadamColorSensor leftSensor(0x40);
BarigadamColorSensor rightSensor(0x41);BarigadamColorSensor sensor(address)create a sensor object
Creates an object that communicates with the sensor at the specified address. The device's address is unchanged. Declare the object before setup().
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
address | The connected sensor's address. | From 0x40 to 0x60, inclusive. Default: 0x40. |
BarigadamColorSensor sensor(0x40);Color and readings
sensor.readColor()read the recognized color
Returns the color number recognized by the sensor. Select mode 1 for standard recognition. Store the result in an int to retain negative codes.
| Result | Meaning |
|---|---|
1 | Black. |
2 | Blue. |
3 | Green. |
4 | Yellow. |
5 | Red. |
6 | White. |
-1 | Color not recognized. |
-2 | I²C communication error. |
int color = sensor.readColor();
if (color == -2) {
Serial.println("I2C error");
} else if (color == -1) {
Serial.println("Unknown color");
} else {
Serial.println(color);
}sensor.readRGBC(r, g, b, c)read RGBC channels
Reads the red, green, blue, and clear light channels in one request. Select mode 4 to inspect raw readings. Results are written to uint16_t variables.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
r, g, b | Output variables for red, green, and blue channels. | uint16_t variables: 0–65535, raw counts. |
c | Output variable for the overall light level. | A uint16_t variable: 0–65535, raw counts. |
| Result | Meaning |
|---|---|
true | The readings were written to the variables. |
false | Reading failed; all output variables are zero. |
uint16_t r, g, b, c;
if (sensor.readRGBC(r, g, b, c)) {
Serial.println(r);
Serial.println(g);
Serial.println(b);
Serial.println(c);
}Use sensor.readRGBC(channel) to get one channel. It returns 0 for an error or an invalid channel number.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
channel | Channel to read. | 1 — R; 2 — G; 3 — B; 4 — C. |
uint16_t red = sensor.readRGBC(1);
Serial.println(red);sensor.readHSV(h, s, v)hue, saturation, and brightness
Calculates three HSV values from one RGBC sample and writes them to float variables. If the clear channel is zero, all three values are zero and the read succeeds.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
h | Output variable for hue. | float: from 0° inclusive to 360° exclusive. |
s | Output variable for saturation. | float: 0 to 1, dimensionless. |
v | Output variable for brightness. | float: 0 to 1, dimensionless. |
| Result | Meaning |
|---|---|
true | The readings were written to the variables. |
false | Reading failed; all output variables are zero. |
float h, s, v;
if (sensor.readHSV(h, s, v)) {
Serial.println(h);
Serial.println(s);
Serial.println(v);
}Use sensor.readHSV(channel) for one value. An error or an invalid channel number returns 0.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
channel | Value to read. | 1 — H; 2 — S; 3 — V. |
float hue = sensor.readHSV(1);
Serial.println(hue);Registers
sensor.readRegister(reg, value)read a register and check communication
Reads one byte from a register into value. Returns true on success. On failure, returns false and sets value to zero.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
reg | Register number. | 0x00–0xFF; 0x08 is the color code, 0x09 is the current mode. |
value | Output variable for the byte read. | A uint8_t variable: 0 to 255. |
uint8_t mode;
if (sensor.readRegister(0x09, mode)) {
Serial.println(mode);
} else {
Serial.println("I2C error");
}The call sensor.readRegister(reg) returns the byte directly. In this form, 0 can mean either a valid reading or an error.
uint8_t mode = sensor.readRegister(0x09);
Serial.println(mode);Mode and address
sensor.setMode(mode)select the sensor mode
Sends the mode command and returns to the program. true confirms I²C transmission. Read register 0x09 to check the current mode; persistence after power-off depends on the firmware.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
mode | Sensor operating mode. | An integer from 0 to 5. |
| Mode | Purpose |
|---|---|
0 | Operation without indication. |
1 | Basic color recognition. |
2 | Recognition of colored objects only. |
3 | True color mode. |
4 | Raw RGBC readings. |
5 | Ambient readings without illumination. |
| Result | Meaning |
|---|---|
true | The I²C command was acknowledged. |
false | The command failed: check the arguments, address, and connection. |
if (!sensor.setMode(4)) {
Serial.println("I2C error");
}sensor.getAddress()object address
Returns the address this object uses to communicate with the sensor. Does not check the connection.
Serial.println(sensor.getAddress(), HEX);sensor.changeAddress(newAddress)change the sensor address
Checks that the chosen address is unused and sends the change command. Then checks for a response at the new address, allowing up to 100 ms of delays plus I²C communication time. Updates the object's address when confirmed. Connect only the sensor being configured.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
newAddress | The sensor's I²C address. | 0x40–0x60; must differ from the current address and be unused. |
| Result | Meaning |
|---|---|
true | The sensor responded at the new address; the object now uses it. |
false | The change was not confirmed; the object's address is unchanged. Check the device's actual address with the scanner. |
if (sensor.changeAddress(0x41)) {
Serial.println(sensor.getAddress(), HEX);
} else {
Serial.println("Address change failed");
}sensor.sendToSensor(dataToSend)configure with one command
Selects a mode or changes the address according to the value provided. Returns the result of setMode() or changeAddress() and continues the program.
Scroll sideways to see all columns.
| Argument | Purpose | Accepted values |
|---|---|---|
dataToSend | Mode or address. | 0–5 selects a mode; 0x40–0x60 changes the address. Other values return false. |
if (!sensor.sendToSensor(1)) {
Serial.println("I2C error");
}Diagnostics
BarigadamColorSensor::scanI2C()find I²C devices
Prints responding device addresses to the Serial Monitor. Checks addresses from 0x01 to 0x7E, then pauses for 5 seconds before repeating. The call never returns; use a separate diagnostic sketch.
BarigadamColorSensor::I2Cscan() performs the same scan.
#include <Wire.h>
#include <Barigadam_ColorSensor.h>
void setup() {
Serial.begin(9600);
Wire.begin();
BarigadamColorSensor::scanI2C();
}
void loop() {
}