Funduino Funbot – Assembly and Programming

FunduinoPublished on

Vom Zusammenbau bis zum selbstfahrenden Roboter – in elf Lektionen

Der Funbot ist ein fertiges Roboter-Kit: Alle Bauteile sitzen bereits auf einer Platine, es muss nichts verkabelt und nichts gelötet werden. Nach vier Handgriffen steht der Roboter – und die eigentliche Arbeit kann beginnen.

Diese Anleitung ist ein vollständiger Kurs. In elf aufeinander aufbauenden Lektionen gehst du von der blinkenden Leuchtdiode bis zum Roboter, der einer Linie folgt, vor Hindernissen anhält und mit Hilfe eines Lagesensors geradeaus fährt. Jede Lektion führt genau einen neuen Baustein ein – Variablen, if-Abfragen, Bibliotheken – und endet mit einem lauffähigen Sketch.

Robotic vehicle on a circuit board with wheels, an ultrasonic sensor, and LEDs
The fully assembled Funduino Funbot
Underside of the Funbot with motors and ball caster installed
Motors and ball caster on the underside of the board
1 / 2

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

What makes the Funbot special

On a moving robot, you can immediately see what a line of code does. That’s exactly why small robot vehicles are among the most popular Arduino projects in the classroom.

All the components on the Funbot are already firmly mounted on a circuit board. There’s no wiring or soldering needed - once assembled, you can start programming right away. This makes it especially suitable for computer science classes, where the focus is on software anyway.

The Funbot is compatible with a range of microcontrollers: Arduino UNO, Arduino NANO, ESP32 and Raspberry Pi Pico, as well as boards in the same form factor. You can program it using the classic Arduino IDE or visually with blocks, for example with OpenRoberta.

Robotic vehicle on a circuit board with wheels, an ultrasonic sensor, and LEDs
The fully assembled Funduino Funbot

Assemble the Funbot

Assembly consists of four simple steps:

  • Prepare the board: Carefully break off the edges at the breakaway points, and lay out the individual components next to the board.
  • Install the motors: Attach the two motors with the white brackets to the underside. The protruding part of each bracket should face outward, and the wire colors should match on both sides.
  • Install the ball caster: Attach it at the front of the underside using the short nylon screws and spacers.
  • Add the components: Plug the remaining modules into their designated connectors - and you're done.

When plugging in the motors, make sure you connect them to the correct side: viewed from above, the left motor goes into the left connector and the right motor into the right connector. If a wheel later spins the wrong way, it's no big deal - just swap the two direction pins in the sketch.

Underside of the Funbot with motors and ball caster installed
Motors and ball caster on the underside of the board

Power supply

The USB cable is all you need for programming and testing: it can communicate with all the modules. However, it cannot reliably provide enough power for the drive motors.

To let the Funbot move freely, connect an external 6 to 9 V DC power supply to the DC jack. A voltage converter on the board converts this to 5 V for the microcontroller, so you don't need a second power source.

Conveniently, the battery and USB cable can be connected at the same time. So you can program without constantly switching connections.

Practical tip: Rechargeable 9 V batteries are available that can be charged via USB cable. They’re better for the environment and your wallet - especially when several groups are working at the same time.

The Funbot modules at a glance

These components are mounted on the board and can be programmed via the microcontroller:

  • Front headlights – two LEDs at the front, switched together via pin A1, with a 200-ohm series resistor
  • Two buttons – left on pin A2, right on pin A3, each with a 10-kilohm pulldown resistor
  • Speaker – on pin A0, a passive component: sound must be generated using the tone command
  • Two N20 drive motors – controlled via the L293D motor driver
  • Six WS2812B pixels on the underside – on pin 13, each individually controllable in thousands of colors
  • RCW-0001 ultrasonic sensor – works like the well-known HC-SR04, but runs at both 5V and 3.3V
  • Line sensor – inserted horizontally at the front, reports light or dark as a digital signal
  • MPU6050 orientation sensor – gyroscope and accelerometer, connected via I2C

The Funbot can be expanded with your own 3D-printed attachments. The base plate, with its mounting holes, is available as an STL file, allowing you to build mounts for additional modules – including AI cameras like the Pixycam or Huskylens.

The six pixels on the underside are numbered 0 to 5 - as is customary in programming, counting starts at zero.

Lesson 1: The flashing headlights

