Pages

Friday, October 4, 2013

Learning to Code and Coding to Learn



Learning to Code and Coding to Learn (Updated 12/21/2013)

In a TED video about kids learning to program computers, Mitch Resnick pointed out that when we’re young, we learn to read and eventually that enables us to read to learn.  The same idea applies to computer programming (coding).  First we learn to code and then we can code to learn.  I argue that it’s impossible not to do both simultaneously, and that’s a great thing!

For some time now, I’ve been disappointed with how few schools actually teach computer programming.  There are many children out there under the age of 13, some under the age of 10 who are learning to code on their own.  There are languages that use drag-and-drop blocks to teach the concepts of coding and plenty of resources to learn how to use them.  Dozens of compilers or editors can be installed for free to code on Windows, Mac, or Linux computers.

My daughter uses a device called a Raspberry Pi to code in multiple languages.  She’s 11 years old.  The Raspberry Pi is a $35 Linux computer on a credit card sized circuit board.  It can be programmed from the Linux command line in Java, C++, Python or many other languages.  Through the graphical Linux screen, users can install software in order to program in compilers like IDLE or Nano in a variety of languages.   I’ll talk about some of the things my daughter has learned as she’s been learning to code.  She blogs about her adventure at http://raspberrypikid.wordpress.com

Variables-
If you ask most kids, “What is a variable?” they’ll tell you, “X is a variable.”  They really do not understand the concept of what a variable is.  You’d be hard-pressed to find any computer program that doesn’t have dozens or even hundreds of variables in it.  When my daughter was learning about coding in Python, variables are one of the first things she learned because you truly cannot do anything in coding without variables.  I challenged her to write a couple of programs using variables and she completed them all. 

First, when she learned how to ask a user a question and store the answer in a variable, I challenged her to write a program in Python on her Raspberry Pi that asked how many pennies, nickels, dimes, and quarters the user has and then report to them how much money that is.  This requires storing each of the answers in a variable, multiplying the variable by the coin’s value, adding up all of the coin values, and then getting the answer to display in dollars and cents format.  She really came to understand variables and calculations using variables from this program as well as the conversion from cents to dollars.  In addition, she learned a lot of new coding concepts too.

Next, she learned about a math function called “modulo.”  When you take the modulo (whose symbol is %) of two numbers (7 % 2), the answer is the remainder of the division problem.  This can be used to tell if a number is even or odd, to tell if two numbers are divisible by each other, or to round a number up or down.  I challenged her to write a program in which the user could type in a number and the program would tell them if it is a prime number or not.  This involved taking user input and storing it in a variable, writing a loop to divide that variable by every number between 2 and one less than its own value, and seeing if any of those division problems gave a modulo of zero (no remainder).  Not only did she learn about variables, but she also has a much better understanding of what a prime number really is and how you can test to see if a number is prime or not.  And at the same time, she learned about writing “while loops,” an extremely important coding concept.

MIT created a programming language called “Scratch.”  It is very kid friendly and cartoony, but teaches the concepts of computer coding without having to type in code.  Instead of writing a “while loop,” users just drag a “forever block” onto the screen.  My daughter’s first project was to create an animation of a fish moving around in an aquarium.  To move the fish around, she had to drag a “move to x=__, y=__” block onto the screen and type in values.  She’s learning about ordered pairs!  But it looked funny for the fish to be moving left yet facing right so I asked her how she can tell if the fish is moving left or right.  She figured out that if the x value is increasing, the fish is moving right and if the x value is decreasing, it is moving left.  Then she looked at each movement to see if the value was increasing or decreasing and dragged a block to tell the fish which direction to face.  She learned about ordered pairs, the difference between a changing x value and a changing y value, and what it means when the x value is increasing or decreasing.  These will be very valuable skills in other classes like physics when she’s learning to analyze displacement versus time graphs.   The next step will be for her to figure out how to make it tilt upwards a little when it’s swimming upwards and vice versa.  This will also improve her understanding of ordered pairs.

One major difference between variables in math and variables in coding is that in coding, variables can hold words or letters.  This allows her to learn about language arts while she’s coding also.  She was learning a lesson in coding on how to include a variable in a sentence to make it look like it belonged there.  For example, a program might ask me the question, “What is your name?”  The program could then output, “Nice to meet you, Mike” with my name inserted into the sentence.  So, I challenged her to write a MadLibs-like program.  MadLibs are books that ask students to randomly name a noun, an adverb, or an adjective and then insert them into story frames to make funny non-sensical stories out of them.  So, she wrote a story, took words out of them, figured out what part of speech the words were, wrote into the program prompts to ask the user to type in a word that was that type of speech, and then printed out the story with that part of speech in it.  This lesson was a great example of combining learning to code and coding to learn into one activity.

