Thursday, 27 March 2014

Getting physical with Scratch

Don’t you find it entertaining if an animated Scratch that you had created comes alive when the night falls, and stops when the sun rises ??   Or turns on the alarm when there is an intruder around the perimeter fence ??

A typical example is show below

In the above set up, Scratch constantly polls information from the micro controller and check if the motion sensor is triggered. If true, it turns on the siren (alarm). The siren continues to switch on to a logical “True” state until the spacebar of your keyboard is pressed.

It’s not as difficult as you thought, all you need is to snap a few Scratch blocks as follows

The combination of Arduino and Scratch Extension makes physical computing possible, that allows Scratch program to react and response to events happening outside of the computer. It is an engaging and practical topic that adds interactivity where children (even adults) enjoy learning about.

A Scratch Extension is an object that can be used with Scratch, in some cases as an add-on to interface with the outside world. There are many variants (or Scratch-like), specifically developed to teach children the concept of physical computing, each has its shortcomings and appropriateness for specific applications. Of the variants, Scratch for Arduino (S4A), developed by Citilab is a direct modification of Scratch 1.4 and is explicitly designed to work with Arduino UNO. However, the pre-assigned I/O pins in S4A hinders the flexibility of I/O configuration as you would in a micro controller. That doesn’t imply it is not up to snuff but relevance of its application.

The 18 I/Os are assigned as followss
  • 6 analog inputs (pins A0, A1, A2, A3, A4 and A5)
  •  2 digital inputs (pins 2 and 3)
  •  3 analog outputs (pins 5, 6 and 9)
  •  4 digital outputs (pins 10, 11, 12 and 13)
  •  2 digital outputs for continuous-rotation servo motor (pins 4 and 7)
  • 1 digital outputs for 0-180 rotation servo motor (pins 8)
S4A is an ideal program for beginners (at least in my context) but as you progressed, it might not be adequate for your applications where more I/Os are needed. 

Citilab has also developed an extension for SNAP! called Snap4Arduino, a derivative of Scratch by Jens Mönig at MioSoft Corporation, with contributions by some folks at Berkeley.
SNAP! allows you to build your own blocks, and with the added features of first class list, procedures, etc, it renders a serious introduction to computer science to high school or college students. 

Snap4Arduino is purportedly tailored for the Arduino variants such as the UNO, Mini, Mega2560, Pro, Lilypad, etc. Although it’s still in its alpha phase, it works flawlessly with the atmega328 and atmega168 processors, namely the UNO, Pro Mini, to name a few.  

For the Mega2560, all 54 IO pins are accessible, except there are some bugs with PWM, digital input and Servo functionality for higher pins (pin14 onwards). 
Unlike S4A, all the digital I/O pins are configurable, extending its flexibility.

As of this date, the bugs has not been fixed but the folks at Citilab has committed to sort it out prior to the upcoming Scratch@MIT conference, somewhere in August this year. 

You can download both S4A or Snap4Arduino at and respectively. Instruction to install the program is available at their site.

Throughout this blog, we’ll start with S4A for the very beginners and switch to Snap4Arduino for projects where more IOs are needed.

To be continued……….

Tuesday, 11 March 2014

More Variables

In my previous post, a simple program was shown how variables were applied to a moving Sprite using the “Up Arrow” and “Down Arrow” of the keyboard. In short, the value of a variable can generally be accessed or changed at any time by the programmer. However, such variable can only store one value at a time.

There are times when you need a variable to store multiple data in your script, Scratch’s “List” block provides that concept of arrays to help you with these circumstances.

The “List” block stores a series of values in a single variable. Each of these values is an 'element' of the “List” and each element has an index value to mark its position within the list. Suppose you would like to keep an inventory of the following grocery at home, 

  1. Mushroom
  2. Onion
  3. Carrot
  4.  Apple
  5. Orange
  6. Milk 

The “List” block will keep a record of the above elements, and is displayed by the "List Watcher" at the Stage Area :

Here, the order arrangement in the list corresponds to the sequence the data is entered and each of the data is indexed with a number accordingly.
For example, Mushroom is marked by an index value of “1”, Onion with an index value of “2” and so on. The index is referred to as “Item” in Scratch.

Note : I should have included the quantity for each element, but since the “List” block is a one dimensional array, I’ll leave it out for now until the next post where we will discuss about two dimensional arrays.

Start your Scratch programme now and click on the “Make a List” button (under “Variables” control block). A dialog box will appear as follows :

You are asked to give a name to the “List” you want to create, let’s name it “Grocery” and click the “”ok” button.

