Ladder Game

This is a game which I first encountered many years ago as a pure hardware project consisting of some discrete components, a 555 timer an analogue to digital converter, an LED bargraph driver IC, a dozen LEDs and a button. Actually, I don’t recall the exact components, but I recall the game… It was published in one of the many magazines at the time; either Everyday Electronics, Practical Electronics or Elektor Magazine. (If anyone else remembers it, please let me know!)

The idea is to climb out of a well using a ladder. There is a flashing LED representing your position on the ladder, but you can only move when the LED is on. If you push the button to try to move up when the LED is off, then you slide back down to the bottom again… The hard part is that the led flashes less and less the higher you get up the ladder, making it easier to keep the button pushed for too long when near the top, causing you to slide back down again. Additionally, if you don’t move by pushing the button then eventually you start to slide down anyway…

It’s a very simple game and was educational to build at the time, now to re-create it in hardware and software…

Simulating the circuit in software is in some-ways harder to do than to actually built the circuit in electronic components, but in can be used as an introduction to building up simple RC networks in a teaching environment.

To build the game, you’ll need almost all the bits in the SK Pang starter kit – 12 x LEDs, 12 x 270Ω resistors and one button. You can build it with less LEDs if you don’t have enough.

Ladder Game - Board Photo

Ladder Game – Board Photo

As you can see, I’ve changed the layout slightly from the way the Tux Crossing board was laid out. I’ve put the resistors on the other side of the LEDs and use short jumper wires to connect the LEDs to the 0v rail. The only reason for this was to make it look a little better to get the jumper wires away from the LEDs. I’ve also used a button out of my own bits & pieces box – The tiny buttons that come with the starter kit are OK, but I find them a little fiddly to use.

View from directly above

View from directly above

Ladder Game - Breadboard Layout

Ladder Game – Breadboard Layout

In these views you can see the layout a little clearer. the LEDs are staggered as they’re just a little too large to fit in a row, and they’re actually rotated thorough 45 degrees as the pin spacing is just a bit wide for the breadboard sockets. If you use 3mm LEDs you’ll probably be OK, but I only had 5mm LEDs to hand.

The board is wired up as follows – From top to bottom:

LED wiringPi Pin GPIO Pin
1 (Green) 0 17
2 1 18
3 2 21
4 3 22
5 (Yellow) 4 23
6 5 24
7 6 25
8 7 4
9 (Red) 10 8
10 11 7
11 12 10
12 13 9
Button 8 0

Once built, you can use the gpio command and/or the following program to make sure all the connections are correct:

#!/bin/bash

setup ()
{
  echo Setup
  for i in 0 1 2 3 4 5 6 7 10 11 12 13 ; do gpio mode  $i out ; done
}

setup
while true; do
  for i in 0 1 2 3 4 5 6 7 10 11 12 13 ; do
    gpio write $i   0
    sleep 0.1
  done
  for i in 0 1 2 3 4 5 6 7 10 11 12 13 ; do
    gpio write $i   1
    sleep 0.1
  done
done

Copy this into a file – e.g. test.sh, then chmod +x test.sh ; ./test.sh

Test the button as before:

gpio mode 8 in
gpio read 8

Now that we have the hardware, we can have a look at the theory of operation, then finally the software.

Or check the video demo…

And if at first you don’t win, try again:

Comments

