Thursday 27 November 2014

Mapping two range of numbers


Math has never been my favourite subject during my school days and I’ve always asked why do we need to learn Math. It would have changed my mind and become more interesting if students have been taught how Math can be applied in the real world.

Take for example a simple linear equation y = mx + C, given two points and we can find an equation of the line.

So what’s the big deal with finding an equation of a line and what can it practically do in the real world ??

Here’s the answer.

Suppose you would like to plot a trigonometric function of “sine”, with an amplitude of 10,  over a horizontal range of 0 to 360 degrees, we would find the graph as tabled below


FIG 1

Note that the value on the X-axis is given the same value as the angle θ since we can assign the positive range of the X-Axis from 0 to 360. The Y-Axis represents the amplitude of the waveform.

However, plotting similar graph with Scratch is a different game altogether. In Scratch, the screen has a resolution of 480x360 pixels (FIG.2), such that the X-Axis range from -240 to 240 instead of 0 to 360 as seen from FIG.1 


FIG. 2 Scratch screen


How do you plot the points on the graph across the screen where the X-Axis range from -240 to 240 with a complete cycle of 0 to 360 deg ??? In other words, we need to translate the whole range of 0-360 degrees proportionately to a range of -240 to 240 on the X-Axis as shown below,


FIG.3

FIG.3 denotes that if the value of the angle is 0, it is translated to -240 on the X-Axis, 90 deg to -120, 180 deg to 0 etc.

The translation of one range of numbers to another is often called “mapping” or “scalling”.


Let’s revisit the linear equation,


Note that the x and y in eqn(1) are not the points on the X-Y coordinates of the screen. It’s just an expression of two range of numbers where y represents a set of number and x represents another set.


In our case, we would like to map the angle (0-360 deg) to the full range on the X-Axis (-240 to 240). Hence y, being the desired output, represents the values that lies on the X-Axis and x represents the value of the angles (0 to 360 deg) to be mapped.

Re-writing  eqn(1) with its min and max value, we have,


The min and max values of the two sets of numbers are :









Let’s solve for m and C.

Subtracting eqn(2) from 3, we have


Re-arranging eqn(4) yields



Replacing m from eqn(5) in eqn(3), we can now solve the constant C.


Replacing m and C in eqn(1) :



Re-arranging the above equation,


We had earlier defined that 


Let’s rename the above symbols to make our life easier, i.e,






Renaming y in eqn(7) to x (the desired points on the X-Axis) and replace with the new symbols, we have


We can now plot the graph as shown below.
















Below is the Scratch script to plot a “sine” function with and amplitude of 150.

A block is also created to map the above discussed two range of numbers.



























And the graph on the Stage Area is show below


I’ll cover more on the “mapping” subject in my next post, that will involve Arduino and external sensors. Happy Scratching………


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 http://s4a.cat/ and http://s4a.cat/snap/ 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 !!