I’ve been using the Arduino with my groups of Year 11 enthusiasts and we’ve been exploring together some of the tutorials and demo set ups, which is all reasonably exciting, but doesn’t really offer much of a progression. You wire up a circuit according to the diagrams, you download the code, you run it, it works (or not), you cheer (or not), you move on to the next one. Rinse, repeat.

I wanted something more. I wanted my students to learn some useful Computer Science concepts through first hand experimentation. I was walking down the road on the way to supervise a Zumba session (why does my inspiration always come whilst walking to sports practices?) and I thought of a task which involves all of the following:

- Wiring a circuit
- Binary counting
- Where those funny #0099ff type numbers in HTML colours come from
- An appreciation of the need for efficient algorithms
- Arrays
- Iteration

…and best of all

- Computational thinking

Now, people like Tom Crick keep banging on about ‘computational thinking‘ and how important it is, and other people keep agreeing and nodding their heads like the Churchill dog and I’m not quite sure if they understand or not. I just want to be able to ask students to be able to attempt a challenge with enough depth that they begin to get an appreciation of how large scale problems involving Computing can be.

Enter…the binary counting machine.

### 1. What is binary?

My students are just enthusiasts, we aren’t offering GCSE Computing until next year so they have had no formal Computing lessons at all. Thus, they do not know what binary is, other than “noughts and ones”. I started off by introducing them to the way we write numbers in binary – place values, number of bits etc. I was going to post a link to an explanation here as I guess most people reading this will already know this…but I can’t find any good ones! :( As my oft mentioned logic lecturer used to say, that is left as an exercise for the reader. (Which is code for I can’t be bothered to do notes on this bit.)

### 2. Wire it up and check it works

Now, the students need to wire up the Arduino with four LED’s in a row. This isn’t hard – you do this but with four LED’s instead of eight. You might as well run some sample code to check that all of the LED’s turn on and you’ve wired it up correctly.

### 3. Make it count to 10

So we know that to turn a LED on, we use

digitalWrite(ledPins[0], HIGH); //Turns on LED #0 delay(delayTime); //waits delayTime milliseconds

So it’s a fairly trivial task, even if the students have no idea what an array is, to figure out how to make the lights count in binary

i.e. Binary 1 = Off Off Off On,

Binary 2 = Off Off On Off,

Binary 3 = Off Off On On etc.

Make a program which counts to 10. You (and they) might find this tedious – but that’s entirely the point! They are supposed to get bored of copying and pasting code and start asking “Isn’t there a better way of doing this?”. You can even jokingly set the task at the start to count to 100 if you want – wonder if they’ll figure out that they need more LEDs for this!

### 4. Isn’t there a better way of doing this?

Of course! This is where we apply some Computational Thinking – Boom! (Or, as my colleague mistakenly put it, Bang.)

We are going to solve a tedious problem through the power of thought. Wouldn’t it be good if we could take a decimal number, figure out how to represent that in binary, and then instantly display it on the LEDs? Then we wouldn’t have to go through the tedious and potentially error ridden human task of figuring out which lights should be on and which should be off, and we could use a loop to count to whichever number we saw fit. Result.

The students should be able to come up with the decimal to binary algorithm themselves with a bit of thought. So you don’t have to bother thinking, here’s pseudo code for one I made earlier:

// Define how many bits you have and their bit place values // starting with the largest placeValues = array[8,4,2,1]; binaryNumber = array[0,0,0,0]; decimalNumber = 9; // or whatever for i in placeValues{ if( decimalNumber >= placeValue[i] ){ binaryNumber[i] = 1; decimalNumber = decimalNumber - placeValue[i]; } }

Woohoo! So now we have a binary number (as an array). We can loop through the binaryNumber array, corresponding it to the LED positions and if the value is 1 light the LED, and if not, turn off the LED.

### 5. Bonus learning stuff

There are also some bonus easter egg features to this lesson:

- I really like this because instead of just doing a task this way “because I said so”, it’s a good illustration of how being clever with our thinking can result in better code and more efficient programs. With more efficient programs, more becomes possible – our counting to 100 task doesn’t seem so ridiculous now, does it?
- You can show how to make a decimalToBinary() function, illustrating perfectly why functions are beneficial as we don’t have to keep copying and pasting code all over the place
- Perhaps you could use this to make a binary LED clock?
- If you don’t have an Arduino, you can still benefit from the Computational thinking by getting students to write a decimal to binary converter in Python or another language of your choice
- When you’re exploring the possibilities of different number bases, you can also mention (e.g.) hex – they will have probably seen the hex colour codes in HTML, and suddenly this will click into place

Love this — inspired me to use something similar with my students. For more ‘bonus’ how about auto building/replacing the placevalue array with 2^(n-1)

Thank you for this great idea and associated pseudo-code!