Showing posts with label S4A. Show all posts
Showing posts with label S4A. Show all posts

Tuesday, March 15, 2016

A Musical Instrument with a Photoresistor - Scratch for Arduino (S4A) project

A project to make a musical instrument using the Arduino and Scratch for Arduino (S4A). The inspiration came from the theremin-like musical instrument project on s4a.cat

The version below uses a photoresistor. It is much simpler than the s4a.cat one and I have not used any LEDs either. I might add them in, as part 2 of the project.

A photoresistor (or light-dependent resistor, LDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. (Wikipedia)

Monday, March 7, 2016

Scratch for Arduino (S4A) Project - A Food Detector

Here is a project to design a Food Detector - an interactive game that tries to identify the type of food among a few given food items.  It uses the Arduino's analog inputs to measure the resistance offered by different food items. I designed this lesson for our Grade 5 students, and taught it last week at school. The inspiration for this lesson came from the Pixelate game, that I found on the Arduino site. The class went really well, and the kids had fun playing detective with different fruits & vegetables.

Below is the formal lesson plan:

Aim

Different food items have different resistance/ conductance to electricity. Learn to use the analog sensors in the Arduino to identify different food items, with S4A (Scratch for Arduino) as the software development environment.

Monday, February 15, 2016

S4A (Scratch for Arduino) Project - Robot Car Part 2 - Indicator Lights

Continuing with the previous project of the keyboard controlled robot car and adding indicator lights to it... This is a really simple addition to the robot car, just a few LEDs to act as the car's indicator lights. And modifying the program to blink the LEDs on left/right turns and on reverse.

Additional materials for the indicator lights:
  • 4 LEDs (preferably, 2 each in the same color)
  • 4 Resistors
  • Jumper wires as needed

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

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.

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!


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.