This is a draft, open for comments. Got to add diagrams to this. You can send me feedback here if you have some.

Ok kids, would you like to learn how to speak to a robot?

Let's jump right in.

The first thing you need to know is that robots do everything exactly the way you tell them to do it. So if you tell a robot,

Go to the store and pick up some eggs

It will (if you have explained to it what go means, and what a store is, and where the store is, and how to operate a door, and so on) go to the store, and then it will helpfully lift eggs into the air. And then it'll just stand there.

So you have to get a lot more specific.

Putting a robot to bed

Let's say it's bedtime for a robot. Before it can go to bed, it needs to take a shower, brush its teeth, and put on its pyjamas.

If you just tell the robot,

Get ready for bed!

It'll just stare at you stupidly. What's bed? What's get ready?

Let's try again.

Take a shower!

The robot heads to the shower and you, realizing what it's about to do, dash after it and stop it from dutifully ripping the shower head out of the wall. Stop program! Cancel! Escape! This is clearly not the way to go. You're going to have to get more detailed.

  • take clothes off
  • turn shower faucet on
  • stand under shower
  • pick up soap
  • rub soap on body
  • flush off soap
  • turn shower faucet off
  • dry body with towel

Robot takes its clothes off, turns the shower on, stands under the water, picks up the soap, rubs the soap on a tiny part of its chest for...less than a second? No, no, silly robot!! We're going to have to tell it where and for how long to rub soap on itself for. But it's not really about time, is it. It's about making sure there's soap everywhere. So let's try this:

rub soap on every part of the body where there is no soap

there's a better way of putting this:

  • WHILE there is a part of the body with no soap:
    • rub soap there
  • flush off all soap, turn off faucet, etc

WHILE is useful for telling Robot to do something until a certain thing isn't true anymore (in this case, until there is no longer a part of the body with no soap.) Once WHILE is done, Robot will continue with the next step.

Ok, now showering seems to be working. Robot diligently makes sure every square centimeter of itself has soap on it before flushing it all off.

It would be super annoying to have to repeat all these commands every time we want the robot to take a shower though, so lets give the robot a shortcut command:

Function: take a shower

Robot! Listen to this. Next time I say:

take a shower

I want you to remember all of this stuff and do it:

  • take clothes off
  • turn shower faucet on
  • stand under shower
  • pick up soap
  • WHILE there is a part of the body with no soap:
    • rub soap there, then check if IF (above) is still true
  • flush off all soap
  • turn shower faucet off
  • dry body with towel

ok? Let's run a little test. Testing as you go is always a good idea, because it helps you catch mistakes before they get too complicated for you to figure out. "Robot", you say,

take a shower!

and Robot dutifully takes a shower. Hooray! We have a clean robot. But how about brushing teeth and putting on pyjamas?

Function: brush teeth

Brushing teeth means a lot of moving the toothbrush back and forth. We could keep telling Robot:

Move brush right
Move brush left
Move brush right
Move brush left

That's going to get really frustrating really fast. We should really give the robot one simple command that describes this, like "brush", the same way we turned "take a shower" into a shortcut. First we'll have to describe it to Robot though. Let's try this: Robot! Next time I say "brush" do this:

  • Move brush right
  • Move brush left

That's one brushing action. To have robot brush a lot, we could say:

brush (100 times)

after which Robot would move the brush right and left 100 times, which you'd think would be a bit excessive. How about this instead:

  • WHILE teeth are dirty:
    • brush, then check if IF (above) is still true
  • flush brush under tap, put brush in cupboard, etc

This makes a lot more sense, because we want Robot to brush its teeth to make them clean, and not to just brush a certain number of times.

But how do we check if teeth are dirty? Robot has a sensor in its mouth that can measure how dirty its teeth are from 1-10. 10 is really dirty. 1 is super clean. So we could also say:

  • WHILE (teeth dirt =>1)
    • brush

= means "equals", and > means "larger than".

So =< means "equal to or larger than".

"teeth dirt" is an example of a VARIABLE, or something that might change from one day to the next. Robot's "teeth dirt" might be 10 if it has been guzzling soda and candy all day, or maybe 8 if it had an apple. Every evening might be different. Also "teeth dirt" changes with every brush, and the point is to stop brushing when "teeth dirt" is 1.

After a period of testing and adjustments, you add some commands to have Robot pick up and properly squeeze the right amount of toothpaste onto the right end of the toothbrush, get the toothbrush into the mouth and not the eyeball, and have the brushes actually touch the teeth instead of the back of the brush. Now it's time for:

Function: put on pyjamas

First, Robot has to identify which clothes are actually pyjamas. There's an issue with "put pyjamas on" to which Robot responds with a blank stare until you realize it's stuck on "put pyjamas on what". You try:

put pyjamas on yourself

Robot ends up with its pyjamas neatly piled on its head. No, no, no. Again, details matter. You have to break the big step down into smaller steps.

Pyjamas are in two parts, the shirt part and the pants part. They're both quite different, and Robot will pick them up one at a time, so it makes sense to tackle them one at a time.

Mini-function: put on pants

Now we need a bunch of commands to put on pyjama pants:

First you should check if Robot is already wearing other clothes. Probably not, because Robot just took a shower, but maybe some other evening Robot might skip the shower? So let's say IF Robot is not wearing pants, then Robot should put on some pyjama pants. Otherwise (ELSE), Robot should take off any pants its wearing and THEN do all this.

  • IF: you are not wearing pyjama pants or other pants
    • pick up pyjama pants
    • IF: you are holding one of the two small openings
      • turn pants so you're holding the big opening
    • ELSE: IF: a tag is facing you
      • rotate so tag is not facing you
    • ELSE: Put a leg one at a time into the big hole and have each come out a different small hole
    • pull big hole up to waist
  • ELSE: IF: you are wearing pants
    • take off pants
    • go back to beginning of put on pyjama pants
  • ELSE: put on shirt

Mini-function: put on shirt

Now let's call another bunch of commands put on shirt:

(You should have gotten an idea of how this works by now...try it yourself!)

Putting it all together

We now have a function (command shortcut) called: Get ready for bed.

Inside it are three functions: take a shower, brush teeth, put on pyjamas

brush teeth has brush in it

put on pyjamas has put on pants and put on shirt in it

Notice how the big command is made of lots of little commands which also have little commands? This is how we talk to robots! Functions inside functions inside functions. And we do it like this so we don't have to explain everything one by one each time. Convenient, eh?

Another way to see this:

  • Get ready for bed
    • take a shower
    • brush teeth
      • brush
    • put on pyjamas
      • put on pants
      • put on shirt

We also used WHILE and IF and ELSE to describe what to do in certain situations.

And a VARIABLE to describe something that keeps changing (teeth dirt).

Everything I've told you here are actual ways that people tell computers and robots how to do things. Different languages have slightly different ways of saying these things, but I bet you can see familiar parts:


while teethdirt =<1:
put down brush


while (teethdirt =<1){
put down brush;

Also notice that they're not that different. Python uses spaces, JavaScript uses (),{} and ; to show where a loop starts and ends. There are languages that are super different from this, but they're not as common as these two.

If all of this seems interesting and you want to try telling a computer what to do, have a look at these links below. Good luck!

HTML dog tutorials

Glitch online coding and beginner's community

Codepen online coding and community examples