3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Arduino and Touchpad Tic Tac Toe

Step 3Addressing the LED Matrix

For the tic tac toe code, we'll need to be able to store the following information about the LEDs:
- whether an LED is lit or not
- if lit, whether it's red or green

One way of doing this is to store the state in a 9-cell array, using three digits to represent the state (0 = off, 1 = red on, 2 = green on).  Everytime we need to check on the states of the LED, for example, to check if there's a win condition, we'll need to cycle through the array.  This is a workable method, but rather clunky.

A more streamlined method would be to use two groups of nine bits.  The first group of nine bits stores the on-off status of the LEDs, and the second group of nine bits stores the colour.  Then, manipulating the LED states simply becomes a matter of bit arithmetic and shifting.

Here's a worked example.  Let's say we draw our tic tac toe grid graphically, and first use 1s and 0s to represent the on-off status (1 is on, 0 is off):

000
000  = matrix with bottom left LED lit
100

100
010 = matrix with diagonal LEDs lit
001

If we enumerate the cells from the bottom left, we can write the above representations as a series of bits.  In the first case, that would be 100000000, and in the second case, it would be 001010100.  If we think of these as binary representations, then each series of bits can be condensed into a single number (256 in the first case, 84 in the second case).  So instead of using an array to store the state of the matrix, we can just use a single number!

Similarly, we can represent the colour of the LED in the same way (1 is red, 0 is green).  Let's first assume all the LEDs are lit (so the on-off status is represented by 511).  The matrix below will then represent the colour state of the LEDs:

010   green, red, green
101   red, green, red
010   green, red, green

Now, when displaying the LED matrix, we just have to cycle through each of the bits, first in the on-off state, and then in the colour state.  For example, let's say our on-off state is 100100100, and the colour state is 010101010.  Here's our algorithm for lighting up the LED matrix:

Step 1.  Do a bitwise addition of the on-off state with a binary 1 (ie bit masking).
Step 2.  If it's true, the LED is lit.  Do now a bitwise addition of the colour state with a binary 1.
Step 3.  If it's true, light up the red LED.  If it's false, light up the green LED.
Step 4.  Shift both the on-off state and colour state, one bit to the right (ie bit shifting).
Step 5.  Repeat Steps 1 - 4 until all nine bits have been read.

Note that we're filling the matrix backwards - we start with cell 9, then proceed back down to cell 1. 

Also, the on-off and colour states are stored as an unsigned integer type (word) instead of a signed integer type.  That's because in bit shifting, if we're not careful, we might inadvertenly change the sign of the variable.

Attached is the code for lighting up the LED matrix.

« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
3
Author:origamiwolf