Friday, July 31, 2015

An Introduction to Arduino & S4A (Scratch for Arduino)

Arduino is a very popular hardware platform with makers & hobbyists. The microcontroller board provides sensors and actuators that allow for interaction with the physical world. The official website for Arduino (arduino.cc) defines it as:
Arduino is an open-source prototyping platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. All this is defined by a set of instructions programmed through the Arduino Software (IDE)
A more detailed explanation of "What is Arduino?" can be found on the Arduino Intro page.

Personally, I find Arduino to be a very versatile platform, that provides the opportunity for children to unleash their creativity. It would also allow for the development of interdisciplinary projects. While designing projects for children, I am always on the lookout for platforms that allow for the development of problem solving skills, logical analysis and thinking-outside-the-box, without getting trapped in the intricacies of the platform itself. I was thrilled by the opportunities that Arduino could provide, but was really skeptical about using its IDE for younger students.

A program or code written for Arduino is called a "sketch". Arduino programs are usually written in C or C++.  However, these languages are too complex for the younger kids to master and use. Hence, I did my bit of research and came across S4A - Scratch for Arduino. Developed by Citilab, a group in Spain, it is an improvised version of Scratch from MIT, with instructions that can be used to control the Arduino hardware. Here was the perfect solution to my dilemma: My students are already familiar with Scratch and absolutely love working with it; all they need to do is familiarize themseves with the extra set of instructions for the hardware. The instructions, written in plain English, are simple enough for the kids to understand and are very intuitive to use. It would open up the Arduino platform for them, without getting tangled in the intricacies of the syntax and grammar of a formal programming language.

The S4A site provides instructions on the download and installation; and frankly, it's quite a simple process. The main difference from the online Scratch is that you need to download and install S4A on your machine. And you are not allowed to share the S4A projects on the Scratch community website. The Arduino board is represented as a Sprite on this version of Scratch, and you have access to blocks that can perform digital/analog reads and writes, as well as blocks that can control motors. S4A is compatible with a few versions of Arduino, but I'll be primarily working with the Arduino Uno. Unless specifically mentioned, all references to Arduino in this blog will default to the Uno.

In the upcoming weeks, I'll be posting a few projects for Arduino, using S4A as the IDE.

Monday, July 27, 2015

Doodle Pencil - Using the Pen & Mouse Pointer Coordinates in Scratch

Here's a fun little Doodler made with Scratch:  The Doodle Pencil
















This was rather a spur-of-the-moment project... inspired by Etch-a-Sketch...  and kind of a follow-up on the Pac-Man game...

The code is minimal, and I feel not much of an explanation is required. The instructions are mainly from the Pen area of the Scratch Instruction Set, along with the instruction to follow the mouse-pointer. The "green flag click" handler does the initialization - clearing the screen, setting the sprite size and setting up the pen size & color.

The code is interactive, and it's designed to enable the user to lift and lower the pen as required while doodling: Move the mouse pointer to the desired location on screen and then click on the up arrow key; the pen will start drawing now by following the movements of your mouse pointer. Click on the down arrow key to stop drawing at any point.

This could be a fun little exercise for the kids to see how different combinations of the "Pen" and "Motion" instructions interact with each other.

Enjoy doodling!

Saturday, July 25, 2015

A Simple Math Game in Scratch - Introducing Conditionals & Comparators

Video games are ever popular among kids. And there are plenty of Math tutorials out there that use the format of video games to entice kids to practice their arithmetic. My child definitely enjoys working with these, and I am sure yours do as well. So, how about getting the kids to design and program their own little Math video game?

The students shall design the Math questions (and figure out the answers) to use in the game. They shall use comparators to decide if the answers entered by the user are correct or not. Variables shall be used to keep track of the number of right and wrong answers. This could potentially be a project for grade 4 and above, since variables are introduced as part of grade 4 Math.

Aim:  


Design a simple, interactive Math Video Game, with a pre-decided set of questions. The user gets to enter the answers, and the game should tell the user if the answers are right or wrong. The game should also keep track of the number of right and wrong answers, and display the scores at the end of the game.


Programming Concepts Introduced:

  1. Conditionals    (If - Then - Else)
  2. Comparators   (Equality testing)
  3. Variables        (To keep track of the game scores)

Implementation:


Here's the Scratch project that my child and I made:  A Simple Math Game.























The Design Process:


The exercise is intended to introduce the students to Conditionals and Comparators.

To make the game, start off by writing up a few Math questions and their answers. Have this list ready with you. Next, choose a fun sprite and a background, to make the game look interesting. Now, build up the program step-by-step.