The blinking LED is the classic first program in microelectronics - and it introduces three fundamental building blocks right away.

Task: Turn on the front LEDs, wait one second, turn them off again, and wait another second.

  • The variable at the top keeps track of which pin the LEDs are connected to. int stands for integer, a whole number. You can choose the name freely - it just has to stay the same throughout the sketch.
  • In setup, pinMode specifies that this pin should be used for output: OUTPUT.
  • In loop, digitalWrite turns the LEDs on with HIGH and off with LOW.

Without the two delay commands, the blinking would happen so fast that we wouldn't be able to see it - the LEDs would simply appear to stay on continuously. The pauses are what make the change visible. And because loop immediately starts over, the second pause is needed after switching the LEDs off, too.

The LEDs are connected to A1, an analog pin. Still, you control them with digitalWrite. That’s no contradiction: Analog pins can do everything digital pins can, and can also read analog values.

To think about: What actually happens if you move the second pause to the beginning of the loop section?

Lesson 1: The flashing headlights
int modul_LED = A1;
void setup()
{
  pinMode(modul_LED, OUTPUT);
}

void loop()
{
  digitalWrite(modul_LED, HIGH);  // Headlight on
  delay(1000);                    // wait one second
  digitalWrite(modul_LED, LOW);   // Headlight off
  delay(1000);                    // wait one second
}

Lesson 2: Reading the two push buttons

Task: When the left button is pressed, the headlights should light up for two seconds, and for four seconds when the right button is pressed.

There are three new things here. First, a button is set to INPUT in the setup instead of OUTPUT - after all, something needs to be read from this pin. Second, digitalRead reads the state of the pin and stores it in a variable: 1 for HIGH, 0 for LOW.

Third, the if statement comes into play. It executes a section only if a condition is met - just like in everyday life: If rain is forecast, you take an umbrella. Here, the condition is: Is the value of the button variable exactly 1?

There are two equals signs in the condition. A single = assigns a value to a variable; two == compare two values. This is one of the most common beginner mistakes.

Use descriptive names: TasterL for left, TasterR for right. It doesn’t matter much in this little sketch - but as soon as you program all the modules together, the names will determine whether you can still find your way around.

Lesson 2: Read both buttons
int modul_TasterL = A2;
int modul_TasterR = A3;
int modul_LED = A1;

int TasterL_status = 0;
int TasterR_status = 0;

void setup()
{
  pinMode(modul_TasterL, INPUT);
  pinMode(modul_TasterR, INPUT);   // auch der rechte Taster ist ein Eingang
  pinMode(modul_LED, OUTPUT);
}

void loop()
{
  // Linker Taster: Scheinwerfer fuer 2 Sekunden
  TasterL_status = digitalRead(modul_TasterL);
  if (TasterL_status == 1)
  {
    digitalWrite(modul_LED, HIGH);
    delay(2000);
    digitalWrite(modul_LED, LOW);
  }

  // Rechter Taster: Scheinwerfer fuer 4 Sekunden
  TasterR_status = digitalRead(modul_TasterR);
  if (TasterR_status == 1)
  {
    digitalWrite(modul_LED, HIGH);
    delay(4000);
    digitalWrite(modul_LED, LOW);
  }
}

Lesson 3: Tones with the Speaker

Task: Whenever the speaker plays a sound, the headlights should also light up. Then there’s a short pause, and it starts all over again.

The speaker on pin A0 is an output - after all, the microcontroller sends something to it. However, you don’t need a pinMode command for it in setup: the command tone takes care of that automatically.

Two commands go together: tone starts the sound at a specific pitch, noTone turns it off again. Note the uppercase T - the Arduino software is case-sensitive.

The speaker on the Funbot is a passive component. A simple digitalWrite with HIGH therefore won’t produce a tone, at most a click - the tone has to be generated by the microcontroller.

To think further: What would the program look like if the light and sound were to run alternately instead of simultaneously?

Lesson 3: Sound and light alternating
int modul_speaker = A0;
int modul_led = A1;

void setup()
{
  pinMode(modul_led, OUTPUT);
  // Fuer den Lautsprecher ist kein pinMode noetig:
  // der tone-Befehl kuemmert sich selbst darum.
}

