Binary conversion in Small Basic

Time to try Small Basic with a binary conversion program.  All seems straightforward, except that I found one annoying quirk with Small Basic – even though I built up the answer string by passing in the 1 and 0 in quotes, which should produce a string result, Small Basic added them as though they were numbers – so “1” + “1” + “1” would produce 3 instead of “111”.

binary code v1

Binary code v1

I ended up with two versions of the code – the first version, keeping things simple by adding strings, used “I” and “O” instead of “1” and “0”, which then produced the desired result.  Again I’ve kept it simple by not putting a loop in, so that the code needs to be run again for each number.

Small Basic itself seemed very straightforward, using the same algorithm as I used in other languages.

display from version 1

Display from version 1

The built in intellisense helps to formulate instructions correctly, it lays the code out neatly so it is easy to follow, and color coding also helps.


binary code version 2

Binary code version 2

The second version, which does output 1s and 0s rather than Is and Os, used the more cumbersome result=Text.Append(result,”1″) to build up the answer string.  This prevented the earlier problem of turning the strings into numbers, but looks more confusing and clumsy.  I wonder what other examples I’ll come across of Small Basic trying to be too helpful and correcting something that it shouldn’t do.

Result display version 2

Result display version 2


Binary display in Scratch

digit sprite

digit sprite

Python was a straightforward input/process/output project, but in Scratch displaying binary numbers can be much more visual.  I found it fairly easy to create once I’d got the basic principles: I used 9 sprites altogether, one for each of the 8 digits and a button which handled most of the actual code.

Each digit sprite has two costumes, one showing 0 and one showing 1.  In fact they have a third, which is the number they represent, but I chose not to continue with the option of displaying that as it would have made the project more complicated and I wanted the first version to be as simple as possible; instead I displayed the value of each digit below it on the background.  Each of these digit sprites responds to three instructions: when Green flag clicked switch to costume zero, when I receive reset switch to costume zero and when I receive [name of digit] on switch to costume one.

button sprite code

button sprite code

The button sprite resets all digits to zero, asks for the number to display, uses a variable to store the answer, then runs through the algorithm to turn the right digits on, broadcasting a message to each digit that needs to be turned on.  The number that is to be displayed is shown at the top of the screen as well.

This project was fun to create, but more complicated than the python version as I had to work out how the sprites would work together to complete the project.  There is also the possibility of becoming distracted by sorting out fancy graphics rather than focusing on pure code, so while I would probably encourage students to play with a finished version and see how it works, I’m not sure I would give it to them as a project for them to create too early on in their learning. as the visual element can distract from the code learning.  On the other hand, it would work well as an extension activity to students who already have experience in Scratch and have learnt the project in a different form.

You can try my project out for yourself on the Scratch website, and download it to look at the code used.  I made this completely in Scratch rather than BYOB, as it did not seem to need any of the extra features.

Displaying binary numbers in Python

Understanding a binary number

Understanding a binary number

One topic that needs to be covered in OCR GCSE Computing (and presumably in the other exam boards as well) is converting denary numbers to binary and vice versa, so this would seem to be a good topic to use in a practical programming session in the various different resources.  This time I’ll be looking at writing a program in Python to input a denary (base 10) number and display the result as binary.

The program would use the following concepts: 

  • inputting a number and storing it as a variable
  • simple arithmetic on that variable
  • simple string concatenation (recognising 1 and 0 as characters as well as numbers)
  • if statements testing number for size (>=; greater than or equal to)
  • writing to screen

I would recommend that most of these skills are introduced as part of getting used to Python, for example inputting a number and reporting if it is bigger than or smaller than a set number; inputting two strings and putting them together before displaying (“Hello ” + name, for instance).

Extension possibilities for this exercise: loop the program to continue asking for numbers and displaying them, testing input for validity and stopping program or giving error message if invalid number is input (could be used to end program), exploring ways to display the answer other than outputting a string (this could possibly be on a revisit having learnt graphic display options later on).

the code for displaying a number as binary

binary display program

The algorithm for converting a number from denary to binary is straightforward – each digit in the binary represents a specific number. If the denary number is at least as big as that number, then turn that switch on.  Then you have accounted for that part of the number so can subtract it from the running total.   This program will work for numbers from 0 to 255 (using one byte, or 8 bits (switches) for storage).  This is basically the same algorithm I used for the arduino, to turn on or off a set of LEDs to display a number.  The only difference is that for the arduino I set all lights to off first, whereas here I’m building a string character by character, so have to include the else: line to turn the bit off if not in use.

binary code program results

Results of running the code

Note that at the moment there is no check on the input, so it’s up to the user to enter a number in the correct range, and I have to re-run the program each time I want to enter a new number.  This would be easy enough to fix, using a while loop for the entry and looping as long as a valid number is input, displaying that number then asking for the next.

I could also extend the program by writing a function to deal with displayBinary, and calling that within the loop, so separating out the main running loop (ask for number, display answer) from the rest of the work (work out what answer to display), but I was trying to keep this program as simple as possible in order to be able to have students run it and understand it very early on in their learning.


Arduino – further experiments

arduino set up with 8 LEDs

Arduino with 8 LEDs

While I had my arduino set up with eight LEDs, I thought it was the ideal opportunity to write my own function to take in a decimal number and display the binary equivalent.

This proved extremely straightforward to do, leaving me with a pleasant sense of having got the idea with output code and basic C structure.

void displayBinary(int number)
   int delayTime=5000;
   int num=number;
   if (num>=128){
   if (num>=64){
   if (num>=32){
   if (num>=16){
   if (num>=8){
   if (num>=4){
   if (num>=2){
   if (num>=1){

Then I just called the function in the loop section, with different integers.  Something to aim for in future is to input a number on the computer and have that number pass to the arduino, which then displays it!

all the assembled parts for testing the motor

The parts for testing the motor

Then it was time to test a different output device, so I collected the pieces for circuit 3, the motor test.  This introduced some new pieces: a transistor, a different resistor and a diode, as well as the motor itself.  I still don’t have much of an idea yet what they do, but it was still straightforward to clip the pieces together to build the circuit. In a way, though, I think the breadboard hinders learning, as although it makes it very easy to connect the pieces together, it’s not obvious what’s going on underneath, and where circuits are being completed.  I also need to learn how to read an electronic circuit diagram sometime!  It’s been years since the batteries, switch and bulb days of my childhood science.

Testing the motor

Testing the motor

Still, the motor and other parts were soon connected (about 10 minutes max), and I downloaded the program from the web and uploaded it to the arduino. There were three sample functions, one to run the motor, one to accelerate it slowly and one to decelerate it.  All seemed to run very smoothly, using the same principle as the code to dim the lights, of sending different values of i in a loop in turn to analogWrite(motorPin, i) .  Now I need to figure out what sort of things I could connect the motor to.  This is where something like lego or k’nex might come in handy, to build a housing and purpose for the circuit.  There is a 9v adapter supplied with the kit, so that it doesn’t have to run connected to the computer.

So I’ve seen some of the outputs in action so far, lights and motor.  Still to come are the servo for finer control of movement and using a shift register, which apparently will reveal how to control a group of LEDs with less than one pin each.  Then there’s sound to add, and then I’m on to the input options.  That’s when things should start getting really interesting as it will then be possible to write interactive programs.

I have now also bought a book on programming the arduino, so there will be plenty more to come on this topic.