Ladder Game — 24 Comments

  1. Gordon – you are using a ‘gpio’ command – where has that come from? because its not on either of the Debian distros

    • It’s part of my wiringPi package. If you get the wiringPi and install it, then install the gpio command too.
      See this page:
      wiringPi
      to fetch it and install it.

  2. Hi Gordon,
    This is great! WiringPi gives us what we really need for fast switching of the pins. I tried a different solution for Python but I don’t get the performance I need, probably because it goes via the OS file system: I don’t think it is a Python (interpreter) problem.
    You have a couple of typos in the pin assignments (they do not agree with your “pins” page). Everything else agrees with the schematic (17 Apr 2012 version).
    WiringPi/GPIO
    11/8 should read 11/7
    13/19 should read 13/9
    Thanks.
    John.

    • Yes. I realise now that I’d goofed on that page – I remember doing an edit to it, then I must have forgotten to save. Building up a 2nd ladder game right now, so will make sure it’s correct!

      I have a BASIC interpreter that I wrote recently which runs on the Pi and it has GPIO access functions (via the wiringPi library) – when I did some crude benchmarks it was marginally slower than Python, but it will be intersting to see if this sort of thing can be done in an interpreted language.

      Thanks,

      -Gordon

      • Hi Gordon,
        I was very interested in your recent comment in which you wrote:
        “have a BASIC interpreter that I wrote recently which runs on the Pi and it has GPIO access functions (via the wiringPi library)”…
        **End-quote**

        I wanted to ask if the BASIC interpreter would be available for pi users at some point in the future? As a 57 year old enthusiast I still have some confidence in using BASIC. I am also learning to use Python and respect its many advantages but BASIC might offer a way of re-coding the many odd bits of code and ideas I had in the past.

        In any event, thanks for posting your ladder-game project and code, the practical information on the use of the GPIO will also be very handy.

        Best wishes,

        Des.

  3. Hi, just purchased the SKpang kit and have set up your ladder game. I absolutely love it… My six year old will have loads of fun with this as well as teaching me a new scripting language (c).
    I am a complete noobie to this type of thing and have one question:

    I notice that if I run the script and play the game, once I have finished is there any way to kill the process. I have tried using task manager in Debian, but it won’t allow me to kill the process (I am also new to Linux lol). Is there anything that can be written into the script so that if you win the game it will then kill it???
    I only say this because I stupidly started the script again and the two processes kept conflicting, ruining the game. I then had to restart the Pi.

    Please tell me if i’m being stupid and I hope that you can help. 🙂

    Andy

    • Glad your having fun!

      If you start the game with:

      sudo ./ladder

      then in the same window, you can type a Control-C to kill the game.

      If you want the game to exit on a win, then edit the file ladder.c, and look for the call to

      winningLeds () ;

      near the end of the program, and insert this:

      return 0 ;

      on the line after it, and re-compile the program.

      -Gordon

  4. Having just moved over to Raspian “Wheezy” from the “Squeeze” distro I found that the binary didn’t work (the ladder wouldn’t climb when the button was pushed). I suspect a timing issue (versus the optimisations in the new distro), but it’s not obvious from the source code how that would come about. A recompile fixed things.

    • Actually, I’d fully exepect a recompile to be needed. Might have to make a point of mentioning that when people upgrade. Surprised the binary worked at all, really!
      Glad you’re sorted now though.

      Probably wan’t a timing issue – please do have a look at the code though – it should use various system calls to do the timings, etc.

      Cheers,
      -Gordon

  5. I would like to build this game with my grandson, but am unsure of some of the electrical connections:
    1. Does the blue lead in the photo connect with the 0v (ground) pin on the pi?
    2. What connections does the button make?
    Sorry if these are very simple questions, but as you may guess, I am a novice.

    • Hi,
      Yes, it’s not terribly clear and I need to find a better way to represent these things.
      In this photo:

      Ladder

      There is a blue male-female jumper lead going to the 0v pin on the Pi to the 2nd top of the vertical blue-line. This is jumpered over to the other side of the board by the green jumper wire.

      The button has a short green jumper that goes to the 0v rail (vertical blue line) and the black wire goes to BCM-GPIO pin 0 (SDA0), which is wiringPi pin 8. Physically, it’s the 2nd one down on the left side of the GPIO connector.

      Hope this helps!

      -Gordon

  6. Hi Gordon,
    Thanks again for the turorials, they have been really helpful.
    It’s been years since I studied c, can you recommend any good online tutorials for beginners like me?
    Thanks,
    Robin

    • It’s been 30+ years since I started in C myself, so I’m not that familiar with any online tutorials – sorry! I’m there there are plenty though… Or just start to read programs – I have a relatively easy on the eye way of coding, so start to read my programs, I’m sure it’ll all start to come back to you!

      -Gordon

  7. Hi Gordon!
    Unfortunately my starter kit only came with 10 M/F jumper cables, so that limited the amount of LEDs I could use.
    How do I udjust the program to account for this?
    I’ve now got only 8 LEDs.

    • If you fetch the code for the new ladder board, it includes a version of the ladder.c program that has a #define at the top which sets the number of LEDs – change this to 8 and it will work just fine.

      -Gordon

  8. Hi, I was recently given a Pi for my 14th Birthday. I have tried a number of projects using hardware such as LEDs, resistors and switches, but everything I have done so far used Python coding language. I have wired up my bread board following the instructions for the Ladder Game, and have copied the coding to test the board. However when I type into the LX Termanal the command “chmod +x test.sh ; ./test.sh” I get an infinite stream of error messages telling me that in line 6, 12, and 16 no GPIO commands were found.
    I would appreciate your help,
    Thank you.

    • You need to install the wiringPi libraries and support program.

      Do this:

      cd
      git clone git://git.drogon.net/wiringPi
      cd wiringPi
      ./build

      That should do it – and you can then run the ladder tests, etc. which use the gpio program which is part of wiringPi (it lets you play with the GPIO pins from the command-line).

      -Gordon