void loop()
{
  tone(modul_speaker, 100);        // Ton mit 100 Hertz ausgeben
  digitalWrite(modul_led, HIGH);   // Scheinwerfer an
  delay(1000);

  noTone(modul_speaker);           // Ton abschalten
  digitalWrite(modul_led, LOW);    // Scheinwerfer aus
  delay(1000);
}

Lesson 4: The drive motors

The two N20 motors don't get their power from the microcontroller - its pins aren't powerful enough. The L293D motor driver, an H-bridge, sits between them. The microcontroller sends only control signals; the driver switches the power.

Each motor has two direction inputs. Their combination determines what the motor does:

  • HIGH and HIGH - the motor stays still
  • HIGH and LOW - the motor turns in one direction
  • LOW and HIGH - the motor turns in the other direction
  • LOW and LOW - the motor stays still

A third pin, the Enable pin, controls the speed. Since digitalWrite only knows on and off, we use analogWrite here: values between 0 and 255 determine how much power the driver lets through. This uses pulse-width modulation, which works only on certain pins - the Funbot has suitable pins wired for this.

Task: First, make only the left motor run at full power. Then extend the sketch so that the wheel turns forward for two seconds, stops for one second, and then turns backward for two seconds.

If a wheel spins in the wrong direction, this is due to its design and is not a fault. Simply swap the two direction pins for that motor in the sketch - change MotorLa HIGH and MotorLb LOW to MotorLa LOW and MotorLb HIGH.

Motor driver diagram with the numbered wires to both motors
Wiring the two motors to the L293D motor driver
Lesson 4: Let the left motor turn
int MSL = 9;        // Motor Speed Links - Enable-Pin des linken Motors
int MotorLa = 7;    // Richtungspin a
int MotorLb = 8;    // Richtungspin b

void setup()
{
  pinMode(MSL, OUTPUT);
  pinMode(MotorLa, OUTPUT);
  pinMode(MotorLb, OUTPUT);
}

void loop()
{
  analogWrite(MSL, 255);           // volle Geschwindigkeit (0 bis 255)
  digitalWrite(MotorLa, LOW);      // eine Richtung: a LOW, b HIGH
  digitalWrite(MotorLb, HIGH);
}

Lesson 5: Drive, little robot!

For the Funbot to actually move, both wheels need to turn. Going from one motor to two is a small step: add the three variables for the right motor - MSR for the speed and MotorRa and MotorRb for the direction - set them as outputs in setup, and control them in loop just like the left one.

Then try what happens when you choose different speeds. Can you get the Funbot to drive in a circle?

Lesson 5: Both Motors - the Funbot drives
int MSL = 9;        // left motor
int MotorLa = 7;
int MotorLb = 8;

int MSR = 6;        // right motor
int MotorRa = 5;
int MotorRb = 4;

void setup()
{
  pinMode(MSL, OUTPUT);
  pinMode(MotorLa, OUTPUT);
  pinMode(MotorLb, OUTPUT);
  pinMode(MSR, OUTPUT);
  pinMode(MotorRa, OUTPUT);
  pinMode(MotorRb, OUTPUT);
}

void loop()
{
  analogWrite(MSL, 255);
  digitalWrite(MotorLa, LOW);
  digitalWrite(MotorLb, HIGH);

  analogWrite(MSR, 255);
  digitalWrite(MotorRa, LOW);
  digitalWrite(MotorRb, HIGH);
}

Lesson 6: Follow the line

Now the Funbot gets a sensor that controls its movement for the first time. Goal: It follows a black line on its own - for example, a strip of electrical tape on the table.

To do that, it needs to be able to turn. It does this by adjusting its speed: If the left motor turns faster than the right one, the Funbot turns right - and vice versa.

The line sensor provides the same kind of signal as a button: 1 for a light surface, 0 for the dark line. So you read it in the same way as the button in Lesson 2.

New is the extension of the if statement with else: This lets you specify what should happen when the condition is not met. In words, the program says: If a light surface is detected, turn right. Otherwise, turn left. This makes the Funbot move back and forth along the edge of the line.

Calibrate the sensor before uploading the sketch. Position the Funbot so the sensor is above the black line, then turn the blue potentiometer with a small screwdriver until the sensor’s status LED turns off. Don’t turn it too far, or it won’t turn back on over a light-colored surface.

An intermediate step helps with testing: first, make the headlights light up only when the line is detected. Add the motors only once this works reliably.

