Monday, November 30, 2015

TAV (Terrestrial Aquatic Vehicle)

This is a long overdue post for a fun little science project...  My child came up with the idea for TAV (Terrestrial Aquatic Vehicle) and designed it a few months ago, for a class project in Grade 3. Interestingly, he designed it not for a Science project, but an Economics one! His classroom had a project called "Market Place", where students are supposed to come up with ideas for little stores and businesses: make & sell products, earn (fake) money and then use the money for their expenses including living costs, education, retirement savings, etc. A simulation of real-life economics...

The kid decided to create a Tech company and recruited a couple of his classmates for the project. He came up with the idea for "TAAV - Terrestrial Aquatic Aerial Vehicle".

Saturday, November 14, 2015

S4A (Scratch for Arduino) Project - A Math Game using LEDs: Version 2, with a Single Sprite

Here is a much simpler version of the previous Math Game using LEDs: The aim is to design an interactive Math video game, where you ask questions and if your answer is right, an LED lights up. In the previous version, you had two sprites that communicate with each other, checking the values of variables. In this much simpler version, there is just a single sprite - the Arduino sprite itself, that asks questions and checks the answers for correctness. This version takes away any complication involved in maintaining a communication channel between the sprites, and is probably the easiest way to start off. The following is a formal lesson plan for the same; a set of slides are also provided. The assignment is intended for students in Grade 5 and higher.

Aim

A gentle introduction to the Arduino hardware, with S4A (Scratch for Arduino) as the IDE.

Objective

Introduce the students to the Arduino platform, via a simple project to light up an LED. The students use their knowledge of reactive programming & conditionals

Saturday, October 24, 2015

A Formal Lesson Plan for the Scratch Food Chain

Here is a formal lesson plan that I designed for our Grade 5 students, to create an animated representation of a food chain. I have previously published a Scratch project for a food chain: A Food Chain in the Northern Temperate ForestThe lesson plan below is intended to help the instructor introduce the project in the classroom step by step, so that the students can learn the logic involved and gain familiarity with the Scratch instruction set. Food chains are part of the Grade 5 Science Standards, and this project could easily evolve into a presentation by the students at the end of the session.

Monday, October 19, 2015

A Pong Game - programmed with Scratch

Different versions of the ever popular Pong game are available on Scratch. Here’s the link from ScratchEd: http://scratched.gse.harvard.edu/sites/default/files/scratch-lesson-7-the-pong-game.pdf. The following lesson plan using Scratch is one that I created for our Grade 5 students, taking inspiration from this sample code for Pong.
Given the code for an interactive Pong video game, the students edit the program to create multiple versions of the game with varying difficulty levels. The aim is to gain familiarity with the Scratch software platform and learn how to create games/ programs that involve user interaction. The students learn to modify the various facets of the code, and in the process learn about reactive programming, conditionals, loops, sprites, backgrounds and variables. They discuss how the variables in the given program can be used to model the different factors (forces, energy, orientation, type of materials, etc.) involved in the movements of a bouncing ball.

Platform

We use the S4A (Scratch for Arduino) platform for the Grade 5 students in our school. This project is intended to provide an introduction to this software platform before the kids start using it as the IDE for Arduino. S4A is based on Scratch from MIT and is quite similar to it. Note that in this particular assignment, we do not use the Arduino.
The sample code for Pong can be found in the Examples/Games folder in S4A.

Computer Science Concepts

Reactive programing
Forever loops
Conditionals

Common Core Standards

  • Variables
  • Angular measurements
  • Coordinate geometry for a 2D plane
  • Forces & interactions involved in a bouncing ball

The Lesson Plan

The aim is to learn about the basics of the Scratch/S4A platform in this assignment:

  • open, edit, save & close a project
  • sprites & backgrounds
  • the Scratch instruction set

