1

Program the Clapping Monkey Bot

The Clapping Monkey Bot is designed to play a game. Once the center IR sensor detects something in front of it, the robot will wait a random amount of time before it quickly claps its hand together. The object of the game: don’t get your hand caught!

This is the Clapping Monkey Bot’s pseudocode:

  • Wait for an object to be detected in front of the center IR sensor.
  • Wait for a random amount of time.
  • Quickly clap hands together!

First thing to do: include the Smart Inventor library at the top of your code, even before void setup(). Next, include your motors in void setup().

#include <SmartInventor.h>
 
void setup() {
  SmartInventor.DCMotorUse();
}

Underneath the Smart Inventor library and before void setup(), set a variable for the center sensor’s base value. Use that variable in void setup() so the center sensor will know if it’s seeing a value that isn’t normal. Add a buffer of 50 to leave some room for error.

In case you’re wondering what analogRead(20) is, that just means pin 20 on the Smart Inventor Board is being read!

#include <SmartInventor.h>
 
int baseLevelCenter = 0;
int handTime = 0;
 
void setup() {
  SmartInventor.DCMotorUse();
 
  baseLevelCenter = analogRead(20) - 50;
}

In void loop(), set a variable so the center sensor gets pin 20. Next, write an if statement so that if the center sensor’s value is below a certain amount, the motor moves a certain way.

#include <SmartInventor.h>
 
int baseLevelCenter = 0;
int handTime = 0;
 
void setup() {
  SmartInventor.DCMotorUse();
 
  baseLevelCenter = analogRead(20) - 50;
}
 
void loop() {
  int centerSensor = analogRead(20);
 
  if(centerSensor < baseLevelCenter)
  {
     //insert motor movement here
  }
}

The Clapping Monkey Bot isn’t as fun if you know that it will clap its hands at a certain time! To keep things interesting, you can add randomization to your program.

First, use random() to create a variable called minimumTime to set a minimum amount of time. Next, create a variable called handTime which will measure how many times in the loop your hand has been in front of the sensor, and it will also add one every time the sensor detects something in front of it. If the value in handTime goes over the range you set in minimumTime (ours is 255), handTime is set back to 0.

As the program is going through the loop, minimumTime is going to be set to a random number between 50 and 255 every time.

void loop() {
  int centerSensor = analogRead(20);
 
  int minimumTime = random(50, 255);
 
  if(centerSensor < baseLevelCenter)
  {
    handTime += 1;
 
    if(handTime > minimumTime)
    {
     handTime = 0;
    }
  }
}

You need to add the part where your motor actually moves! Inside the if statement forĀ if(handTime > minimumTime), have your motor move clockwise to close the bot’s hands, counter-clockwise to close the bot’s hand, and then stop.

void loop() {
  int centerSensor = analogRead(20);
 
  int minimumTime = random(50, 255);
 
  if(centerSensor < baseLevelCenter)
  {
    handTime += 1;
 
    if(handTime > minimumTime)
    {
      handTime = 0;
 
      SmartInventor.DCMotor(M1, CW, 100);
      delay(1000);
 
      SmartInventor.DCMotor(M1, CCW, 100);
      delay(2000);
      SmartInventor.DCMotor(M1, LOOSE, 0);
    }
  }
}

Finally (and this is completely optional), you can add buzzers before and after your bot claps its hands.

void loop() {
  // check sensors
  int centerSensor = analogRead(20);
 
  int minimumTime = random(50, 255);
 
  if(centerSensor < baseLevelCenter) 
    { 
        handTime += 1; 
            if(handTime > minimumTime)
                {
                  handTime = 0;
 
                  SmartInventor.Buzz(1160, 8);
 
                  SmartInventor.DCMotor(M1, CW, 100);
                  delay(1000);
 
                  SmartInventor.DCMotor(M1, CCW, 100);
                  delay(2000);
                  SmartInventor.DCMotor(M1, LOOSE, 0);
 
                  SmartInventor.Buzz(1160, 8);
                  SmartInventor.Buzz(740, 8);
                }
  }
 
}

Your final code should look something like this:

#include <SmartInventor.h> 

int baseLevelCenter = 0; 
int handTime = 0;

void setup() {  
    SmartInventor.DCMotorUse();
   
    baseLevelCenter = analogRead(20) - 50;
}

void loop() {  
    // check sensors  
    int centerSensor = analogRead(20);   

    int minimumTime = random(50, 255);   

    if(centerSensor < baseLevelCenter) {         
        
        handTime += 1;             

        if(handTime > minimumTime)                
            {                  
               handTime = 0;                   

               SmartInventor.Buzz(1160, 8);                   

               SmartInventor.DCMotor(M1, CW, 100);                  
               delay(1000);                   

               SmartInventor.DCMotor(M1, CCW, 100);                  
               delay(2000);                  

               SmartInventor.DCMotor(M1, LOOSE, 0);                   

               SmartInventor.Buzz(1160, 8);                  
               SmartInventor.Buzz(740, 8);                
            }  
        } 
    }

Still not sure about the Clapping Monkey Bot or need to check your program? Watch the video!