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.


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


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
from the previous classes (the Food Chain and the Pong Game) to design a Math video game that asks questions and waits for answers from the user. The answers are checked for their correctness, and if correct, an LED on the Arduino board lights up.

Computer Science Concepts

  • Designing interactive programs
  • Conditionals
  • Comparators
  • Timing
  • Variables

Common Core Standards

  • Variables

  • 1-1.5 hrs

  • Software Development Environment: S4A (Scratch for Arduino)
  • Arduino Uno
  • USB cable to connect the Arduino to the laptop
  • Breadboard
  • LEDs (1 LED for the basic program, 2 LEDs for the challenge section)
  • Jumper wires
  • Resistors (1 for the basic program, 2 for the challenge section)


Lesson Plan

Part 1:  Before the Lesson:
Consult with the classroom teacher two to three days before the class and ask the students to write down 4-5 simple math problems, as well as their solutions, to use in the video game design. Questions can range from simple equations involving addition/ subtraction/ multiplication/ division/ fractions/ percentage/ decimals, etc. to small word problems. This pre-set group of questions and answers should be brought into the class on the day of the programming assignment.

Part 2:
On the day of the programming class, ask the students to keep the above list ready. Use the attached set of slides to help the students understand the requirements for the program, and the basic set of instructions to use. The slides provided here show how the program is built up, step by step.
Let the students tag you through the initial steps, by programming on their laptops. Once they get the basic idea about the design, the students can tap into their creativity & program the rest on their own.

  1. Ask the students to open the Scratch or S4A application on the laptop.
  2. Create a new project:  Go to File →  New →  Save the Project with a name
  3. Go through the requirements/specs for the video game design

Note that the Arduino Sprite is present on the screen. The Arduino board is treated just like a regular Sprite in S4A, which makes it really easy to program. We shall have the Arduino Sprite ask the questions, check the answers and light up the LED accordingly. 

BTW, if the kids would like to see a different look for the Arduino Sprite, just change its costume: choose the "costume" button for the Sprite, and then one of the three options - (1) "Import" to get a Sprite from the library, (2) "Paint" your own Sprite, (3) "Edit" the existing Sprite.

Part 3:
Go through the attached slides, with the students tagging along on each step. Given below are a few pointers to accompany the slides.

Slide 2:
Specs for the program:  We are using a single LED to provide a visual indication as to whether the answers in our Math game are right or wrong. Point out a few examples from daily life where LEDs are used as visual indicators to denote if something is working correctly or not: TV remotes, appliances in the kitchen/home, electronic devices, etc.

Slides 3, 4, 5:
Build up the program in baby steps. Start with a single Math question & go through the entire design process with it. Comparators & Conditionals are the two concepts used extensively in this program. The students would also be introduced to a new variable - “answer” - that is used to store the user’s response to each question. By checking the box next to “answer”, you can see its value on the screen in real-time. This might be a helpful tip for the students.

Slides 6, 7, 8:
Connect the Arduino to the laptop. Wiring diagram for the LED is attached. Might be helpful to point out how the connections in a breadboard work.

The students learn the basics of electrical circuits (serial & parallel) in grade 4. Here, we create a simple serial circuit with the current flow going from Digital Pin 13 through a resistor to the LED, and from there to the GND via a jumper wire. The resistor is used rather than a jumper wire, to prevent the LED from getting damaged. LEDs have a tendency to draw up as much current as they can, rather than what they should - it's like going to a buffet and eating too much, rather than what is required for the body :) Hence, a resistor to prevent the LED from drawing too much current & getting burned out.

Slides 9, 10:
Modify the code in S4A to help the LED blink - set Pin 13 on/off. The wait instruction is required between the Pin on/off statements; the execution of instructions in the computer happens really fast and hence, this delay/wait statement is needed for us to see the LED blink. Let the students experiment and find out that without this delay, the human eye cannot detect the LED being turned on & then off.

Slides 11, 12:
The students should now be allowed to build up the rest of the game on their own, following the structure used so far. They shall run the code with multiple test cases to ensure that the game is working as intended. Each group of students could be encouraged to play other groups’ games at this point.


Slides 13, 14:
For those students who finish the above assignment early, a challenge can be provided to light up two LEDs - one each for right & wrong answers - as they proceed through the game. The corresponding wiring diagram is attached on Slide 13. The modification needed for the code is shown on Slide 14.

Slides 15:
Add a variable to keep track of the right answers. You can even go a step further and use two variables - one each for right & wrong answers, to make the game more interesting. Remember to initialize the variable(s) to zero at the beginning of the game. Check the box next to the variable names when creating them, so that they stay on screen all the time.

No comments:

Post a Comment