Chapter 2. Coding

The code that controls your robot is simple. Well, it’s simpler than one would expect for a sci-fi trick like mind control.

A simplified, pseudocode version of your robot would be as follows:

void loop()
{
        while (lineDetected()) turn();  // 1
        forward(getAttention());        // 2
}
1

The reflectivity sensor tells if the robot is on top of a black line, which forms the border of the arena. When the robot is on the border, it turns until the sensor sees white again.

2

Then the robot goes forward. The speed is read from the EEG headband.

Rinse and repeat.

In this coding part of the book, you’ll write each part of the code that makes your robot tick.

Moving

Continuous rotation servos are motors you can easily control. With continuous rotation servos, you can only control the speed (forward, stop, back), but unlike standard servos, you can’t set the servo to point at a specific angle.

There are three wires going to a servo: red (positive), black (ground), and white (data). To control a servo, you need to keep sending pulses to it. The length of the pulse tells how fast the full rotation servo ...

Get Make a Mind-Controlled Arduino Robot now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.