Showing posts with label Arduino Uno. Show all posts
Showing posts with label Arduino Uno. 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

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!


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.