The students read the given code and understand the various programming components that are involved in the design of the game. The factors that affect the movement of the pong ball are included as variables in the code. Students learn how modifying them can change the way the game is designed. The difficulty level of the game can be altered by modifying the variables. A discussion on how the variables in the program represent the different factors (forces, energy, orientation, type of materials, etc.) that govern the movement of the pong ball can round up the class.

This assignment also provides a first hand look at the use of conditionals and forever loops. Reactive programming, where the sprites react to various key presses or mouse movement is also learned.

Slides

I put together a set of slides for this lesson as I felt that it might be easier for the kids to tag along with the lecture, by programming on their computers. They can store the different versions of their program under different file names. 

The Pong Game PPT



Programming Assignment

Modify the code for the sprites to create different versions of the game, as suggested below.

Paddle Sprite:

The code for the Paddle Sprite in the sample code is designed to follow the mouse/cursor horizontally.

  1. Can you modify the code for the Paddle Sprite so that it follows the mouse pointer vertically on the screen?
  2. Modify the code for the Paddle Sprite so that it follows the mouse pointer everywhere on the screen.

Ball Sprite:

Try experimenting with different values for the variables. Note the changes you see each time.

  1. Use the “pen down” instruction to track the path of the ball.
  2. Would the game ever take off if the starting position has the ball touching the red zone on the background?
  3. Guess why the “forever loop” is used in the game.
  4. Guess why the “180 - direction” is used to set the direction of the ball when it hits the paddle.
  5. Guess what the “turn random angle” does. Experiment with it to increase the difficulty level of the game.
  6. Can you make the ball move faster at the start of the game by changing one of the variables?
  7. Can you make the ball move faster when it hits the paddle by changing one of the variables?
  8. Increase the difficulty level of the game by increasing the speed & randomness of the ball.

Challenge

  1. Introduce a variable to keep track of scores - you start with zero points & every time the paddle hits the ball, you get a point.
  2. Can you program the paddle to be moved on the screen using the arrow keys on the keyboard, instead of the cursor?

Summary

Summarize the lesson by going over what some of the variables in the game represent. Here are a few examples:

  1. When you change the speed of the ball, what does it imply about the loss/transfer of energy?
  2. If the ball moves faster after hitting the paddle, what kind of material could the paddle be made of? Is it one that absorbs much of the ball’s energy?
  3. The red background in the sample code makes the ball come to rest completely. Discuss the energy transfer of the ball in this case. What materials could possibly have made the pong ball come to rest on coming into contact with it?

Here’s a link with more info about the science of a pong ball, if you would like to go further with the discussions.

Monday, September 28, 2015

Draw Polygons & Compound Figures with a Given Area using Pro-Bot

Can you program the Pro-Bot to draw polygons & compound figures with a given area?

This, again, is an assignment that I designed for our Grade 3 students. It relates to the Common Core Math Standards: Geometric measurement: understand the concepts of areaFor this exercise, I highly recommend using graph paper, as it provides a helpful medium for the kids to work out the math problems. Provide at least one sheet per child to work out the problem and then additional sheets as required for the groups to draw the figures using Pro-Bot. Here is a link to a graph paper with 1 cm grid in PDF format; you can make copies for the students to draw on using the Pro-Bot.


Area of a Figure


The area of a figure is the number of squares required to cover it completely, and is specified in square units. Here's an article from math.com that gives a quick overview of the topic.

How do you calculate the area of a given figure? You add the number of squares needed to cover the entire figure. Say you are given a square with sides 3 cm each. You need 9 squares of sides 1 cm x 1 cm to cover it completely. The area of the square is 3 x 3 = 9 sq.cm. Similarly, a 5 cm x 6 cm rectangle has an area of 30 sq.cm.

Can we do the reverse too? Given the area, can we come up with the design for a figure with that area? 

Let's look at an example. Given an area of 9 sq.cm, how many polygons can we draw? We can draw multiple polygons, all with the exact same area of 9 sq.cm. In the figure below, you can see:
  • a square 3 cm x 3 cm,
  • couple of polygons with an area of 9 sq.cm.

