Library Functions Reference

Functions for controlling the robot and its sensors. How to Install Libraries.

Results: 57

LittleRobot

26 functions

Version 1.0.3 · Arduino Mega 2560 commands

#include <LittleRobot.h>

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()

How it works

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.

Arguments
ArgumentTypeMeaning
invertEncoder1boolInvert encoder 1 readings; true by default.
invertEncoder2boolInvert encoder 2 readings; true by default.
wireClockunsigned longI²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);

Button and LED

robot.waitButtonPressRelease()

How it works

Blocks until the button has been pressed and released, with debouncing. Takes no arguments and returns void.

Signatures and overloads
void waitButtonPressRelease();

Returns void (no result). See declarations for each overload and optional arguments.

robot.LED(state)

How it works

Sets the onboard LED: state=1 turns it on; any other value turns it off.

Signatures and overloads
void LED(int state);

Scroll sideways to see all columns.

Arguments
ArgumentTypeMeaning
stateint1 enables the LED; other values disable it.

Returns void (no result). See declarations for each overload and optional arguments.

robot.LED(1);
robot.waitButtonPressRelease();
robot.LED(0);

Motors and stopping

robot.MotorStart(motor, speed)

How it works

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.

Arguments
ArgumentTypeMeaning
motorintMotor 1 or 2.
speedfloatWorking 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)

How it works

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.

Arguments
ArgumentTypeMeaning
motorintMotor 1 or 2.
speedfloatWorking 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()

How it works

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)

How it works

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.

Arguments
ArgumentTypeMeaning
motorintMotor 1 or 2.

Returns void (no result). See declarations for each overload and optional arguments.

robot.MotorStop(1);

robot.MotorsHold()

How it works

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.

Arguments
ArgumentTypeMeaning
degree_errorfloatStrict wheel-position tolerance in degrees relative to entry, default 1.0.
degree_timeunsigned longContinuous 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.

SettingDefaultMeaning
MotorsHold_Settings.degree_error1Strict wheel-position tolerance in degrees relative to entry, default 1.0.
MotorsHold_Settings.degree_time100Continuous 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)

How it works

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.

Arguments
ArgumentTypeMeaning
speedfloatWorking 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_2floatSigned command of motor 2 before counter-braking.
ms_1unsigned longCounter-command interval in ms, default 120.
ms_2unsigned longElectrical braking interval in ms, default 30.

Returns void (no result). See declarations for each overload and optional arguments.

Scroll sideways to see all columns.

SettingDefaultMeaning
MotorsStop_Settings.ms_1120Counter-command interval in ms, default 120.
MotorsStop_Settings.ms_230Electrical 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)

How it works

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.

Arguments
ArgumentTypeMeaning
speed_0floatInitial/final profile command; choose a positive value for a ramp from rest.
speedfloatWorking 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_1floatFirst wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle.
enc_2floatSecond wheel-distance segment in degrees, at working command.
enc_3floatThird wheel-distance segment in degrees, returning to speed_0.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
directionint1 forward, -1 reverse.

Returns void (no result). See declarations for each overload and optional arguments.

Scroll sideways to see all columns.

SettingDefaultMeaning
MotorsMoveSync_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
MotorsMoveSync_Settings.direction11 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);
Video example

robot.Rotate(speed_0, speed, enc_1, enc_2, enc_3)

How it works

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.

Arguments
ArgumentTypeMeaning
speed_0floatInitial/final profile command; choose a positive value for a ramp from rest.
speedfloatWorking 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_1floatFirst wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle.
enc_2floatSecond wheel-distance segment in degrees, at working command.
enc_3floatThird wheel-distance segment in degrees, returning to speed_0.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
clockwiseint1 clockwise, -1 opposite direction.
to_lineint1 continues rotation through a line transition after the profile; 0 disables it.
yaw_modeint1 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.

SettingDefaultMeaning
Rotate_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
Rotate_Settings.clockwise11 clockwise, -1 opposite direction.
Rotate_Settings.to_line01 continues rotation through a line transition after the profile; 0 disables it.
Rotate_Settings.yaw_mode01 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);
Video example

robot.Turn_One(motor, speed_0, speed, enc_1, enc_2, enc_3)

How it works

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.

