Skip to content

Instantly share code, notes, and snippets.

@microtherion
Created December 30, 2012 19:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save microtherion/4414439 to your computer and use it in GitHub Desktop.
//
// 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