Lesson 6: Follow the line
int modul_Tracking = 12;
int Tracking_status = 0;

int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;

int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;

void setup()
{
  pinMode(modul_Tracking, INPUT);
  pinMode(MSL, OUTPUT);
  pinMode(MotorLa, OUTPUT);
  pinMode(MotorLb, OUTPUT);
  pinMode(MSR, OUTPUT);
  pinMode(MotorRa, OUTPUT);
  pinMode(MotorRb, OUTPUT);
}

void loop()
{
  Tracking_status = digitalRead(modul_Tracking);

  if (Tracking_status == 1)
  {
    // Heller Untergrund erkannt: Rechtskurve,
    // der linke Motor dreht schnell, der rechte langsam.
    analogWrite(MSL, 255);
    digitalWrite(MotorLa, LOW);
    digitalWrite(MotorLb, HIGH);

    analogWrite(MSR, 40);
    digitalWrite(MotorRa, LOW);
    digitalWrite(MotorRb, HIGH);
  }
  else
  {
    // Dunkle Linie erkannt: Linkskurve,
    // jetzt dreht der rechte Motor schnell.
    analogWrite(MSL, 40);
    digitalWrite(MotorLa, LOW);
    digitalWrite(MotorLb, HIGH);

    analogWrite(MSR, 255);
    digitalWrite(MotorRa, LOW);
    digitalWrite(MotorRb, HIGH);
  }
}

Lesson 7: Measuring distance with ultrasound

The ultrasonic sensor works like a bat: it emits sound waves and waits for them to bounce back from an object. The distance can be calculated from the travel time.

Two pins are involved: Trigger starts the measurement, Echo reports when the wave returns. To do this, briefly set Trigger to LOW, then to HIGH for 10 microseconds, and back to LOW.

The command pulseIn measures the travel time – the second-to-last character is an uppercase i, not a lowercase L. Because the measured time includes both the outward and return journeys, it is halved and then multiplied by the speed of sound:

Distance = (measured time ÷ 2) × speed of sound

Task: Measure the distance to your hand or a wall. If it is less than 30 centimeters, the headlights should light up.

A new data type appears here for the first time: float. An int can only store whole numbers - for the speed of sound of 0.0341 centimeters per microsecond, you need decimal places. Important: In the sketch, write 0.0341 with a decimal point, not a comma.

Lesson 7: Measure distance and light up when under 30 cm
int modul_led = A1;
int modul_trigger = 10;
int modul_echo = 11;

float entfernung = 0;
float gemessene_zeit = 0;
float zeit_pro_strecke = 0;

// Schallgeschwindigkeit in Zentimetern je Mikrosekunde
float schallgeschwindigkeit = 0.0341;

void setup()
{
  pinMode(modul_led, OUTPUT);
  pinMode(modul_trigger, OUTPUT);
  pinMode(modul_echo, INPUT);
}

