/* Glitch Shoes - first test code Multi-mode moving LED light pattern 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/ */ // Define the pins that we're using for our hardware int pin1 = 9; int pin2 = 10; int pin3 = 11; int pin4 = 3; int pinButton = 12; // Some constants for adjusting the speed of the effects int speed = 150; int speed2 = 150; int flashDelay = 40; // Ignore button presses within one second of the previous press int debounce = 1000; // The current effects mode int mode = 0; void setup() { // Be sure the on-board LED is off pinMode(13, OUTPUT); digitalWrite(13, 0); // Set our LED lines to be outputs pinMode(pin1, OUTPUT); pinMode(pin2, OUTPUT); pinMode(pin3, OUTPUT); pinMode(pin4, OUTPUT); // And configure our mode-switching button pinMode(pinButton, INPUT); digitalWrite(pinButton, HIGH); // Turn on the serial port for debug text Serial.begin(9600); } // Main program loop void loop() { // If the button is down, then step to the next effects mode if (buttonPressed()) { mode = mode + 1; } Serial.println(mode); // Debug trace // Call a specific function depending on what effects mode we're in switch (mode) { case 0: ledWave(0); // Lights move one way break; case 1: ledWave(1); // Lights move the other way break; case 2: ledWave(0); // Lights move back & forth ledWave(1); break; case 3: ledFlash(); // Flash all the lights break; default: mode = 0; // We've reached a mode that doesn't exist so reset back to 0 break; } } // end of the loop() // Is the button pressed? bool buttonPressed() { static unsigned long lastPressTime = 0; // Do a bit of time checking to make sure we don't count a double-bounce // as two button presses if ((digitalRead(pinButton) == LOW) && millis() > (lastPressTime + debounce)) { lastPressTime = millis(); return (true); // button is pressed } else { return (false); // button isn't pressed } } // A simple flash: all the lights off, pause, all the lights on, pause void ledFlash() { digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); delay(flashDelay); digitalWrite(pin1, HIGH); digitalWrite(pin2, HIGH); digitalWrite(pin3, HIGH); digitalWrite(pin4, HIGH); delay(flashDelay); } // A simple wave of light: turn on each light in turn with a pause between each // Go up the line or down the line depending on the parameter void ledWave(int direction) { if (direction == 0) { digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, HIGH); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, HIGH); digitalWrite(pin4, LOW); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, HIGH); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); } else { digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, HIGH); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, HIGH); digitalWrite(pin4, LOW); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, HIGH); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); delay(speed); digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin3, LOW); delay(speed); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); digitalWrite(pin3, LOW); digitalWrite(pin4, LOW); } delay(speed2); }