One afternoon, she was playing with a device that I had lying around that measures how loud a sound is.  We started talking about the decibel scale and how different levels on the decibel scale are associated with pain and permanent ear damage.  She wanted to turn her Raspberry Pi into a sound level meter that could be installed in a factory or a school bus stop to warn people if the sounds there would damage their ears and signal with a red light that they need ear protection.  The program didn’t end up working out for technical reasons that you can ignore if you’re not interested (the sensor is analog and the Raspberry Pi is digital and it was too complicated for her to figure out how to convert the analog signal into a digital signal).  But, she wrote the program anyhow before she realized the problem.  This process involved storing the value from the sound sensor in a variable, comparing that variable to a known value, and then determining what to do based upon whether that value was greater than or less than a known dangerous value.  She was basically saying that if the sound level is less than a certain number, light the green light.  If the value is greater than a certain number, light the red light.  She was learning about inequality expressions!  How many 6th graders really understand what inequalities mean when they learn about them in school?  Fortunately, the coding of inequalities is very similar to inequalities in math.  The code would look something like this (simplified into English a little):
if variable < value:
     Light green LED
else if variable > value:
     Light red LED

She has also programmed Lego Mindstorm robots both in the drag-and-drop Mindstorm language as well as the RobotC text-based language.  For one of her projects, she followed directions online to make a Lego Segway that balances itself on only two wheels and avoids obstacles using ultrasound “eyes.”  The step-by-step instructions didn’t leave much room for learning, so I challenged her to modify the program and the robot in the end to make it do something different.  The robot was designed to turn around and roll a different direction if it gets within 20 cm of an obstacle.  I challenged her to make the robot so that it could walk on a table without falling off.  This involved pointing the ultrasonic eyes down instead of straight ahead.  She built a structure to hold the eyes, but then she had to make another structure on the back to balance it out.  She had to realize that to tell when the eyes had gone off of the table, she could measure the distance between the eyes and the table and write an inequality saying basically, “If distance is greater than the robot’s height, turn around.”   In this case, the code was already there, she just had to change it from “less than” for the obstacle-avoiding version to “greater than” for the table-walking version and type in a different number.  But in the process, she was learning about variables and inequalities again as well as measurement and the metric system.  She also learned about the physics of balance when building the robot.

In another Scratch lesson, she was learning how to make Scratchy, the orange cat character, jump up and down.  By simply moving the cat from one ordered pair to another and back, she accomplished this, but it looked terribly unrealistic.  So, we had a conversation about acceleration.  On Earth, if one were to throw an object up at a speed of 30 meters per second, after one second, it would be travelling 20 meters per second, after two seconds it would travel 10 meters per second and at 3 seconds it would have reached its peak and turned around.  After another second, it would be travelling downward at 10 m/s, after another second it would be falling at 20 m/s, and after another second it would be back to its original starting place travelling at 30 m/s in the opposite direction.  So, she built this idea into her animation.  Instead of moving from point A to point B at a constant speed, she broke the trip down into several steps, each step a little slower than the last.  After it reached its peak, she had it fall slowly, then faster and faster after that.  It was a lot of work doing this by hand and she realized that she could write a program to do it in just a couple of steps using variables and loops.  In this lesson, she learned about acceleration, ordered pairs, what a changing y-value does, and variables.

In coding, programmers can use less than, less than or equal to, greater than, greater than or equal to, or equal as the comparators just like in math.  The difference between “greater than” and “greater than or equal to” is a lesson taught in elementary school without much in the way of real world application.  This difference is very useful in coding.  If the programmer wants to run a loop 10 times, they can set a variable to zero, run the loop, and increment the variable.  They can either stop the loop when the variable is >9, =10, or >=10. 

*Update: My daughter participated in the "Hour of Code" presented by code.org as well as the extension activities.  This involved using a program called Blockly that is Google's version of MIT's Scratch.  While running through the activities, she learned a great deal of geometry.  She learned that the internal angles of a triangle add up to 180 degrees in learning to draw a triangle.  She learned about complementary and supplementary angles when determining how far to turn when drawing different shapes.  She learned how many 30, 45, 60, and 90-degree angles make up a circle and many other geometry topics.

These examples show that students can learn to code and code to learn simultaneously.  Each of these programs is simple enough that a late elementary or middle school student could understand them.  I would argue that when math is applied this way, it will certainly lead to a deeper understanding of the math with the added bonus of learning the valuable skill of coding.

My daughter blogs about her adventures in programming and her blog can be found here or she tweets about her adventures under the name @kid_pi.  Her Scratch projects can be found here and her lego robot videos can be found here, here, and here.  Mitch Resnick’s “Let’s Teach Kids to Code” video can be found here.

Download her PowerPoint here "Top 10 Reasons Kids Should do Science"
Here’s a link to the Raspberry Pi computer
Here’s a link to the Lego Mindstorms Robots
Here’s a great book about learning Python
Here’s a link to the Scratch website
Here’s a great book about creating games in Scratch
Here's a great science toy that she loves, Snap Circuits

No comments:

Post a Comment