Magic 8 Ball Bot

40-60m

Introduction

Ready to reach into the future? Yes? No? Maybe so? In this activity, program your robot into an all-knowing Magic 8 Bot. Get the answers to your deepest questions. Will it rain tomorrow? Will you become a superstar? Who knows!

Getting setup

First, you’ll need to get started with a few programs! Here’s a list of the things you’ll need:

  • Computer
  • Arduino + LRF Library
  • Printer
  • Paper
  • Scissors
  • Colouring Pencils or Markers
  • Tape

Download the Costume: magic-8ball-bot-costume.pdf.

Download the Code: magic-8ball-bot-code.zip.

Important

This is an Arduino activity, so make sure you download the program and the library here.

Explore your code

Once you’re all setup, check out a sample snippet of your code below. It’s much longer, but here’s what you should see.

#include <LittleRobotFriends.h>

int fate = 0;

bool headHeld = false;
bool firstRun = true;

LRFExpression thinkingExpression =
{
  .pattern = { LRFColor_White, LRFColor_Blue, LRFTransform_Random, LRFDuration_DoubleLong },
  .sounds = {
    { LRFNote_C, LRFOctave_3, LRFIntonation_Rising, LRFDuration_DoubleLong, LRFDuration_Medium },
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK
  }
};

What does this program do?

The program tells your robot when its hair sensor has been hugged (hug = hold and release), which sets it into thinkingExpression mode. Ask your robot a question, then release its hair sensor to get your answer! Here’s what each Magic 8 Bot eye colour represents:

  • White = Thinking…
  • Blue = Yes
  • Red = No
  • Yellow = Maybe So

Basically, when the hair sensor is hugged and released the program tells your robot to choose a random eye colour. This happens in the checkHeadSensor part of the code. The program is saying “if hair sensor is hugged, choose a random fate”, then the program provides 3 cases scenarios: no, yes, and maybe.

Take a look at the code below!

void checkHeadSensor() {

  if (lrf.touch.readHair() == 1) {

    if (headHeld == false) {

      headHeld = true;
    }



  }

  else if (lrf.touch.readHair() == 0) {

    if (headHeld == true) {

      fate = random(3);
      lrf.eyes.set(0, 0, 0);
      switch (fate) {
        case 0 :
          lrf.blinkAndSay(&noExpression);
          break;

        case 1 :
          lrf.blinkAndSay(&yesExpression);
          break;

        case 2 :
          lrf.blinkAndSay(&maybeExpression);
          break;
      }
      headHeld = false;
    }
  }
}

Upload this trick onto your robot

It’s time to upload the Magic 8 Bot trick onto your robot. You’ll need to connect your robot to the computer with the USB cable.

Make sure your board and port is connected, then simple click the upload button!

Upload

You’ll know when the trick is uploaded onto your robot when the Arduino Sketch says “Upload Complete”.

Upload Complete

Now, try out the trick! Ask your robot a question about the future, hold its hair sensor, and release when you want to know the answer!

Don’t Forget

Each colour represents: yes, no, maybe so. Check out the explore your code section.

Print your Fortune Teller costume (optional)

Print the costume out on regular paper, or cardstock paper if you want it to last longer. Whip out the pencil crayons, markers, and scissors. Secure the costume onto your robot with a bit of tape and you’re done! Remember, the trick requires you to interact with the touch sensors and the costume might cover them, so just take it off when you want to try to trick!

Costume

Don’t Forget

Download your costume here!

Important

Make sure you print your costume in Actual Size, otherwise it won’t fit!

What’s next?

If you want to take this activity one step further, try completing a few of these challenges!

Challenge 1:

Make the Thinking Expression four notes, instead of one! You’ll need to change the section of the code below.

LRFExpression thinkingExpression =
{
  .pattern = { LRFColor_White, LRFColor_Blue, LRFTransform_Random, LRFDuration_DoubleLong },
  .sounds = {
    { LRFNote_C, LRFOctave_3, LRFIntonation_Rising, LRFDuration_DoubleLong, LRFDuration_Medium },
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK
  }
};

Challenge 2:

Make the Yes Expression of your robot sound more excited! Here’s a hint - you’ll need to change the section of the code below.

LRFExpression yesExpression =
{
  .pattern = { LRFColor_White, LRFColor_Blue, LRFTransform_None, LRFDuration_DoubleLong },
  .sounds = {
    { LRFNote_C, LRFOctave_3, LRFIntonation_Rising, LRFDuration_DoubleLong, LRFDuration_Medium },
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK,
    LRF_SOUND_BLANK
  }
};

Challenge 3:

Add a new fate to your robot’s Magic 8 Ball trick. Let’s call it an Absolutely, Without a Doubt! Expression. This challenge is a bit tricky. You’ll need to create a new LRFExpression, adjust the fate = random value, and make sure to call it in your void checkHeadSensor case scenarios.

    if (headHeld == true) {
      //released
      fate = random(3);
      lrf.eyes.set(0, 0, 0);
      switch (fate) {
        case 0 :
          lrf.blinkAndSay(&noExpression);
          break;

        case 1 :
          lrf.blinkAndSay(&yesExpression);
          break;

        case 2 :
          lrf.blinkAndSay(&maybeExpression);
          break;
      }