Interfacing Scratch and Python

Hello to Jason Isaacs is the new ‘hello world’.

I decided it might be cool to knock up a simple Scratch interface for the MonkMakes Robot Rover – a bit like the block coding game I made for Year 1 but also controlling a real robot in the real world.

The GPIO commands needed to control the robot directly were going to be a bit heavy and involved, so I decided to see if I could use Scratch to interact with a Python program using the library supplied with the MonkMakes robot. Turns out it’s pretty easy to do this. I’ll write up the robot code later, but here’s a simple example of Scratch interacting with Python.

First I installed scratchpy with
sudo pip install scratchpy

I then opened Scratch and made a very simple program where Scratch cat waits for the message ‘jason’ and replies by saying ‘Hello’. Then I enabled remote sensor connections in Scratch by right-clicking on the () Sensor Value block, found in the sensing category. I then selected the “Enable remote sensor connections” option.

In Python/IDLE 2 I then ran a very simple program to ask your name and broadcast the answer to Scratch. If you replied ‘jason’ you are rewarded with a ‘hello’ back from Scratch cat.

import Scratch
s = scratch.Scratch()
name = raw_input("What is your name? ")

It’s also possible to respond in Python to messages from Scratch, which I’ll explain next time when we look at the Scratch project to drive a robot round the screen – and a real one round your floor.

This entry was posted in computers, Raspberry Pi and tagged , , . Bookmark the permalink.

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>