# Tap Code

## Introduction

Tap code (or knock code) is a simple way to encode text messages using co-ordinates. It has been used by prisoners to communicate by tapping on walls, pipes or bars. A reference is made to this code in Arthur Koestler's novel Darkness At Noon where political prisoners communicate with one another whilst held in isolation. It would also be possible to tap out a message discretely on the thigh or foot of a person sat next to you, even if the 'enemy' is within hearing distance. It only requires a single sound, no difference between long and short taps and was easy to remember. Any way that you could reliably communicate the numbers 1 to 5, audibly or visibly would work for this. The ability to communicate was significant for prisoners of war not only for morale but also in being able to preserve the chain of command whilst being held prisoner.

## The Code

Tap code is based on the following Polybius square.

 1 2 3 4 5 1 A B C/K D E 2 F G H I J 3 L M N O P 4 Q R S T U 5 V W X Y Z

## Programming Tap Code

Whatever you are going to do with this on a computer, you're going to need a data structure to store the square. At first thought, you might go with a 2 dimensional array. That's great when it comes to writing out the letter at a particular co-ordinate but not so cool when it comes to looking things up. You can use a string to store a square as long as you can make translations between the single number that identifies the position of a character within the string and the two numbers that represent the position of the character in a notional 2 dimensional grid space.

The key information is shown below. The index is the position of the letter in our 25 character string with the 'K' left out. We need to work out the relationship between the numbers in each column.

ABCDEFGHIJLMNOPQRSTUVWXYZ
Index0123456789101112131415161718192021222324
Row1111122222333334444455555
Column1234512345123451234512345

The key is division by 5. The following pseudocode describes how you might look up the co-ordinates of a number given its position. There is also an example of how to find the position of a character given the row and column.

```square ← "ABCDEFGHIJLMNOPQRSTUVWXYZ" letter ← "M" position ← square.INDEXOF(letter) row ← (position DIV 5) + 1 column ← (position MOD 5) + 1 calculated_position ← (row - 1) * 5 + column - 1```

The word DIV is used to represent integer division. Some languages use the back slash for this. If you divide the position of a character by 5, ignoring any remainder, and add 1, you get the number of the row.

The word MOD is the remainder when the position is divided by 5. Add 1 to this and you get the column number.

Once the cipher alphabet can be written out like this, the remaining techniques needed are outlined in the arbitrary substitution page.

## Challenges

One goal for a program might be to turn text entered by the user into tap code represented in notes, buzzes or lights going on and off.

You could get an Arduino to do the encoding for you and use a buzzer to output the values. Alternatively, get the Arduino to deal with decoding a message that is input using some hardware. You could do the whole thing with a single pushbutton and output the values to the serial port. If you are using a Leonardo, you could use the keyboard library to send the character to the keyboard. This would be a nice project to use with capacitive sensing or with a Makey Makey and a Scratch program. You could make a knock sensor using a piezo element and a high value resistor or use a light sensor as your on/off switch. The main thing is to work out the start and stops of your signals and filter out any 'noise' in the input.

You might want to use a slightly enlarged square for your implementation allowing all of the characters from the alphabet and digits too. You'll need to adjust the formulas to account for the extra row and column but can still get there.

 1 2 3 4 5 6 1 A B C D E F 2 G H I J K L 3 M N O P Q R 4 S T U V W X 5 Y Z 0 1 2 3 6 4 5 6 7 8 9