Can you think of more polygons with an area of 9 sq.cm?

Let's now look at a scenario that shows the practical application of the concept of area. And then program the Pro-Bot to draw a few polygons with a given area.

Programming Assignment

You work for an architectural firm, and have been asked to design a single story house with a floor area of 100 square meters (roughly 1076 sq.ft.) You are to draw and present various designs for the floor plan.

  1. How many different ways can you draw the floor plan with an area of 100 sq.m.? Provide at least 2 to 3 different designs and make a rough drawing of the figures that you come up with.
  1. Classify the figures that you came up with into the various classes of polygons based on the number of sides.
  1. Program the Pro-Bot to draw them on graph paper. Use 1 sq.cm. to represent 1 sq.m. in your figures.
  1. Assume that the plot of land available for the construction, is a rectangle that is 15 m long and 8 m wide. Can you provide a design(s) to build a 100 sq.m. building in this plot? Program your Pro-Bot to draw the design. 
  2. You can program the Pro-Bot to draw other figures with the 100 sq.m. area. Or explore other figures with different areas.

Draw Polygons with a Given Perimeter using Pro-Bot

Can you program the Pro-Bot to draw polygons & compound figures with a given perimeter?

This is an assignment that I designed for our Grade 3 students. It relates to the Common Core Math Standards: Geometric measurement: recognize perimeterFor this exercise, I highly recommend using graph paper, as it provides a helpful medium for the kids to work out the math problems. Provide at least one sheet per child to work out the problem and then additional sheets as required for the groups to draw the figures using Pro-Bot. Here is a link to a graph paper with 1 cm grid in PDF format; you can make copies for the students to draw on using the Pro-Bot.


Perimeter of a Figure


A perimeter is a path that surrounds a two-dimensional shape. The term may be used for either the path or its length. It can be thought of as the length of the outline of a shape. (Wiki)

How do you calculate the perimeter of a given figure? You add the length of all the sides of that figure that form its outline. Say you are given a square with sides 3 cm each. The perimeter of the square is 3 + 3 + 3 + 3 = 12 cm. Similarly, a 5 cm x 6 cm rectangle. has a perimeter of 5 + 6 + 5 + 6 = 22 cm. 

Can we do the reverse too? Given the perimeter, can we come up with the design for a figure with that perimeter? 

Let's look at an example. Given a perimeter of 12 cm, how many polygons can we draw? We can draw multiple polygons, all with the exact same perimeter of 12 cm. In the figure below, you can see:
  • a square 3 cm x 3 cm,
  • a rectangle 5 cm x 1 cm,
  • a rectangle 4 cm x 2 cm,
  • a hexagon with sides 3 cm, 1 cm, 1 cm, 1 cm, 4 cm, 2 cm

Can you think of more polygons with a perimeter of 12 cm?

Let's now look at a scenario that shows the practical application of the concept of perimeters. And then program the Pro-Bot to draw a few polygons with a given perimeter.

Programming Assignment

Old McDonald lives on a farm and has lots of animals. He would like to build a new set of fences to keep his cows safe.

  1. If Old McDonald has 36 meters of fencing available, how many different ways can he build an enclosed area for his cows? Make a rough drawing of the figures that you come up with and then program the Pro-Bot to draw them on graph paper. Use 1 cm to represent 1 m in your figures.
  2. Classify the figures that you came up with into the various classes of polygons based on the number of sides.
  3. Write programs for Pro-Bot to draw at least 3 of the figures that you came up with.
  4. Suppose Old McDonald has only 35 meters of fencing available, but wants to build a square or rectangular fence using all of that fencing material. Would it be possible for him to build it? Why or why not? 
  5. You can program the Pro-Bot to draw other figures with the 36 cm perimeter. Or explore other figures with different perimeters.


Saturday, August 29, 2015

S4A (Scratch for Arduino) Project - A Math Game using LEDs: Version 1, with 2 Sprites

