Make Two Robots Do the Wave with MicroBits

Make Two Robots Do the Wave

The ‘Wave’ is a great group activity.  Any stadium full of people can do it. Someone stands up and raises their hands, and sits back down.  The person next to them does the same thing, and it goes on throughout the stadium until people lose interest in the wave, or gain interest in whatever they should be watching.

It is a perfect activity for the MicroBit.

The instructions below are for two robots, but it can be extended to any number of robots.  Here the arms of the robot are pipe cleaners that have been attached to a servo motor.

When the first robot raises its arms, it pauses briefly, sends a signal to another robot, and then lowers its arms.  When the second robot gets the signal, it raises it arms, pauses, and then lowers them. To send the wave back, the second robot raises it arms, pauses, radios the first robot, and lowers its arms.  When the first robot gets the signal, it raises its arms, pauses, and lowers them.

 

Materials

  • 2 MicroBits
  • 2 Servo Motors
  • 3 Alligator clips
  • 2 male to male jumper wires
  • Pipe Cleaners and clips
  • Robot materials (cardboard, tape, craft stuff)

Connections

Using the Alligator clips make connections to ‘0’, ‘3V’, and ‘GND’ on the MicroBit.  

Then connect the jumper wires to the servo and the Alligator clips.  Use the connections below:

  • Servo Brown – MicroBit GND
  • Servo Red – MicroBit 3V
  • Servo Orange – MicroBit 0

At this point your servo is connected and should work. There is some programming you will need to do.

First Time Servo Use

Out of the box (in my experience, at least) the MicroBit was not programmed to work with the servo, so I needed to give it the missing software.  Using the Python programming part of the MircroBit site, I ran this program.

This gives the MicroBit a Servo class that it can use from that point on.  You only need to do this once per MicroBit.


# Rotates the servo depending on the microbit's rotation through the x axis.
# pressing button_a sweeps the servo from 0 degrees to 180 degrees
# pressing button_b gives 0 degrees then 180 degrees.
# Tested with SG90 servo @ 3.3v

from microbit import *

class Servo:

"""
A simple class for controlling hobby servos.
Args:
pin (pin0 .. pin3): The pin where servo is connected.
freq (int): The frequency of the signal, in hertz.
min_us (int): The minimum signal length supported by the servo.
max_us (int): The maximum signal length supported by the servo.
angle (int): The angle between minimum and maximum positions.
Usage:
SG90 @ 3.3v servo connected to pin0
= Servo(pin0).write_angle(90)
"""

def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180):
self.min_us = min_us
self.max_us = max_us
self.us = 0
self.freq = freq
self.angle = angle
self.analog_period = 0
self.pin = pin
analog_period = round((1/self.freq) * 1000) # hertz to miliseconds
self.pin.set_analog_period(analog_period)

def write_us(self, us):
us = min(self.max_us, max(self.min_us, us))
duty = round(us * 1024 * self.freq // 1000000)
self.pin.write_analog(duty)
self.pin.write_digital(0) # turn the pin off

def write_angle(self, degrees=None):
degrees = degrees % 360
total_range = self.max_us - self.min_us
us = self.min_us + total_range * degrees // self.angle
self.write_us(us)

while True:
if button_a.is_pressed():
for x in range(0, 180, 5):
# from 0 to 180 in steps of 5
# write the angle of the step (x)
Servo(pin0).write_angle(x)
sleep(200)
if button_b.is_pressed():
# show maximum and minimum rotation if button
# b pressed
Servo(pin0).write_angle(0)
sleep(2000)
Servo(pin0).write_angle(180)
sleep(2000)
else:
# rescale accelerometer x axis to between 0 and 180
rescaled_angle = rescale((-1024, 1024), (0, 180), accelerometer.get_x())
Servo(pin0).write_angle(rescaled_angle) # write rescaled angle
sleep(200)

Robot One

You can then use the JavaScript Blocks Editor to program the first robot.  I used the program below.

When the program starts, it gets a radio group (5).  Then we set the arms to 125, which on our robot meant the arms were down.

When we press A, the servo goes to 0 (arms up) waits for a short time sends a radio signal (2), the servo goes back to 125 (arms down), and we scroll ‘GO!’ on the LEDs.

When this robot gets a signal (3), it pauses for a moment, then raises and lowers it arms like before, and then scrolls ‘GO!’.

Robot 2

You can then use the JavaScript Blocks Editor to program the first robot.  I used the program below.

When the program starts, it gets a radio group (5) and then sets the servo to 0 (arms down).

When it gets a signal it sets the servo to 125 (arms up), pauses, sets the servo to 0 (arms down), and then says ‘GO!’ on the LEDS.  It then pauses, sets the servo to 125 (arms up), signals the other robot, pauses, and then lowers its arms and says ‘GO!’ again.

It took a little experimentation to figure out the servo settings based on where I put the arm at the start.  After this, you can can connect them to the cardboard robots, and you’ll have your own wave.

Challenge

Put in a third robot, between these.  What would be different about the program for that one?

 

Leave a Reply

Your email address will not be published. Required fields are marked *