Showing posts with label Conditionals. Show all posts
Showing posts with label Conditionals. Show all posts

Sunday, January 10, 2016

S4A (Scratch for Arduino) Project - A Keyboard Controlled Robot Car

A fun project to create a keyboard controlled robot car... based on the "Robotics: keyboard driven car" project on the S4A site. This to me, is a perfect example of an interdisciplinary project: there is quite a bit of mechanical, electrical and computer engineering involved. This project is being used by the Grade 5 kids in my child's school this year; the kids have been working with the Pro-Bot for quite a bit of time and I felt that letting them create their own robot car that behaves similar to the Pro-Bot is a great finale. So far, it's my favorite Arduino project. Below is a formal lesson plan for the robot car.

Note: This is a simplified version of the car from the S4A site - it just involves creating the keyboard driven car, minus the remote controller. This means the car stays connected to the laptop via the USB cable.

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

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.

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, 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!!