Unit C · Telling a machine what you want
What we are making: a keypad that reports which key you pressed. The first time your machine takes real input from a person.
Sixteen keys, but only eight wires. The keypad is wired as a grid: four rows crossing four columns. When you press the key at row 2, column 3, it connects that row wire to that column wire, and nowhere else. The library checks every crossing point, hundreds of times a second, and works out which one you are holding down.
This is called matrix scanning, and it is how every computer keyboard works, including the one in front of you.
The keypad's ribbon has eight pins in a row. With the keys facing you, they go into the Arduino left to right:
A turn the built-in LED on, and B turn it off.# is pressed, then print the whole number.OPEN when it is typed in.rowPins and colPins in the code and upload again. Ten seconds, and it tells you whether that was the problem.| What you see | Why | What to do |
|---|---|---|
| Nothing prints at all | The ribbon is in backwards. | Turn the connector round and try again. |
| Every key gives the wrong character | Rows and columns are swapped. | Swap rowPins and colPins in the code, not the wiring. |
| One whole row is dead | One pin is not making contact. | Push the ribbon fully home and check that pin on the board. |
| One press prints several times | Switch bounce. | The library usually handles it. Add a short delay if it does not. |
char holds one character, in single quotes: '7'.In this session