Once a list is created, an empty List Watcher will appear on the Stage Area, the control palette would look like below, 

There are 7 blocks available in the “List”, and each has its own function. Let’s explore each block before we create a simple script for the Grocery.

1) “add () to ()” block
Drag the block to the Script Area and click twice on the block. You have just entered two identical elements (called “thing”) in the “Grocery” list and the contents are shown in the "List Watcher"  below.

Replace the text with “Carrot” in the “add () to Grocery” block, click on it once and the list will be updated as shown below:

New entries always succeed the last item of the list.

2) “delete () of ()” block
I guess I don’t have to describe more, as your intuition should be able to tell you what the block does by its name. One thing you should know, the drop down menu displays only 3 items by default, regardless of its contents

There’s nothing to concern about, just key in the item number corresponding to the data you wish to delete.
The option “all” will remove all of the contents in the list.

3) “insert () at () of ()” block

This block is similar to the add (thing) to Grocery” block, except that the new element “inserted” to the list precedes the item you had choosen. Referring to the list we had created earlier, 

Let’s insert a new item “Onion” at item 2, and you will have a new list as below.

4) “replace item () of () with ()” block
This block changes (or replace) the specified item's content to a given entry.
Replacing Item 1 (currently "thing") with "Mushroom" yields the following

5) “item () of ()” block

This is a reporter block, reporting the content of its corresponding item in the list. It is commonly used in “Say” block or used for logical operation. Below is an example used in “Say” block

6) “length of ()“ block
This is a reporter block, which reports the total item a list contains. The total item is also displayed at the bottom of the List Watcher

 7) “() contains ()“ block

This is a “Boolean” block, which returns “True” if the text entered is equal to any of the elements in the list, otherwise it returns “False”. For example, if you need to check whether “Apple” is in your Grocery List, it will return “False” as the current list contains only Mushroom, Onion, thing and Carrot.
Example script is shown below :

Note that the text is case-sensitive. Mushroom is not equal to mushroom, as far as the block is concerned.

Let’s start with an example how the “List” block is applied. We will use the same inventory, and is shown below for convenience.

1)    Mushroom
2)    Onion
3)    Carrot
4)    Apple
5)    Orange
6)    Milk

We'll create a pre-defined inventory as follows :

Note 1 : Delete previous entries to avoid duplicates.
Note 2: Elements are written to the “List” block in sequence.
Note 3 : Used for linking to its corresponding elements.

Next, we’ll create a script to make the Sprite summarize (or "Say" out) your inventory :

Note 1 :

The Sprite does not know what’s in the grocery list. The “repeat until ()” block is needed to run through the whole list sequentially from the beginning to the last item. The variable “item” increments by 1 for every “repeat until ()” loop until it is greater than the “length of the grocery”, whereby the “repeat until ()” block exits its loop. Its process flow is shown below

Note 2 :

In the “if () else” block, it checks if “item” is greater than “length of Grocery”. If true, it does nothing and exits the block, else it “says” out the grocery name. The next event is taken over by the “repeat until ()” block, which repeats its process as explained in Note 1.

Note 3 :
The “say () for () secs” block allows only one specified text, and we need the Sprite to say out 4 different text, ie :

    1)  Name of the Grocery
    2)    “ located at “ text
    3)    “Item” text
    4)    Item number corresponding to the grocery

The  “join () ()” block comes to the rescue, where it concatenates as many text as you wish.
Following steps show how “join () ()” block links the text together

Step 1:

Step 2 :

Step 3 :

Step 4 (final)  :

The final "say ()" block will look like below :

Note that 3 different join () () ” blocks are used to concatenate the required text that we want.
The combination of “say () for () secs” and “join () () ” blocks results in the following

The whole script should look like below :

Below is the corresponding flow chart

Alternatively, you can create a script to make the Sprite ask for the groceries you would like to enter.Below is an example :

The above script allows you to enter as many data as you like until “x” is entered, telling the script you’re done with your entry.

The “ask () and wait” block stores your data to the “answer” block and pass it to the “if () else” block.
Note that there are two “if () else” blocks nested together within the “repeat until ()” block, the “else” statement of the first “if () else” block is itself an “if () else” block.
The first “if () else” block checks whether “x” is entered, if true, it exits the block. If false, it enters into the “else” statement. The second “if () else” block checks for “empty” data just in case if you had accidentally pressed the “Enter” key.

The flow chart below provides a better picture :

The new script is show below.

That's about it. Happy Scratching !!