Charlieplexing

Someone at work mentioned charlieplexing, which I thought was rather cool.  And potentially useful for debugging things on the ATTiny where you don’t have many pins.

So in a break from playing with the Scalextric bits, I’ve made up a little board with 6 LEDs and a 3 pin header, using a small circular proto board from proto-pic:

And here it is in action:

The code would be very straightforward if I hadn’t overused the ?: operator and bit operations.  Here it is for you to puzzle over:

  for (int j = 0; j < 64; j++)
  {
    for (int k = 0; k < 100; k++)
    {
      for (int i = 0; i < 6; i++)
      {
        if (j & (1 << i))
        {
          pinMode(5, (i < 4) ? OUTPUT : INPUT);
          pinMode(6, ((i < 2) || (i >= 4)) ? OUTPUT : INPUT);
          pinMode(7, (i >= 2) ? OUTPUT : INPUT);
          digitalWrite(5, (i & 1) ? HIGH : LOW);
          digitalWrite(6, (i & 1) ? LOW : HIGH);
          digitalWrite(7, (((i & 4) >> 2) ^ (i & 1)) ? LOW : HIGH);
        }
        else
        {
          pinMode(5, INPUT);
          pinMode(7, INPUT);
          pinMode(6, INPUT);
        }
        delay(1);
      }
    }
  }

The outer loop selects one of the 64 possible combinations of the 6 LEDs.

The inner loop lights or does not light each LED in turn.  Because it cycles through the LEDs fast enough all the lit LEDs appear lit simultaneously.

The middle loop just runs round lighting the same set of LEDs hundred times before moving onto the next set.

Comments are closed.