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.
Figure 2-27. Circuit diagram for allbutmind.pde
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
Outputs include servos, LEDs, and speakers. The most important output functions are
setBlueToRed(). The rest of the output functions are just helper functions to outputs: