Shaking Hands with Marty

As a fun and fairly simple Scratch project, I've been programming Marty to shake hands. This demonstrates a few concepts - we have to move joints and show expressions - but to do it properly we also need to know whether somebody has grabbed Marty's hand, and for that, we need to read sensors!

get ready, move right arm

With the arm and hand raised, we actually need to shake hands. That will mean moving the arm up and down multiple times. A repeat block will help us here!

get ready, move right arm

In my version, I've made Marty shake his hand 4 times, but that's up to you! Once the handshake is done, it's probably sensible to put the arm down again, 

get ready, move right arm

Getting Started - Handshake Movements

There will be two main bits to this - moving and sensing. First off, lets sort out the movement bit! The first bit of any handshake is to proffer a hand, so lets activate Marty and raise the right hand,

Adding Some Emotion

That's the basic arm movements, but wouldn't it be nice if Marty's eyebrows moved too? We can do that! 

get ready, move right arm
Feeling more adventurous? Why not make Marty's eyebrows move each time the hand moves up and down!

Getting Interactive

We've got the moves, but Marty really needs to be able to tell when to shake hands. To do that, we're going to use the motor current sensors to detect when there is some force on the arm,

Reading Motor Currents

Let's make a variable called right arm torque,

right arm torque

But, the motor current sensors give very small numbers - try pushing Marty's right arm a bit and double-clicking the piece of code we just added and see what value you get displayed on the stage section of the Scratch interface. Let's make that variable a bit bigger!

right arm torque

We're going to want to update this variable often and we don't want to have to code the calculation more than once, so let's define a function to update the sensor values for us,

right arm torque

Putting it Together

Ok then! Let's use our shiny new variables and update sensors function to make the handshake interactive!

interactive handshake
There are a few things to notice here:
  • We wait for a second after moving the arm to give the motors a chance to settle
  • We call update sensors before entering the loop to make sure that we're not using an old sensor value at the start
  • We use a repeat until block to loop until Marty detects an amount of force on the right arm. In this case we have set the threshold to be 3, but try varying it to see what happens!

Knowing When to Start

But how does Marty know when to start the handshake? That's up to you, but I've made mine so that touching either arm will make Marty offer to shake hands. To do that I made another variable to store the left arm torque value and added another block to the update sensors function we made earlier,

update sensors

To keep things tidy, I moved the actual handshake-y bit into its own function and made a seperate block of code to keep track of sensor readings and when to start a handshake!

handshake function

I've also added a cheeky wiggle at the end...

handshake function

Scratch Project File

To get the most from this, I'd recommend copying the blocks yourself, but if you'd rather have a Scratch project file, I've uploaded one here ;-)

Making it Bigger

You don't have to stop. there! I have an even better handshake with my Marty, it has high fives and sound and everything! It looks like this,

handshake function
And the Scratch file for it is here There's something a bit more advanced here too - I wanted to move both Marty's arms at the same time, so I've turned off blocking mode. You can read more about how that works in the Scratch docs So that's my Marty handshake!