Everything But Your Mind

You have the parts now; let’s put them together. This code will have everything but mind control. Here’s what will happen:

Start the bot, and it plays a sound. You can set a movement threshold with the potentiometer. getAttention() is essentially a dummy function that always returns 50% attention. When you turn the threshold under this, the bot starts moving. The multicolor LED shows the threshold level, from 0% blue to 100% red.

When you stop the bot by turning the threshold above 50%, it will slowly grind to a halt. It’s not really that heavy, but the code is faking inertia. Fake inertia will make the robot move smoothly with mind control.

Connect the parts (Figure 2-27) and upload the code.

Circuit diagram for allbutmind.pde

Figure 2-27. Circuit diagram for allbutmind.pde

Code Structure

This code uses only one global variable, speed. For fake inertia to work, we need to know the last speed the robot was moving at as we slow down. This global variable is used only in loop() to keep the program structure clean. The speed variable is sent to functions as a regular parameter.

The three input sensors are processed with lineDetected(), getAttention(), and getThreshold().

Outputs include servos, LEDs, and speakers. The most important output functions are forward(), turn(), sayReady(), and setBlueToRed(). The rest of the output functions are just helper functions to outputs: pulseServo(), wave(), and

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.