Arguments
ArgumentTypeMeaning
motorintMotor 1 or 2.
speed_0floatInitial/final profile command; choose a positive value for a ramp from rest.
speedfloatWorking 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_1floatFirst wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle.
enc_2floatSecond wheel-distance segment in degrees, at working command.
enc_3floatThird wheel-distance segment in degrees, returning to speed_0.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
directionint1 forward, -1 reverse.

Returns void (no result). See declarations for each overload and optional arguments.

Scroll sideways to see all columns.

SettingDefaultMeaning
Turn_One_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
Turn_One_Settings.direction11 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);
Video example

robot.Go_Gyro(speed_0, speed, enc_1, enc_2, enc_3)

How it works

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.

Arguments
ArgumentTypeMeaning
speed_0floatInitial/final profile command; choose a positive value for a ramp from rest.
speedfloatWorking 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_1floatFirst wheel-distance segment in degrees; in Rotate yaw_mode=1, approximate chassis angle.
enc_2floatSecond wheel-distance segment in degrees, at working command.
enc_3floatThird wheel-distance segment in degrees, returning to speed_0.
kpfloatMain proportional gain.
kdfloatMain derivative gain per loop, not per second.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
directionint1 forward, -1 reverse.
addruint8_tMotion 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.

SettingDefaultMeaning
Go_Gyro_Settings.kp5Main proportional gain.
Go_Gyro_Settings.kd50Main derivative gain per loop, not per second.
Go_Gyro_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
Go_Gyro_Settings.direction11 forward, -1 reverse.
Go_Gyro_Settings.addr0x60Motion 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);
Video example

robot.Turn_Gyro(speed, degrees)

How it works

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.

Arguments
ArgumentTypeMeaning
speedfloatWorking 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.
degreesfloatFor PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float).
addruint8_tMotion sensor address used by this command, default 0x60. Independent of an external sensor object.
restrictionfloatPD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
error_okfloatStrict positive Yaw error tolerance in degrees, default 0.9.
more_powerintPD: 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.

SettingDefaultMeaning
Turn_Gyro_Settings.addr0x60Motion sensor address used by this command, default 0x60. Independent of an external sensor object.
Turn_Gyro_Settings.restriction0.9PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
Turn_Gyro_Settings.error_ok0.9Strict positive Yaw error tolerance in degrees, default 0.9.
Turn_Gyro_Settings.more_power0PD: 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);
Video example

Line following and crossings

robot.PD_Enc(speed, degrees, kp, kd)

How it works

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.

Arguments
ArgumentTypeMeaning
speedfloatWorking 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.
degreesintFor PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float).
kpfloatMain proportional gain.
kdfloatMain derivative gain per loop, not per second.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
restrictionfloatPD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
sensorsintLine-following pair: 23 (default), 12 or 34.
s_alignfloatAlignment command, default 25.
degrees_alignfloatFull alignment distance in wheel degrees, default 150; 0 disables alignment.
kp_alignfloatAlignment proportional gain, default 0.3.
kd_alignfloatAlignment derivative gain, default 1.
restrict_alignfloatAlignment lower-command fraction, default -0.2 (allows reversal).
more_powerintPD: 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.

SettingDefaultMeaning
PD_Enc_Settings.more_power1PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart.
PD_Enc_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
PD_Enc_Settings.restriction0.1PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
PD_Enc_Settings.sensors23Line-following pair: 23 (default), 12 or 34.
PD_Enc_Settings.s_align25Alignment command, default 25.
PD_Enc_Settings.degrees_align150Full alignment distance in wheel degrees, default 150; 0 disables alignment.
PD_Enc_Settings.kp_align0.3Alignment proportional gain, default 0.3.
PD_Enc_Settings.kd_align1Alignment derivative gain, default 1.
PD_Enc_Settings.restrict_align-0.2Alignment 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)

How it works

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.