Here is a Math game that uses LEDs to denote if answers in a Math Game are right or wrong. It's a follow-up on my previous Scratch project, A Simple Math Game in Scratch, with the addition of the Arduino hardware to Scratch.

We shall use the S4A development environment, which is quite similar to Scratch from MIT, except for the addition of a few instructions for interacting with the hardware. The Arduino board itself is represented as a Sprite in S4A, making it easy to use hardware specific instructions. We shall write the code for the Math game first and then modify it to include LED blinks to represent right and wrong answers.

Hardware:


  • Arduino board
  • USB cable to connect the Arduino to your computer
  • Breadboard
  • 2 LEDs of different colors (I used green & red)
  • Jumper wires
  • Resistors

Computer Science Concepts newly introduced:


  • Using variables for communication among Sprites

The Design Process:


Let's start with a simplified version of the previous Math Game. Just as in my original version of the game, we need a Sprite to ask the Math questions. I have chosen the exact same Sprite (a starfish) and background (a seabed) in S4A to keep it as close to the original as possible. The design for the first Sprite - the starfish - follows the same process as in the previous version; the details can be found here.

The other Sprite we need is, of course, the Arduino board, to make the LEDs blink. While writing the program, try to confine all the hardware related stuff to the Arduino Sprite, and let the other Sprite (the starfish) handle the process of asking the questions & checking for their correctness. The Arduino Sprite shall blink the LEDs, similar to our previous Blink project. Keep in mind that we shall be using a couple of LEDs here, hence you need to modify the code a bit. The wiring shall also have a couple more components; more on that later...

The starfish shall ask the pre-decided questions, check the answers and keep track of the game scores. Just as in the original version, we shall use two variables - CorrectAnswers & WrongAnswers - for the game scores. I prefer to leave them visible on the screen to let the students see how the variables change during the course of the game. While creating variables, always try to provide meaningful names, it's useful for reading and comprehending your code later on.


Now, the big question:  how will the Arduino know when to blink the LEDs?


For this, we need to link the two Sprites together. Everytime the starfish Sprite finds out that an answer is correct, it needs to let the Arduino board know... One way of doing this is to set a flag & let the Arduino constantly check it... Here's what we shall do:  Create a new variable, say "right", and initialize it to 0. Make sure that when you create this variable that you choose the option "For all Sprites". This is to make sure that this variable is visible to the Arduino Sprite (in tech jargon, using a global vs a local...)


The starfish shall set the value of "right" to 1, everytime an answer is correct. Now, all our Arduino needs to do is check the value of right and blink the corresponding LED, say green...
 "if right is equal to 1, blink the green LED".


We can easily extend this principle to include another LED in our game - one that blinks if an answer is incorrect. Create another variable, say "wrong" along the same lines as "right". The starfish shall set the value of "wrong" to 1, everytime an answer is incorrect. The Arduino shall check the value of wrong as well and blink the corresponding LED:
"if wrong is equal to 1, blink the red LED".

Now our game looks quite interesting... Just like in a real quiz show on TV, we have different LEDs that light up depending on the correctness of the answer...


Here's the code on the Arduino Sprite side...


We have a forever loop where the Arduino is constantly checking the values of the variables "right" and "wrong". You might remember that we used Pin 13 in our previous Blink project to connect the LED. We shall use Pin 12 and Pin 13 here, since we have two LEDs.

You can see that after lighting up an LED, we wait a couple of seconds and then switch it off, similar to the Blink project. But, you also notice that we set the flag/variable "right" (or "wrong", as the case may be) back to 0, each time. This is to ensure that we set and reset the flag for every answer in our Math game.

What happens if we do not reset the flag? The value of "right" will always stay at 1, after the first time an answer is correct. So your green LED will keep on blinking. Similarly for the value of "wrong"... This results in incorrect hardware behavior -- you would not get the results that you wanted or expected... Hence, it's important that you reset the values of the flags. Again, I recommend that you leave the variables visible on the screen so that the students can see how the values change over the course of the program.


