Skip to content

Instantly share code, notes, and snippets.

@dundee
Created December 22, 2011 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dundee/1508578 to your computer and use it in GitHub Desktop.
Save dundee/1508578 to your computer and use it in GitHub Desktop.
X-Mass Arduino sketch
/**
* X-Mas Arduino sketch
* Uses 11 LEDs (digital pin 3 to pin 13) and one switch (digital pin 0)
*/
int switchPin = 0; // Switch connected to digital pin 0
int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3};
int ledCount = 11;
int state;
int buttonState;
unsigned long currentMillis, lastMillis;
void turnOn(int led)
{
digitalWrite(ledPins[led], HIGH);
}
void turnOff(int led)
{
digitalWrite(ledPins[led], LOW);
}
void turnOnAll()
{
for (int i = 0; i < ledCount; i++) {
turnOn(i);
}
}
void turnOffAll()
{
for (int i = 0; i < ledCount; i++) {
turnOff(i);
}
}
void leftRight(int d)
{
static int direction = 0;
static int i = 0;
if (direction == 0) {
if (i < ledCount) {
turnOffAll();
turnOn(i);
delay(d);
i++;
} else {
direction = 1;
i = ledCount - 1;
}
} else if (i >= 0) {
turnOffAll();
turnOn(i);
delay(d);
i--;
} else {
direction = 0;
i = 0;
}
}
void fromMiddle(int d)
{
static int half = ledCount / 2;
static int i = 0;
static int direction = 0;
if (direction == 0) {
if (half - i >= 0 && half + i < ledCount) {
turnOn(half - i);
turnOn(half + i);
delay(d);
i++;
} else {
direction = 1;
i = 0;
}
} else {
if (half - i >= 0 && half + i < ledCount) {
turnOff(half - i);
turnOff(half + i);
delay(d);
i++;
} else {
i = 0;
direction = 0;
}
}
}
void twoPivots(int d)
{
static int i = 0;
static int j = ledCount - 1;
if (i < ledCount && j >= 0) {
turnOffAll();
turnOn(i);
turnOn(j);
delay(d);
i++;
j--;
} else {
i = 0;
j = ledCount - 1;
}
}
void progressBar(int d)
{
static int i = 0;
static int direction = 0;
if (direction == 0) {
if (i < ledCount) {
turnOn(i);
delay(d);
i++;
} else {
direction = 1;
i = ledCount - 1;
}
} else {
if (i > 0) {
turnOff(i);
delay(d);
i--;
} else {
direction = 0;
i = 0;
}
}
}
void invertedLeftRight(int d)
{
static int direction = 0;
static int i = 0;
if (direction == 0) {
if (i < ledCount) {
turnOnAll();
turnOff(i);
delay(d);
i++;
} else {
direction = 1;
i = ledCount - 1;
}
} else if (i >= 0) {
turnOnAll();
turnOff(i);
delay(d);
i--;
} else {
direction = 0;
i = 0;
}
}
void stepByStep(int d)
{
static int offset = 0;
for (int i = 0; i < ledCount; i++) {
if ((i + offset) & 1) {
turnOn(i);
} else {
turnOff(i);
}
}
delay(d);
offset++;
offset %= 2;
}
void tetris(int d)
{
static int i = 0;
static int j = ledCount;
if (j < ledCount) {
if (i <= ledCount - j) {
if (i - 1 >= 0) turnOff(i - 1);
turnOn(i);
i++;
delay(d);
} else {
turnOn(ledCount - j);
j++;
i = 0;
delay(d);
}
} else {
j = 1;
turnOffAll();
}
}
void incrementor(int d)
{
static int num;
turnOffAll();
if (num <= (1 << ledCount) - 1) {
for (int i = 0; i < ledCount; i++) {
if ((1 << i) & num) {
turnOn(i);
}
}
} else {
num = 0;
}
delay(d);
num++;
}
void setup()
{
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
state = 0;
}
void loop()
{
int val, val2;
int modeCount = 8;
int defaultDelay = 200;
val = digitalRead(switchPin);
delay(10);
val2 = digitalRead(switchPin);
if (val == val2) {
if (buttonState != val) {
if (val == LOW) {
state++;
state %= modeCount;
turnOffAll();
}
buttonState = val;
}
}
currentMillis = millis();
if (currentMillis - lastMillis > 30000) {
state++;
state %= modeCount;
lastMillis = currentMillis;
turnOffAll();
}
switch(state) {
case 0:
leftRight(defaultDelay);
break;
case 1:
fromMiddle(defaultDelay);
break;
case 2:
twoPivots(defaultDelay);
break;
case 3:
progressBar(defaultDelay);
break;
case 4:
invertedLeftRight(defaultDelay);
break;
case 5:
stepByStep(defaultDelay);
break;
case 6:
tetris(defaultDelay);
break;
case 7:
incrementor(defaultDelay);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment