Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// MadHat - Lilypad Simple based LED hat.
//
// Makes use of Charlieplexing, due to the limited number of pins available.
//
class LEDPlex {
public:
LEDPlex(int numPins, const int pins[], const char * patterns);
void light(int ledNo) const;
private:
int fNumPins;
const int * fPins;
const char * fPatterns;
};
LEDPlex::LEDPlex(int numPins, const int pins[], const char * patterns)
: fNumPins(numPins), fPins(pins), fPatterns(patterns)
{
}
void LEDPlex::light(int ledNo) const
{
const char * pat = ledNo < 0 ? "xxxxxxxxxxxxx" : fPatterns+(ledNo*fNumPins);
for (int pin=0; pin<fNumPins; ++pin)
switch (*pat++) {
case '1':
pinMode(fPins[pin], OUTPUT);
digitalWrite(fPins[pin], HIGH);
break;
case '0':
pinMode(fPins[pin], OUTPUT);
digitalWrite(fPins[pin], LOW);
break;
default:
pinMode(fPins[pin], INPUT);
digitalWrite(fPins[pin], LOW);
break;
}
}
const int gRedPins[] = {10, 11, A2};
const LEDPlex gRedLEDs(3, gRedPins, "10x" "0x1" "1x0" "x10");
const int kRedSequenceSz = 5;
const int gRedSequence[kRedSequenceSz] = {0, 2, 1, 3, -1};
const int gBlueGreenPins[] = {5, 6, 9, A3};
const LEDPlex gBlueGreenLEDs(4, gBlueGreenPins, "1000" "0100" "0010" "0001");
const int gBlueGreenSequence[] = {0, 1, 2, 3, 1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2, 0, 3, 2, 1, 3, 2, 1, 0, 2, 1, 0, 3, 1, 0, 3, 2};
const int kBlueGreenSequenceSz = sizeof(gBlueGreenSequence) / sizeof(int);
int gRedSpeed = 20;
int gBlueGreenSpeed = 3;
void setup()
{
}
void loop()
{
static int sRedIx = 0;
static int sBGIx = 0;
static int sRedTick = 0;
static int sBGTick = 0;
if (millis() > 1800000L) {
//
// Standby
//
gRedLEDs.light(-1);
gBlueGreenLEDs.light(-1);
delay(10000);
return;
}
if (!sRedTick) {
gRedLEDs.light(gRedSequence[sRedIx++]);
sRedIx %= kRedSequenceSz;
}
if (!sBGTick) {
gBlueGreenLEDs.light(gBlueGreenSequence[sBGIx++]);
sBGIx %= kBlueGreenSequenceSz;
}
if (sRedTick++ == gRedSpeed)
sRedTick = 0;
if (sBGTick++ == gBlueGreenSpeed)
sBGTick = 0;
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment