/* Glitch Shoes - final code using arrays for pattern storage, binary for LED settings and EEPROM for mode storage Multi-mode moving LED light patterns Author: Jon Jennings http://jonjennings.org/ Date: November 2011 Licence: This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-sa/3.0/ */ // Include the library for writing to the non-volatile memory #include // Store the pin numbers of the LEDs in an array for easy access int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12 }; // EEPROM address used to store the effects mode int modeAddress = 0; // Store the total number of steps in the pattern array here int numSteps = 0; // Store the total number of LEDs in the LED array here int numLeds = 0; // LED patterns // col1: effects mode that this line refers to // col2: number of milliseconds to hold this pattern // col3: binary bit-field for which LEDs are on/off in this pattern int pattern[][3] = { { 0,9000, B00000000}, // mode 0: Everything off { 1, 80, B10000000}, // mode 1: 2 LEDs run down the length of the line { 1, 80, B11000000}, { 1, 80, B01100000}, { 1, 80, B00110000}, { 1, 80, B00010000}, { 1, 80, B00001000}, { 1, 80, B00001100}, { 1, 80, B00000110}, { 1, 80, B00000011}, { 1, 80, B00000001}, { 2, 20, B11110000}, // mode 2: simulated heartbeat - two fast flashes followed by a pause { 2, 20, B00001111}, { 2, 120, B00000000}, { 2, 40, B11110000}, { 2, 40, B00001111}, { 2,1860, B00000000}, { 3, 100, B10001000}, // mode 3: 1 LED runs down the length of the line { 3, 100, B01000100}, { 3, 100, B00100010}, { 3, 100, B00010001}, { 4, 150, B10001000}, // mode 4: one LED runs down the length of the line & then back again { 4, 150, B01000100}, { 4, 150, B00100010}, { 4, 150, B00010001}, { 4, 150, B00100010}, { 4, 150, B01000100}, { 5, 300, B11110000}, // mode 5: all LEDs flash on/off together { 5, 300, B00001111} }; // Store the requested effects mode in the EEPROM void modeSet(int mode) { EEPROM.write(modeAddress, mode); } // Read the effects mode out of the EEPROM int modeGet() { return (EEPROM.read(modeAddress)); } // Initial setup code run after the Lilypad is reset void setup() { // Increment mode on reset/startup modeSet(modeGet()+1); // Be sure the on-board LED is off pinMode(13, OUTPUT); digitalWrite(13, LOW); // Set our LED pins to all be outputs and turn them off for now for (int pin = 0 ; pin < sizeof(ledPins)/sizeof(ledPins[0]) ; pin++) { pinMode(ledPins[pin], OUTPUT); digitalWrite(ledPins[pin], LOW); } // Calculate and store the side of the patterns array and the LED array // Doing this means the code doesn't need to be explicitly told how many entries there are // in each array and so makes it simpler to extend or change the arrays in the future numSteps = sizeof(pattern)/sizeof(pattern[0]); numLeds = sizeof(ledPins)/sizeof(ledPins[0]); // Turn on the serial port for debug text Serial.begin(9600); Serial.print("LED count: "); Serial.println(numLeds); } // Main program loop - called repeatedly void loop() { // pointer into the pattern array for tracking which pattern line we're currently at static int currentStep = 0; // remember the time that we're supposed to next change the pattern static unsigned long nextChangeTime = 0; // Don't do anything unless it's time to change our displayed LED pattern if (millis() > nextChangeTime) { int startStep = currentStep; // Remember where we started from in the pattern array // Loop through the array looking for the next pattern line that matches our // current effects mode while (true) { currentStep++; // move to next step if (currentStep >= numSteps) currentStep=0; // at end of array, loop back to start if (pattern[currentStep][0] == modeGet()) break; // this pattern line applies to this mode, so use it if (currentStep == startStep) { // if been all the way around, we have nothing for this mode modeSet(0); // so reset the mode back to the first mode currentStep = -1; // and start searching from the beginning of the pattern array Serial.println("Looped around - clearing mode"); } } // Make a note of the time when we're going to have to change the pattern again nextChangeTime = millis()+pattern[currentStep][1]; // Now, at last, set the LEDs to display the current pattern for (int led=0 ; led < numLeds ; led++) { digitalWrite(ledPins[led], bitRead(pattern[currentStep][2], led)); } } } // end of the loop()