Marty Scratch Fun - Shaking Hands!

Marty Scratch - Shaking Hands

As a fun and fairly simple Scratch project, I've been programming Marty to shake hands!

This demonstrates few concepts - we have to move joints, 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 the sensors!

It'll look a bit like this:

handshake video

Getting started - handshake movements

There will be two main bits to this - moving and sensing. First off, let's 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:

get ready, move right arm

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

get ready, move right arm

In my version, I've made Marty shake its hand 4 times, but thats up to you!

Once the handshake is done, it's probably sensible to put the arm down again

get ready, move right arm

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 movements, but really Marty 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 current

Let's make a variable called right arm torque

right arm torque

And to start we can set it to the right arm motor current

right arm torque

But, the motor current sensors give very small numbers - try pushing Marty's right arm a bit and double clicking the set right arm torque block above. You should see something like this: get ready, move right arm

Let's make that variable a bit bigger:

bigger variable

Now when we set the right arm torque variable, it'll be much 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:

update sensors

Putting it together

Ok then! Let's use our shiny new variable and update_sensors function to make the handshake interactive!

interactive handshake

There a few things to notice here:

Knowing when to start

But how should Marty know when to start a 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:

left arm torque variable

And I added another block to the update_sensors function we made earlier

update 2 sensors

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

handshake function

initiating a handshake

I've also added a cheeky wiggle at the end

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:

Marty secret handshake

The Scratch for it looks like this:

complicated handshake

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!


Leave a comment and join the discussion here

Please Log In or Sign Up to write a comment response

0 Responses