Look at the requirements for the assignment. The game asks questions to the user and the user types in answers. The sprite could ask the questions. Since we need to keep track of the user input, the best option is to use the "ask and wait" instruction in the "Sensing" area of the Scratch Instruction Set.

Once the user types in an answer, it gets stored in the "answer" variable, again available in the "Sensing" area of the Scratch Instruction Set. This is the value that we need to compare against the pre-set correct answers from our list.


Introduce Comparators:  Equality operator

Lead the discussion with the question "How can we find out if the user entered the right answer?"

In the "Operators" area of the Scratch Instruction set, you will find 3 different comparators - less than, equal to & greater than. Discuss with the kids as to which one of these would be the best choice for checking if the user entered the right answer (we shall use the "equal to" operator in this case to compare the user input to the right answer).

For example, if the right answer is 10, we shall check the user input using the following comparator:





Introduce Conditionals:  If-Then-Else 

The next step is to decide what to do if the answer is right or wrong. Here's where the If-Then-Else statement comes into play. If-Then-Else can be found in the "Control" section of the Scratch Instruction Set. You could start by giving the students a few simple examples:
1.  If we behave well, then the teacher will give us extra recess time;
     else we shall have a shorter recess.
2.  If it rains, I shall bring my umbrella to school;
     else I shall leave my umbrella at home.

The simplest step could be the sprite announcing that the answer is right or wrong:
  • Every time the user's answer is correct, say "Right Answer" & 
  • Every time the answer is wrong, say "Wrong Answer". 









Use of Variables:

The final requirement for the game is to keep track of the number of right and wrong answers. We need two variables for this purpose. The variables can be created under the "Data" section:  Click on "Make a Variable", and choose a (meaningful) name for the variable. Make two variables, say "Points" & "Wrong Answers", as in the sample project. Make sure to display both variables on screen, using the "Show Variable" instruction.

Initialize both variables to zero at the start of the game. Discuss with the students why we need to do so. Now, add the following steps to the above If-Then-Else statement:

  • Every time the user's answer is correct, increase the variable "Points" & 
  • Every time the answer is wrong, increase the variable "Wrong Answers". 















We are almost done with the design at this point. To start and stop the game, use an event, such as "When green flag clicked" or a key press, etc., found under the "Events" section. You can add in as many questions as you like to make the game interesting. Make sure to test the code every few steps, as you build up the program.

Note: 

If you look at the sample project that my child and I made, you would see that we grouped the questions into different blocks/function. This keeps the code clean and provides for easier maintenance and testing, but it's definitely not a requirement for creating the game.


Have fun creating your own Math Video Game!!

Friday, July 10, 2015

CopyCat - A Simple Intro to User Input and Variables via Scratch

My child and I worked on CopyCat as a simple introduction to variables and user input in Scratch. Algebra is part of the Grade 4 Math curriculum in the USA, and this project could be a fun way to introduce the use of variables.


Aim:  

Design an interactive game in Scratch, where a CopyCat copies/repeats everything that you type in.

The Design Process:


  • Only a single sprite is required: the CopyCat. You can either choose from the list of sprites already available on Scratch, or draw your own. 

  • To provide user interaction in starting and stopping the game, we used the "green flag click" to start and the "space key click" to stop the game (both of which can be found under the section "Events" in the Scripts area in Scratch). You can choose any of the options that are available in "Events" to do the same.


Various sections in the Scripts area of Scratch

And now the fun part: CopyCat needs to copy everything that you type in.
How can we achieve this?

  • Under "Sensing" in the Scripts area in Scratch, you will find a block that asks for user input and waits for it. This is what we shall use, to ask the user to type in anything they like.


  • Once the user input is received, CopyCat needs to repeat it. But, how can CopyCat remember what the user typed in? Here is where the concept of variables comes into play. In the section "Sensing", you will find the variable "answer", which stores whatever the user typed in. 

  • I recommend selecting the box right next to "answer", so that it is visible on the screen and the kids can see how its value varies (hence the name variable), depending on the user input.


  • The CopyCat can now use this variable along with the "say" instruction (found in the "Looks" area of Scripts in Scratch), to repeat/copy whatever the user types in.



Ask the students to try writing the code upto this point:

  1. When "green flag clicked" (or other event), CopyCat asks the user to type in something.
  2. CopyCat repeats the user input, via the variable "answer". 
  3. When "space key clicked" (or other event), stop the program.



Let the students experiment with different values for the user input and observe how the variable changes accordingly. Once comfortable with the use of the variable, they can hide it by deselecting the box next to "answer".  It would be good to remind the students at this point, that this feature is helpful for debugging.

Here are three screen shots to demonstrate how the user input gets stored in the variable "answer".

  Asking for user input; variable is empty

User input entered; variable is empty till Return key is pressed

User input is now stored in the variable