char foo; //wtf some kind of black magic that makes Serial a thing /* Mimics your timing pattern with flashing light, up to MaxPushes button pushes */ // set pin numbers: const int buttonPin = 3; // the number of the pushbutton pin const int ledPin = 7; // the number of the LED pin const int GAP_TIME = 4000; // gap of how long you have to do nothing before it mimics you, set to 4 seconds const int MAX_PUSHES = 20; // maximum number of button pushes the mimicer will track // variables will change: int buttonState; // variable for reading the pushbutton status boolean turnedOn; //tracker variable to tell when button was just turned on or off boolean neverTurnedOn; //tracks ONLY the very first button push to start the official clock boolean recordFlag; //flag for determining whether we are in record or playback mode int startTime; int currTime; int elapsedTime; //these arrays will store the amount of time the button was on or off so they can be recreated. int onTimeLengths[MAX_PUSHES]; int offTimeLengths[MAX_PUSHES]; // indices for tracking on and offTimes // (yeah, I know we could use just one but thats a little confusing) int onTimeIndex; int offTimeIndex; void setup() { // variables will change: buttonState = 0; // variable for reading the pushbutton status turnedOn = false; //tracker variable to tell when button was just turned on or off neverTurnedOn = true; //tracks ONLY the very first button push to start the official clock recordFlag = true; //flag for determining whether we are in record or playback mode startTime = 0; currTime = 0; elapsedTime = 0; onTimeIndex = 0; offTimeIndex = 0; //initialize all values in timing arrays to 0 for (int i = 0; i < MAX_PUSHES; i++) { onTimeLengths[i] = 0; offTimeLengths[i] = 0; } // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH); // turn on pullup resistors } void loop(){ while (recordFlag) { record(); } playback(); setup(); digitalWrite(buttonPin, LOW); } // this function records the pattern of on's and off's into two arrays to be played back later void record(){ buttonState = digitalRead(buttonPin); if (buttonState == LOW) // means button was pressed { // if button was just pushed, start clock if (!turnedOn) { if (neverTurnedOn) //if this is the very first button push, dont track offTime { neverTurnedOn = false; } else //track offTime for all other button pushes { //measure how long button was off for currTime = millis(); elapsedTime = currTime - startTime; //store elapsed off time and increment index offTimeLengths[offTimeIndex] = elapsedTime; offTimeIndex++; } //start the clock and reset tracker variable startTime = millis(); turnedOn = true; //turn LED on while button pressed digitalWrite(ledPin, HIGH); } ; //stay the same while button is being held down } else { // button is not pushed //this code checks to see if button has been off long enough to go to playback currTime = millis(); elapsedTime = currTime - startTime; if (elapsedTime > GAP_TIME) { //turn recordFlag off and go to playback recordFlag = false; return; } if (turnedOn) //means button was just turned off { // measure how long button was on for currTime = millis(); elapsedTime = currTime - startTime; //store elapsed off time and increment index onTimeLengths[onTimeIndex] = elapsedTime; onTimeIndex++; //reset flag for next button push turnedOn = false; // turn LED off: digitalWrite(ledPin, LOW); } ; // do nothing if still unpushed } } void playback() { //temporary indices to walk through arrays int tempOnInd = 0; int tempOffInd = 0; while (tempOffInd <= offTimeIndex) { digitalWrite(ledPin, HIGH); //turn LED on delay(onTimeLengths[tempOnInd]); // wait for OnTime ms digitalWrite(ledPin, LOW); //turn LED off delay(offTimeLengths[tempOffInd]); // wait for OffTime ms tempOnInd++; tempOffInd++; } }