Wiring:


The wiring involved is pretty simple. Two LEDs, green and red, connected via resistors to Pins 12 and 13. I have used the green LED connected to Pin 13 to represent the correct answers, and the red LED connected to Pin 12 to represent incorrect answers. As mentioned in our previous Blink project, the resistors are used to ensure that the LEDs do not get burned out by high currents from the board. Both the LEDs have their positive (longer) legs connected to Pins 12 and 13. Their negative (shorter) legs are connected to the GND (Ground) on the Arduino via a jumper wire.































Putting it all together:


Here are the screen shots of the (simplified) Math Game...

Code for the Starfish Sprite: I start by initializing all the variables to be used, followed by a short conversational intro to the game. Next come the questions in our Math quiz: ask the question, wait for the answer, check its correctness and then set the variables accordingly. Notice that we "change" or increment the CorrectAnswers/ WrongAnswers, while we "set" our flag variables right/ wrong to 1.






































The code for the Arduino Sprite... I choose to hide this sprite (via the "hide" instruction) for aesthetic purposes; you probably do not want the board to show up on the screen along with the starfish... The "wait" instruction is purely optional; it corresponds to the time taken by the starfish's conversational intro... 
























And a snapshot of the screen with the variables...






































Testing:


Time to test the code and see if our Math Game works...  Test with as many combinations of the answers as possible: 4 combinations of answers in the above case (Right-Right, Right-Wrong, Wrong-Right, Wrong-Wrong).

Now, click on the green flag at the top of the screen, play the game and watch the LEDs blink!


Saturday, August 22, 2015

Blink 2 - An S4A (Scratch for Arduino) Project using a Breadboard & an LED

This is version 2 of Blink, where we shall see how to use a solderless breadboard to connect an LED to the Arduino. Learning to use breadboards is very fundamental to building circuits. The S4A (Scratch for Arduino) code we shall use here is exactly the same as in the previous Blink project. And the exact same blink functionality for the LED will be developed, but using a different wiring this time involving a breadboard.

Why use a breadboard? A breadboard is a quick, easy way to prototype a circuit. It allows you to experiment with electronic circuits, build and test them without any soldering. It can be used to build the simplest of circuits to very complex ones. You can read more about breadboards here and here.

Back to Blink2... We shall connect the LED to Pin 13, just as we did in Blink version 1. The S4A code for blinking the LED turns it on and off, with an interval of 1 second, to provide the blinking effect.















Here's the wiring we shall use for Blink2 with the above S4A code:





























