Okay kids, we’ve all seen what Hiro Hamada, Tony Stark and Flint Lockwood can do building and coding robots in the movies. Wouldn’t it be awesome to know how to code just like them? In this first installment, we are going to start your journey into the exciting world of coding by learning how computers think.
Written for ages: 8+
Assumed coding knowledge: None
In this article, you’ll learn about computers and foundations of coding. You'll require a computer with a printer in order to print the resources.
Think about all the computers that are around us. Your school classroom surely has a few computers in it, along with a smart whiteboard. You likely have a computer or two at home as well. Do you have an iPad or tablet? How about a smartphone? Even a microwave, Fitbit, and fancy electric toothbrushes have computers in them. A smart TV that connects to the internet also has a computer in it. Wowsers, that’s a lot of computers just in your home.
Have you ever seen a 3D printer working or played with a Sphero, ANKI or LEGO™ Boost robots? They all need to be programmed in order to work.
Even if you don’t want to become a computer programmer, learning to code can teach you other skills such as problem-solving and critical thinking. These skills can help you in everyday life from improving your chances of winning your favourite games (Chess for example), or to help you work smarter in whatever job you choose to do in the future.
We will assume you have never done coding before. This article will teach you about the basics of computers and teach you how to code without the need of one. You will need access to a computer with a printer, however, to print the parts you need to play the games.
By the end of this article you will be able to show off your new-found coding skills to your friends, family or teacher. You will also be ready to read next month’s article on Scratch programming.
HOW DO COMPUTERS WORK?
First, it is important to learn a little bit about how computers work before you learn to code.
Computers have Inputs, Outputs and a brain called a processor. Let’s explain them more in detail.
You will be familiar with some common computer inputs. Your computer’s keyboard and mouse are common computer inputs. These input information into your computer. Other inputs include touchscreens, switches, sensors and even a microphone. If you think the camera on your phone is an input, you would be correct.
Outputs you would be familiar with would be a computer monitor or printer. These display or print the information from the computer. The speaker on your phone would also be an output.
The processor is like the brains of the operation. It needs to listen to the inputs, perform any actions depending on the input, and then show what is happening on the outputs. The processor will usually have some memory to store the information from the inputs, and store a computer program.
WHAT IS A PROGRAM?
A computer is pretty useless unless it is told what to do (no, not like your parent nagging you to make your bed). The computer needs to be given a detailed list of instructions on what it needs to do to perform certain tasks.
Let’s give you a few examples of programs that would be in devices around your home:
THE MICROWAVE: If you want to heat food in a microwave, the microwave first needs you to enter the power and time into the keypad, wait until you close the door, heat the food until the timer ends, and then beep and display that the food is ready on the screen. The microwave also needs to be programmed to stop in case someone opens the door while it is heating.
YOUR FAVOURITE CONSOLE GAME: A program that tells the Xbox, Playstation or Nintendo what to display on the TV screen, and what to do when you press the buttons on the controller.
ROBOT VACUUM CLEANER: Have you seen one of those fancy robot vacuums that clean the house automatically? Think about how it is programmed to turn around when it hits a wall or furniture, avoids falling down a staircase, and knowing when to recharge itself when the battery is going flat. To avoid falling down a staircase a sensor such as an infrared beam tells the program to turn the robot around.
WHAT IS CODING?
Code is a set of instructions in a special language that a computer can follow. Coding can also be called computer programming. Coding is when you write those instructions in a special language that computers can understand. There are hundreds of different coding languages from easy to use Scratch that we will describe next month, to complex languages that are powerful and efficient but take many years to master.
Python is a popular language that is great to use after you have mastered Scratch. Python can be used on a Raspberry Pi. Arduino uses a language similar to C++.
If you play Minecraft, some of that is written using a language called Java.
Before we go on to learn a language though, we need to learn how to think like a computer.
THINKING LIKE A COMPUTER
For a computer to perform tasks, it needs to be given step by step instructions to follow (i.e. a program). For those of you who have never seen “i.e.” before, this is a Latin word (another language) that means “that is”.
Think about how you would bake a cake. You need a recipe to follow to get it to cook and taste just right. The ingredients need to be measured correctly and added to the recipe in the right order. You need to know what temperature to set the oven to and how long the cake needs to bake. If you did it all in the wrong order, with the wrong measurements, you’ll probably end up with an awful tasting pancake. Yuck!
To think like a computer, we’ve made some games to teach you while having some fun at the same time.
LET’S PLAY SOME GAMES!
Before we get stuck into our cool games, we’ll need you to use a computer and printer to print the required printable parts. Ask a parent or your teacher if you are unsure how to do this. The files to be printed can be downloaded from the DIYODE website by going to the link at the end of this article.
These cut-out pieces will help us keep track of the movements our car needs to make in our street maze. Think of the cards as the steps in our computer program that we will imagine are uploaded to the computer in the autonomous car.
Let’s go through the pieces you should have:
STREET MAZE: This is the road you will drive your autonomous car.
CAR: This is an imaginary car that you need to code to drive along the road.
FORWARD: Move the car forward one space.
REVERSE: Move the car backward one space.
LEFT: Turn the car left on the same spot.
RIGHT: Turn the car right on the same spot.
LOOP CARDS: These tell you how many times to repeat (loop) the previous step.
CONDITION CARD: We use this card to make a decision in our program.
CONSTRUCTION CRANE: This will be used as an obstacle in the scene in an algorithm.
Note: An autonomous car is a car that drives itself. It looks like a regular car, but it has computers and many sensors to know where it’s going and to avoid any people, buildings or obstacles. A lot of programming must go into these autonomous vehicles, but we’ll keep it simple in our demonstration.
YOUR FIRST ALGORITHM
An algorithm is a step-by-step instruction that you need to code into your computer. In our case, we are going to give your autonomous car the algorithm it needs to drive from the garage of your home to the shopping centre carpark so you can get some ice cream.
Algorithm 1: Step by Step
STEP 1: Place the car on the garage square of your home
STEP 2: With your finger, count the number of squares on the street from the garage to the shopping centre car park. In this case it is 7 squares in a straight line.
STEP 3: Place 7 of your green arrow cut-outs in front of you. They should be all facing up and in a row. These 7 green arrows represent the algorithm that your car will need to follow.
STEP 4: Let’s imagine you have uploaded this algorithm to your car to drive you to the shopping centre. Go ahead and move your car from square 1 to square 7 times so it ends up in the shopping centre car park.
Nice work! You have successfully executed your first algorithm.
Algorithm 2: Sequencing
Next, we will take your autonomous car to your friend’s house so they can have some of the ice cream you just bought.
This time we will use a computer concept called sequencing. This is a list of actions in sequential order that can’t be skipped.
STEP 1: Layout the following cut-out cards like shown here. This is your new algorithm that your car needs to follow in order to get to your friend's house.
STEP 2: Your car should be still in the shopping centre car park. The first step in the algorithm is to turn your car right on the spot two times so your car faces the street.
STEP 3: Move your car 4 squares towards your friend’s house.
STEP 4: To turn into your friend’s carpark you need to turn the car to the left so it faces the garage.
STEP 5: Drive the car one space forward.
Your car should be now in your friend’s garage. Enjoy the ice cream!
Algorithm 3: REPETITION (LOOPS)
You look at the time and realise you are late for school. You better program your car to drive you to school on the quickest route. Have you seen your parents do this in the GPS navigation system in the car or on their smartphone to drive somewhere?
This time we will use a computer concept called Repetition. This is also referred to as a loop. In the previous algorithm 2 we drove the car step by step with an instruction to move from square to square. This time we will code the autonomous vehicle with a loop statement so it goes forward 4 times in a row, not 4 steps one after another.
STEP 1: Layout your cards in the order shown here.
STEP 2: Rotate the car twice on the spot so your car faces the road. For example, right, right.
STEP 3: Go forward 1 space to get your car onto the street.
STEP 4: Turn left on the spot to point towards your home.
STEP 5: Drive forward, and repeat again (this means the car moves forward 2 squares without stopping at each square)
STEP 6: Turn right on the spot so your car is facing the school.
STEP 7: Drive forward once, then 3 more times (4 steps total) so you finish in the school car park. Enjoy your class!
ALGORITHM 4: Conditional Logic (IF...Then)
The school bell sounds, so it is time to go home. You want your autonomous car to drive straight home, but what if a construction crane has blocked the road outside the Construction Site car park? If the road is blocked then you will need to go home the long way past the Shopping Centre.
Conditional logic means we need to make a decision in our program. In our case, if the road is OPEN we go the quick way home past the Construction Site, but if the road is BLOCKED we need to go the long way past the Shopping Centre.
STEP 1: Put the cards in front of you like shown at the top of the page. You will notice it branches out into two rows after the third step. This is where we go a different direction if the road is blocked.
STEP 2: Turn car right twice on the spot so you are facing the street.
STEP 3: Drive forward 1 square.
STEP 4: Here we make a decision. If the road is OPEN then we will drive straight home. If the road is BLOCKED by the construction crane we will go the long way.
We see there is a construction crane blocking the road ahead so the road is BLOCKED. We need to go the long way home past the Shopping Centre making the following steps.
STEP 5: Turn left, then forward 5 times, then turn right.
STEP 6: Forward 3 times then turn right.
STEP 7: Forward 6 times.
There you have it. You have just learnt the basics in coding your very own autonomous car!
You have learnt about Algorithms being a set of instructions, a Sequencing statement of steps that can’t be skipped, a Repeat statement that loops code a number of times, and a Conditional statement that makes something else happen if the first way can’t be met.
WHERE TO FROM HERE?
Design your own larger street scene with more buildings and obstacles. You could have conditional statements where police block the road and you need to turn around for example.