Lets load this code onto our Arduino’s.

I’ve added lots of comments to the code itself….lets read through them together

now lets add a little code so that our code doesn’t run forever….. this is getting a little like a stop watch.
can you think of games you might create that would use some counting code?

Its no good if our little machine just goes on counting forever – lets add come more code that tells it to stop counting and start again when it gets to another number.

To do this we need to understand perhaps the most used statement in coding – the IF statement….the if statement is very simple and looks like this;

if ( something equals something else ) {

then do what's inside the curly brackets.


or in proper code it might look like this;

if ( x >= 100){    // if x is greater than or equal to 100 then

x = 0;   // make x equal zero



This is all ever nice but it would be much more interesting if we added a button to our device then we could reset the arduino to start counting and use our added button to stop the count.

Then we could use it as a kind of stop watch and play a game with it.

Wiring up a button is fairly easy and very useful. Like wiring up our LED it requires a resistor – unfortunately it also requires one extra wire. Lets have a look at it;


Our button has four pins (or legs?). We are only going to connect two of them; the top right and the bottom left. To wire up your button;

  1. Connect 5v to the top right.
  2. Connect a 10K Ohm resistor to the bottom left
  3. also; Connect the bottom left pin to a digital port (I have used port 2)
  4. Connect the other end of the resistor to (GND) – ground.

This is what it will look like without the screen;


Now we’ve got our button wired we need to do all the things that we would with an output device like and LED – but here we have an input device;

We will need to add a pin initialisation to our setup – this tell the arduino that pin 2 will be an input pin;

pinMode(2, INPUT);

Then we need to check that state of the of the button in our code. Like an LED, push buttons have two states either ‘HIGH’ (pressed) or ‘LOW’ (not pressed).

We’ll use an IF statement again to check this state in our code;

if ( digitalRead(2) == HIGH ){ // if the button pressed into port 2 is pressed

pressed = 1;   then make the variable 'pressed' equal one


we could also check to make sure the button is not pressed like this;

if ( digitalRead(2) == LOW ){ // if the button plugged into port 2 isn't pressed

pressed = 0;   then make the variable 'pressed' equal zero


In order to stop our code we will need to use one of these so that we are only adding to X until the button is pressed. Because buttons are only HIGH when they are held down we will need another variable to tell us when the button has been pressed but isn’t anymore.

I called that variable ‘pressed’.
Lets have a look at the code we will need to stop our counter.

#include <Wire.h> // these are all the libraries we need for working with our screen.
#include <SPI.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

#define OLED_RESET 4 // here we get the screen ready for sending instructions to using the libraries above
Adafruit_SSD1306 display(OLED_RESET); 

int x = 0; // this is an important new step - here we are making a container called X to put a number in.
//this is a container that can only fit integers inside of it - an integer is a whole number (the ones we count with)
//this container is called a variable. Variables are central to maths and computing.

bool pressed = 0; // this is another important step - we are adding another type of variable here -this is a Boolean variable instead of an Integer/
// a boolean variable can only hold one of two possible states 1 or 0....(off or on, high or low).

void setup() {
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // tell the Arduino that our little OLED screen is 128 x 64 pixels


void loop() {
 display.setCursor(0,0); // this moves our cursor right back to the top left pixel.. we should talk about this. 
 display.clearDisplay(); // we need to clear our screen ever time we want to draw something new on it - other wise it will just write on top of what was there. 
 display.setTextSize(1); // this sets the text size for anything below this point - see what happens when you chnage it.
 display.setTextSize(3); // this sets the text size for anyhting below this point - see what happens when you chnage it.
 display.print(x); // this prints the variable that we set at the top to our displays memory.... the container called X that onlyy fits integrers (numbers);
 display.display(); // this prints what is in the displays memory to the screen
 if (pressed == 0) { // here we are checking to see if the boolean variable is still equal to zero and nothing has changed
 x++; // this adds one to X every time we run through the 'loop'
 if ( digitalRead(2) == HIGH) { // here we are checking to see if anyone is pressing the button right now
 pressed = 1; //if someone is pressing the buttom then change the variable 'pressed' to equal 1
 // once 'pressed' is equal to one x will no longer increment .... and so we have a stopwatch. 



Lets play a game with our new device.

  1. Create an obstacle course in the yard consisting of a number of challenges.
  2. Take turns pressing reset – completing the challenges returning to the device to press stop.

Challenge – in the first coding exercise we did to day we learnt how to wire-up and turn on an LED.  Can you work out how to add a green light and a red light to our device to tell us when to ‘GO’ and when we’ve stopped. 

Here is how you’d wire your device with two LED lights

You’ll need;

  1. A Green LED light
  2. A Red LED light
  3. Two  220 ohm resistors
  4.  wires

As this is starting to look a bit complicated its important that you think about what you are doing and not just copying. We have wired up LED’s before  – its easy-  remember

Each LED is connected by its long foot to a resistor (220 Ohm) and each short foot is connected to one of the Arduino’s digital/PWM input/outputs ports.

Like our very first examples;


I’ve simply done this twice for red and green on pins 6 & 7 and left everything else just the way it was before, button, screen and all.

Here what the hardware will look like now – it looks complicated but all we’ve added is two lights exactly the same as the diagram above.

The other trick we’ve added is to connect all the GND (grounds) through one port by connecting them all to one row on our bread board…. this is called a ‘Bus’  – because all the grounds get on the same bus to the port.


But can you work out what the code will look like yourself;

Here’s a clue it will need you to;

  1. initialise two output pins
  2. tell your code to turn on the lights in the right part of the code.

Here is a more advanced piece of code to try that adds two lights (two pins 6&7) and number of other new bits. Lets load it onto the Arduino and see if we can understand what is going on. If you don’t – it doesn’t matter. You’ve come a very long way – I’m sure you can still use this code to make a little game by adding your own question and answer.