As you can see from the image, the LED is placed on the breadboard this time, rather than connected directly to the board as in Blink version 1. We use a resistor to connect the positive leg to Pin 13. The negative leg is connected to the GND (Ground) via a jumper wire. (Both jumper wires and resistors come as part of the Arduino kit. You can also buy them in bulk at stores such as Fry's, RadioShack, Amazon, etc.)

Here, we take advantage of the fact that all the components in a row on either side of the ravine on a breadboard are electrically connected. We connect one leg of the resistor to the same row as the positive leg of the LED, and connect the other leg of the resistor to Pin 13. The resistor is used to limit the electric current flowing in to the LED, and to ensure that the LED does not burn out due to direct power from the Arduino. The jumper wire from the Arduino GND (Ground) is connected to the same row as the negative leg of the LED, to complete the circuit. The electric current will flow in to the LED from Pin 13 through the resistor and flow out to the GND (Ground), thus completing the circuit.

Now, click on the green flag on the S4A screen and watch the LED blink...























Tuesday, August 11, 2015

Blink - Introductory S4A (Scratch for Arduino) Project

Blink is the first project that we shall try on the Arduino board. This is the S4A (Scratch for Arduino) version of the original Blink tutorial on arduino.cc. The aim is to familiarize the students with the board and using the S4A to interact with it. (Detailed setup instructions for S4A can be found on s4a.cat.)

In S4A (Scratch for Arduino), the Arduino board is treated as a Sprite, and appears on the screen as such. You write code for the Arduino, just as you would for any normal Sprite. If you are using multiple boards, you can open up multiple Arduino Sprites.

While working with a new hardware board, 90% of the time, the first step is trying to light up an LED on the board. Not only does it involve the simplest of wirings, but it also provides a visual indication that the connections are fine and that you can communicate with the board. And keeping with the tradition, that's where we shall start too: light an LED connected to the Arduino.


Aim:  

Connect an LED to the Arduino and try to blink it.

 

Materials required:


  • Arduino board
  • 1 LED
  • 1 Resistor, anything between 220 ohm to 1 K ohm
  • USB cable connecting the computer to the Arduino

Wiring:


The wiring involved is really simple, and is the same as the Blink example on arduino.cc. As shown below, connect the positive leg (the long leg) of the LED to Digital Pin 13 via the resistor, and the negative leg (the short leg) to the GND (Ground) to complete the circuit. Now connect the Arduino to your computer via the USB cable.




The LED does not require much power to light up, and the Arduino board itself can power it. You do not really need a separate power source such as a battery, for this project. The resistor is used to limit the electric current flowing in to the LED and to ensure that the LED does not burn out due to the direct power from the Arduino. The electricity will flow in to the LED from Digital Pin 13 through the resistor and flow out to the GND (Ground), thus completing the circuit.

The Program on S4A:


The Blink code sets the LED on and off. Here is the code snippet on S4A.



We use an event - "Green Flag Click" - to start the program, just as in the regular version of Scratch. Next, in a "forever loop", we include the instructions to turn the LED on and off, by sending a signal to Digital Pin 13, and then switching it off.

The "digital <NUMBER> on/off" instructions can be found within the "Motion" section of the S4A Instruction Set. The "forever loop", "green flag click" and "wait" instructions can be found in the "Control" section of S4A.

As you can see in the code above, we let the electricity pass to the LED and then switch it off, with a delay of 1 second in between. Why the "wait 1 second" between the instructions? Well, if there is no delay in between, the human eye cannot detect the blinking of the light.

Test:


Now, try clicking the green flag on the screen and watch the LED blink.



You have successfully written your first S4A code, and tested out your Arduino!

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

Tuesday, June 16, 2015

Pac-Man is Chasing my Planet!

My child's class was recently introduced to Cartesian coordinates in Math. And in our coding class, we have been practicing interactive programming for the last few weeks. So, I thought of putting together a very simple template that  combines both the concepts, that the kids could then remix...  Pac-Man is Chasing my Planet! was the result... took me less than 10 minutes to put together and the kids loved it.

We went through the Pac-Man template code as a group & discussed the use of the XY coordinates. I showed the kids how the XY coordinates displayed under the Scratch animations area change, as I move the cursor around on the screen. Our discussion then proceeded along the following lines:


  • If I wanted my sprite (the planet, in this case) to move anywhere the cursor moves, what values should I use for the sprite's X and Y coordinates? 
  • The above point was also a good place to talk about variables, and how the change of the cursor position is always reflected in the planet's position. 
  • Should I move the planet around for just a few times or all the time? What kind of a loop should I use here?
  • How can I make Pac-Man always follow the planet? Which loop should I use? 
  • What values should I use for Pac-Man's X and Y coordinates? 
  • Here, the kids quickly saw that without a small degree of separation between the coordinate values of the planet & Pac-Man, the two sprites overlap each other.
  • BTW, the "if-else" clause was purely optional, for those to wanted to add another level to their game. The majority went with just a "go to x() y()"
  • And finally, the interactive part of the game... I put in the requirement that there should "a key press" or "the green flag click" to make the game start, and something similar to end the game. 

In the next one hour, the children came up with multiple variations of the game, making their own sprites and designing various versions of tag... All in all, a very fun class for them and me to wrap up the school year.

Happy Summer!!