Arguments
ArgumentTypeMeaning
speedfloatWorking 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.
degreesintFor PD: minimum/target wheel path as described above (int). For Turn_Gyro: relative chassis angle (float).
kpfloatMain proportional gain.
kdfloatMain derivative gain per loop, not per second.
stopint1 enables the finishing stop described for this command; 0 leaves the last motor command.
restrictionfloatPD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
sensorsintLine-following pair: 23 (default), 12 or 34.
go_tointDistinct digits 1–4 selecting crossing sensors; default 1234.
s_alignfloatAlignment command, default 25.
degrees_alignfloatFull alignment distance in wheel degrees, default 150; 0 disables alignment.
kp_alignfloatAlignment proportional gain, default 0.3.
kd_alignfloatAlignment derivative gain, default 1.
restrict_alignfloatAlignment lower-command fraction, default -0.2 (allows reversal).
thresholdintStrict upper threshold for crossing detection, default 150.
more_powerintPD: 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.

SettingDefaultMeaning
PD_Cross_Settings.more_power1PD: 1 selects MotorPower, 0 MotorStart in both phases. Turn_Gyro: 0/1 selects finishing effort; both use MotorStart.
PD_Cross_Settings.stop11 enables the finishing stop described for this command; 0 leaves the last motor command.
PD_Cross_Settings.restriction0.1PD: lower wheel command / working command, default 0.1. Turn_Gyro: base-command floor outside finishing zone, default 0.9.
PD_Cross_Settings.sensors23Line-following pair: 23 (default), 12 or 34.
PD_Cross_Settings.go_to1234Distinct digits 1–4 selecting crossing sensors; default 1234.
PD_Cross_Settings.threshold150Strict upper threshold for crossing detection, default 150.
PD_Cross_Settings.s_align25Alignment command, default 25.
PD_Cross_Settings.degrees_align150Full alignment distance in wheel degrees, default 150; 0 disables alignment.
PD_Cross_Settings.kp_align0.3Alignment proportional gain, default 0.3.
PD_Cross_Settings.kd_align1Alignment derivative gain, default 1.
PD_Cross_Settings.restrict_align-0.2Alignment 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)

How it works

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.

Arguments
ArgumentTypeMeaning
go_tointDistinct digits 1–4 selecting crossing sensors; default 1234.
thresholdintStrict 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)

How it works

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.

Arguments
ArgumentTypeMeaning
sensorintLogical 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)

How it works

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.

Arguments
ArgumentTypeMeaning
sensorintLogical 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)

How it works

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.

Arguments
ArgumentTypeMeaning
black1intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
black2intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
black3intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
black4intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
white1intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
white2intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
white3intMeasured raw endpoint for this logical sensor (A8/A9/A6/A7).
white4intMeasured 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()

How it works

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)

How it works

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.

Arguments
ArgumentTypeMeaning
muint8_tEncoder 1 or 2.

Returns float as described above. See declarations for each overload and optional arguments.

float degrees = robot.getDegrees(1);

robot.getCounts(m)

How it works

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.

Arguments
ArgumentTypeMeaning
muint8_tEncoder 1 or 2.

Returns long as described above. See declarations for each overload and optional arguments.

long counts = robot.getCounts(1);

robot.resetEncoder(m)

How it works

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.

Arguments
ArgumentTypeMeaning
muint8_tEncoder 1 or 2.

Returns void (no result). See declarations for each overload and optional arguments.

robot.resetAllEncoders()

How it works

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)

How it works

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.

Arguments
ArgumentTypeMeaning
cprlongCounts 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 functions

Orientation, calibration, and motion sensor settings

#include <Barigadam_MotionSensor.h>

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

How it works

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.

Arguments
ArgumentPurposeAccepted values
addressThe 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
yawOutput variable: rotation around the vertical axis.A float variable; angle in degrees.
pitchOutput variable: forward/backward tilt.A float variable; angle in degrees.
rollOutput variable: side-to-side tilt.A float variable; angle in degrees.
ResultMeaning
trueThe readings were written to the variables.
falseReading 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

How it works

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

How it works

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

How it works

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

How it works

Sends a command to use the current orientation as zero. The result confirms transmission, not completion of the reset.

ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (!sensor.zeroReset()) {
  Serial.println("I2C error");
}

sensor.calibrate100()quick calibration

How it works

Starts quick gyroscope calibration. Keep the sensor still until it finishes. The call does not wait for calibration; true only confirms transmission.

ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (!sensor.calibrate100()) {
  Serial.println("I2C error");
}

sensor.calibrate500()full calibration

How it works

