Recording pattern of button presses and replaying pattern with LED blinks

Hello everyone,

I am trying to generate code that follows this logic:

  1. Button #1 is pressed and released multiple times consecutively
  2. This series of button presses and releases are stored
  3. Button #2 is pressed and takes the pattern of button presses/releases time values in chronological order and replays them in the form of LED on/off blinks

I am worried that the offDuration[25] array will quickly fill up with the values from the first if condition and am unsure on how to get the loop to wait to move on to the next step i in the loop until both arrays have their proper values.

Also, is there a way to let the size of the array depend on the number of times the button is pressed instead of defining the size of the array before the loop begins? In my code I arbitrarily assign the arrays to a size of 25 to allow for 25 button presses, but what if you are unsure of how many times the button will be pressed?

My code so far has been included and I would really appreciate any input that anyone has.

Thanks ahead of time!

int BUTTON = 2;
int REPLAY = 3;
int LED = 13;
unsigned long startTime;
unsigned long endTime;
unsigned long offTime;
unsigned long onDuration[25]; //array given 25 rows to allow for up to 25 button presses
unsigned long offDuration[25]; //array given 25 rows to allow for up to 25 button presses

void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT);
pinMode(REPLAY, INPUT);
pinMode(LED, OUTPUT);
Serial.println("Program begin");
}

void loop() {
int prevButtonState = LOW;
int ButtonState = digitalRead(BUTTON);
int replayState = digitalRead(REPLAY);

for (int i = 0, i < 25, i++) {

if (ButtonState == LOW && prevButtonState == LOW) {
//button has not been pressed yet
offTime = millis();
}

else if (ButtonState == HIGH && prevButtonState == LOW) {
//button has been pressed

startTime = millis();
//record starting time of the button press

Serial.print("Button pressed at: ");
Serial.print("/t");
Serial.print(startTime);
Serial. println(" ms");

offDuration = offTime - startTime

  • //store the duration that the button was off before this button press*
  • Serial.print("The values stored in this row of the offDuration array is: ");*
    _ Serial.print(offDuration*);*_

* prevButtonState = ButtonState;*
* //update new value for previous button state *
* }*

* else if (ButtonState == HIGH && prevButtonState == HIGH) {*
* //the button is still being pressed*
* //do nothing*
* }*
* else if (ButtonState == LOW && prevButtonState == HIGH {*
* //button has been released*

* endTime = millis();*
* //record the time that button is released*
* Serial.print("Button released at: ");*
* Serial.print("/t");*
* Serial.print(endTime);*
* Serial.println(" ms");*

_ onDuration = endTime - startTime;
* //store the duration of time that the button was pressed for this step in the for loop*_

* Serial.print("The value of time stored in this row of onDuration array is: ");*
_ Serial.println(onDuration*);*_

* prevButtonState = ButtonState;*
* //update new value for previous button state*
* }*

* else {*
* //do nothing*
* //move to next step in the for loop*
* }*
* }*
* }*
}
[/quote]
button_test.ino (2.28 KB)

I don't see the point of the for loop. Let loop() do what it is designed for

is there a way to let the size of the array depend on the number of times the button is pressed

No.

is there a way to let the size of the array depend on the number of times the button is pressed instead of defining the size of the array before the loop begins?

In the C language you can not change the size of an array dynamically.

Your code is one great big function which makes it almost impossible to read. Break things down into functions.

Please post code in code takes not quote tags. Read the how to use this forum sticky post.

Your attached code does not compile.