When building an electronic sculpture, what we are often interested in is giving it behavior. We don't simply want to have it flash lights and move motors, we want it to interact with its environment as if it were alive. We call this kind of behavior agency.
Cecil is a sock puppet, made to look like a sea serpent. Here we show him without his sock puppet skin, so you can see his inner workings. His eyes are the two ultrasonic transducers that view the area immediately in front of him, and notice changes in the distance between the robot and people passing by.
When at rest, Cecil stands up straight. If he notices that something is now within about a meter in front of him, he leans forward, as if curious. This is accompanied by the sound of the gears in his servo motor, which attracts the attention of the person passing by. If that person comes within about a foot of Cecil, he starts backing away, as if apprehensive about someone invading his personal space.
This simple behavior is enough to delight anyone coming in contact with him for the first time. He seems to have agency, giving a sense that he has a mind, and that he has feelings such as curiosity and shyness.
Hidden in Cecil's wooden box base is a Launchpad and a solderless breadboard. The breadboard is not really even needed, as we are only using it to hold a single resistor that could have easily been soldered to the wire leading to the sonar sensor. As we saw in our first sonar sensor program, that resistor allows the 5 volt sonar sensor to communicate with the 3.3 volt Launchpad.
The other piece to Cecil is his servo motor. No other electronic parts are needed.
A simple trick of geometry is used to keep Cecil's head level as he leans forward and back. We form a parallelogram of wood slats, or in this case foam core board (since it makes prototyping fast and easy). The hinges are simply clear packing tape. As you push on a parallelogram, it can become a rectangle when the sides are upright, or it can slant forward or back, but at all times the top side is level with the bottom side. This keeps Cecil's sonar sensor always looking forward, never upwards or downwards.
The servo is connected to the back slat by a stiff steel wire. As the servo turns, it either pushes or pulls on the back slat, by means of the stiff wire.
See the sections on servo motors and the sonar sensor for details on how to connect and use these parts. They are simple, and your little robot goes together quickly.
The code for detecting distance using the sonar sensor is taken from that project, and is explained there. The rest of the code should be simple to follow. The loop() function gets the distance, and if nothing is detected, it directs Cecil to straighten up. If something is within about 3 feet or so, the code calls lean_in(). If the distance is a foot or less, the code calls back_up().
The lean_in() function decrements the angle of the servo a little bit each time it is called, until we reach a limit (MIN_ANGLE). The back_up() function does the same thing in reverse. The straighten_up() function changes angle by one in the direction of 90 degrees each time it is called.
In setup(), we use P2.0 for the servo motor output, P2.1 for the sonar sensor input, and P2.2 for the sonar trigger. We wait 6 seconds before printing anything to give you time to open the Serial Monitor window.
Cecil a simple robot sculpture