Starts full gyroscope calibration. Keep the sensor still until it finishes. The call does not wait for calibration; true only confirms transmission.

ResultMeaning
trueThe I²C command was acknowledged.
falseThe 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
yawOutput variable: rotation around the vertical axis.An int16_t variable; each unit is 0.1°.
pitchOutput variable: forward/backward tilt.An int16_t variable; each unit is 0.1°.
rollOutput variable: side-to-side tilt.An int16_t variable; each unit is 0.1°.
ResultMeaning
trueThe readings were written to the variables.
falseReading 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

How it works

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

How it works

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

How it works

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

How it works

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.

Arguments
ArgumentPurposeAccepted values
regRegister 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

How it works

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

How it works

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.

Arguments
ArgumentPurposeAccepted values
newAddressThe sensor's I²C address.0x40–0x60; must differ from the current address and be unused.
ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (sensor.changeAddress(0x41)) {
  Serial.println(sensor.getAddress(), HEX);
}

sensor.setMode(mode)display and button mode

How it works

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.

Arguments
ArgumentPurposeAccepted values
modeOperating mode.0, 1, 2.
ModeBehavior
0Display off.
1Display shows angles; button enabled.
2Display shows angles; button disabled.
ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (!sensor.setMode(1)) {
  Serial.println("I2C error");
}

sensor.setCourseCorrection(correction)course correction

How it works

Sends a coefficient that adjusts the measured rotation. Call saveCourseCorrection() to store it in the sensor's memory.

Scroll sideways to see all columns.

Arguments
ArgumentPurposeAccepted values
correctionCorrection coefficient.From 0.5 to 1.5, inclusive; dimensionless.
ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (!sensor.setCourseCorrection(1.02f)) {
  Serial.println("I2C error");
}

sensor.saveCourseCorrection()save course correction

How it works

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.

ResultMeaning
trueThe I²C command was acknowledged.
falseThe 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

How it works

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 functions

Color recognition, RGBC, HSV, and sensor settings

#include <Barigadam_ColorSensor.h>

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

How it works

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.

Arguments
ArgumentPurposeAccepted values
addressThe connected sensor's address.From 0x40 to 0x60, inclusive. Default: 0x40.
BarigadamColorSensor sensor(0x40);

Color and readings

sensor.readColor()read the recognized color

How it works

Returns the color number recognized by the sensor. Select mode 1 for standard recognition. Store the result in an int to retain negative codes.

ResultMeaning
1Black.
2Blue.
3Green.
4Yellow.
5Red.
6White.
-1Color not recognized.
-2I²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

How it works

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.

Arguments
ArgumentPurposeAccepted values
r, g, bOutput variables for red, green, and blue channels.uint16_t variables: 0–65535, raw counts.
cOutput variable for the overall light level.A uint16_t variable: 0–65535, raw counts.
ResultMeaning
trueThe readings were written to the variables.
falseReading 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.

Arguments
ArgumentPurposeAccepted values
channelChannel 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
hOutput variable for hue.float: from 0° inclusive to 360° exclusive.
sOutput variable for saturation.float: 0 to 1, dimensionless.
vOutput variable for brightness.float: 0 to 1, dimensionless.
ResultMeaning
trueThe readings were written to the variables.
falseReading 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.

Arguments
ArgumentPurposeAccepted values
channelValue 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
regRegister number.0x00–0xFF; 0x08 is the color code, 0x09 is the current mode.
valueOutput 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
modeSensor operating mode.An integer from 0 to 5.
ModePurpose
0Operation without indication.
1Basic color recognition.
2Recognition of colored objects only.
3True color mode.
4Raw RGBC readings.
5Ambient readings without illumination.
ResultMeaning
trueThe I²C command was acknowledged.
falseThe command failed: check the arguments, address, and connection.
if (!sensor.setMode(4)) {
  Serial.println("I2C error");
}

sensor.getAddress()object address

How it works

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

How it works

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.

Arguments
ArgumentPurposeAccepted values
newAddressThe sensor's I²C address.0x40–0x60; must differ from the current address and be unused.
ResultMeaning
trueThe sensor responded at the new address; the object now uses it.
falseThe 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

How it works

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.

Arguments
ArgumentPurposeAccepted values
dataToSendMode 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

How it works

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() {
}