void loop()
{
  // Trigger kurz ausschalten, damit keine Stoersignale anliegen
  digitalWrite(modul_trigger, LOW);
  delayMicroseconds(5);

  // Messung starten: 10 Mikrosekunden HIGH
  digitalWrite(modul_trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(modul_trigger, LOW);

  // Zeit messen, bis das Echo eintrifft
  gemessene_zeit = pulseIn(modul_echo, HIGH);

  // Nur der Hinweg zaehlt, also durch zwei teilen
  zeit_pro_strecke = gemessene_zeit / 2;

  // Weg = Zeit mal Geschwindigkeit
  entfernung = zeit_pro_strecke * schallgeschwindigkeit;

  if (entfernung < 30)
  {
    digitalWrite(modul_led, HIGH);
    delay(1000);
    digitalWrite(modul_led, LOW);
  }
}

Lesson 8: Driving and stopping in front of obstacles

Now two lessons are being combined: The Funbot drives straight ahead while continuously measuring the distance in front of it. If an object comes closer than 30 centimeters, it stops.

The combination always follows the same pattern: The variables from both sketches go at the top, the pinMode lines from both sketches go in setup, and the loop contains the motor commands first, followed by the measurement. Finally, the code that stops both motors goes in the if statement - both direction pins set to LOW and the speed set to 0.

When combining them, make sure to address both motors. A common mistake is to copy the block for the left motor and forget to change the variable names to those for the right motor - then the Funbot drives in circles instead of straight ahead.

Lesson 8: Drive and stop in front of an obstacle
int modul_led = A1;
int modul_trigger = 10;
int modul_echo = 11;

float entfernung = 0;
float gemessene_zeit = 0;
float zeit_pro_strecke = 0;
float schallgeschwindigkeit = 0.0341;

int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;

int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;

void setup()
{
  pinMode(modul_led, OUTPUT);
  pinMode(modul_trigger, OUTPUT);
  pinMode(modul_echo, INPUT);
  pinMode(MSL, OUTPUT);
  pinMode(MotorLa, OUTPUT);
  pinMode(MotorLb, OUTPUT);
  pinMode(MSR, OUTPUT);
  pinMode(MotorRa, OUTPUT);
  pinMode(MotorRb, OUTPUT);
}

void loop()
{
  // Beide Motoren vorwaerts
  analogWrite(MSR, 255);
  digitalWrite(MotorRa, LOW);
  digitalWrite(MotorRb, HIGH);

  analogWrite(MSL, 255);
  digitalWrite(MotorLa, LOW);
  digitalWrite(MotorLb, HIGH);

  // Abstand messen
  digitalWrite(modul_trigger, LOW);
  delayMicroseconds(5);
  digitalWrite(modul_trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(modul_trigger, LOW);

  gemessene_zeit = pulseIn(modul_echo, HIGH);
  zeit_pro_strecke = gemessene_zeit / 2;
  entfernung = zeit_pro_strecke * schallgeschwindigkeit;

  // Hindernis naeher als 30 cm: beide Motoren stoppen
  if (entfernung < 30)
  {
    analogWrite(MSR, 0);
    digitalWrite(MotorRa, LOW);
    digitalWrite(MotorRb, LOW);

    analogWrite(MSL, 0);
    digitalWrite(MotorLa, LOW);
    digitalWrite(MotorLb, LOW);
  }
}

Lesson 9: The underbody lighting

The six WS2812B pixels on the underside can be controlled individually in thousands of colors - all via a single data line. In the classroom, they’re more than decoration: they show which part of the program the robot is currently in. If it lights up green, it should move; if it still doesn’t move, the problem is with the motors, not the logic.

This is where a library comes into play for the first time: ready-made code written by others that you use in your sketch. Install it via Sketch → Include Library → Manage Libraries and search there for Adafruit NeoPixel.

In your sketch, call it with #include. Then group the six pixels into an object named BodenLED and pass it four values: number of pixels, data pin, color space, and frequency.

Task: Make pixels 0 and 1 glow red, pixels 2 and 3 green, and pixels 4 and 5 blue. Specify the colors as RGB values - red is (255, 0, 0), green (0, 255, 0), and blue (0, 0, 255).

setPixelColor only sets the color a pixel should have - nothing is visible yet. Only BodenLED.show() sends all the set colors to the pixels at once. Without this line, the underbody lighting stays dark.

To keep experimenting: What color do the values (71, 138, 132) produce?

Arduino IDE Library Manager with the Adafruit NeoPixel library
The “Adafruit NeoPixel” library in the Library Manager
Lesson 9: The Six Pixels in Three Colors
#include <Adafruit_NeoPixel.h>
#define PIN 13        // Pixel data line
#define NUMPIXELS 6   // Number of pixels on the Funbot

Adafruit_NeoPixel BodenLED = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  BodenLED.begin();   // Start the object - comparable to pinMode
}

void loop()
{
  // setPixelColor sets the color, but does not display it yet.
  BodenLED.setPixelColor(0, BodenLED.Color(255, 0, 0));   // red
  BodenLED.setPixelColor(1, BodenLED.Color(255, 0, 0));
  BodenLED.setPixelColor(2, BodenLED.Color(0, 255, 0));   // green
  BodenLED.setPixelColor(3, BodenLED.Color(0, 255, 0));
  BodenLED.setPixelColor(4, BodenLED.Color(0, 0, 255));   // blue
  BodenLED.setPixelColor(5, BodenLED.Color(0, 0, 255));

  BodenLED.show();    // Only this command actually switches the pixels
}

Lesson 10: The MPU6050 accelerometer

The MPU6050 combines two sensors in one chip: a gyroscope that detects rotation around the X, Y, and Z axes, and an accelerometer. Together, they determine orientation in three-dimensional space - the same technology used in VR headsets and in the flight control systems of model aircraft.

It connects via the I2C bus, using pins A4 and A5. The library you need is called TinyMPU6050 by Gabriel Milan, and you install it via the Library Manager, just like the NeoPixel library.

Task: Display the rotation angle around the Z axis in the Serial Monitor. Then turn the Funbot left and right and watch how the value changes.

You’ll need three commands from the library: MPU.Initialize() starts the sensor, MPU.Calibrate() records the initial position, and MPU.Execute() retrieves the current measurements in the loop.

When uploading, place the Funbot upright on the table and leave it still until “Calibration complete” appears in the Serial Monitor. If you move it during calibration, the zero point will be incorrect afterward.

Lesson 10: Read the Z-axis angle
#include <TinyMPU6050.h>
// Object for the sensor. (Wire) means: communication via I2C.
MPU6050 MPU(Wire);

void setup()
{
  MPU.Initialize();
  Serial.begin(9600);

  Serial.println("Calibration starting, please wait!");
  MPU.Calibrate();
  Serial.println("Calibration complete");
}

void loop()
{
  MPU.Execute();   // retrieve current measurement data

  Serial.print("Z-axis angle = ");
  Serial.println(MPU.GetAngZ());
}

Lesson 11: Drive straight using the position sensor

Two motors never run at exactly the same speed - manufacturing tolerances make every vehicle veer slightly to one side. The orientation sensor can compensate for this: when it starts up, the Funbot remembers its initial direction and steers against any deviation.

The principle: In setup, the Z-axis angle is read once and saved as the baseline. In the loop, the current angle is continuously measured to see how far it deviates from the baseline. If the Funbot has veered left, the right motor slows down - and vice versa.

The sketch brings together what you learned in the lessons on motors and the orientation sensor. It is fully commented; detailed explanations are in the two previous lessons.

The Funbot doesn't drive perfectly straight with this approach, but follows a slight curve around the desired direction - constantly correcting its course. This is the simplest form of feedback control. If you like, you can make the countersteering gentler by reducing the difference between the two speeds.

The same principle can also be used to make an exact 90-degree turn: simply keep turning until the deviation reaches 90.

Lesson 11: Drive straight using the position sensor
// First, include the libraries and create the variables.
// 1. For the orientation sensor
#include <TinyMPU6050.h>
MPU6050 MPU(Wire);
float InitialPosition = 0;
float Deviation = 0;

// 2. For the motors
int MSL = 9;
int MotorLa = 7;
int MotorLb = 8;
int MSR = 6;
int MotorRa = 5;
int MotorRb = 4;

void setup() {
  // Start serial communication to read the measured values
  Serial.begin(9600);

  // Set the inputs and outputs
  pinMode(MSL, OUTPUT);
  pinMode(MotorLa, OUTPUT);
  pinMode(MotorLb, OUTPUT);
  pinMode(MSR, OUTPUT);
  pinMode(MotorRa, OUTPUT);
  pinMode(MotorRb, OUTPUT);

  // Start and calibrate the sensor
  MPU.Initialize();
  Serial.println("Calibration starting, please wait!");
  MPU.Calibrate();
  Serial.println("Calibration complete");

  // Record and store the initial position once
  MPU.Execute();
  InitialPosition = MPU.GetAngZ();
}

void loop() {
  // Get the current readings from the orientation sensor
  MPU.Execute();

  // How far has the Funbot deviated from its initial position?
  // To find out, subtract the initial position from the current value.
  Deviation = MPU.GetAngZ() - InitialPosition;
  Serial.println(Deviation);

  // Note: The value increases counterclockwise,
  // and decreases clockwise, becoming negative as well.
  if (Deviation >= 0) {
    // Deviated to the left, so countersteer slightly to the right
    analogWrite(MSL, 255);
    digitalWrite(MotorLa, LOW);
    digitalWrite(MotorLb, HIGH);

    analogWrite(MSR, 40);
    digitalWrite(MotorRa, LOW);
    digitalWrite(MotorRb, HIGH);
  }
  else {
    // Deviated to the right, so countersteer slightly to the left
    analogWrite(MSL, 40);
    digitalWrite(MotorLa, LOW);
    digitalWrite(MotorLb, HIGH);

    analogWrite(MSR, 255);
    digitalWrite(MotorRa, LOW);
    digitalWrite(MotorRb, HIGH);
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.