Recording a sequence, then replaying

hello! i have an arduino pro micro, wired to 2 buttons. im attempting to record, from the board starting up button presses on button 1. the arduino scans for button presses, and when button 1 is pressed it stores it in an unsigned int array. the lengh of the array is 300. the data stored in the array is the gap between button presses. after im done recording button presses on button 1, button 2 is pressed. this tells the arduino to play back the recorded button presses exactly as how i pressed them. my problem is that it just doesnt feel like its playing it back exactly how i pressed the buttons. anyone care to take a look? code below

//#include <Mouse.h>
//#include <Keyboard.h>
unsigned int place[320];
int fly[640];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}
int presscount = 0;
int presscheck = 0;
int spacecount = 0;

void loop() {
  ////////////////////////////////
  pinMode(6, INPUT_PULLUP);

  if (digitalRead(6) == LOW) {
    if (presscheck == 1) {

      // Mouse.press(MOUSE_RIGHT);
      place[presscount] = millis();
      Serial.println( place[presscount]);
      presscount = presscount + 1;
      delay(70);
      presscheck = 2;

    }

  }
  else {
    if (digitalRead(6) == HIGH) {
      // Mouse.release(MOUSE_RIGHT);
      presscheck = 1;
    }
  }

  pinMode(5, INPUT_PULLUP);
  if (digitalRead(5) == LOW) {


    for (int z = 0; z <= presscount; z = z + 1) {
      int flag = millis();
      int x = place[z];
      while (millis() < flag + x) {}
      Serial.println(place[z]);

    }
  }
}
















Millis() ALWAYS returns a long, not an int. All storage of the results must also be LONG.
Set you pin assignments only ONCE and in the setup() code.
Paul

therefore i should change int flag to unsigned long flag
??

Probably.
Paul

1920 bytes of your 2048 bytes of RAM.

still got the same issue :frowning_face: this is my modified code

unsigned long place[320];
int fly[640];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}
unsigned long presscount = 0;
int presscheck = 0;
int spacecount = 0;

void loop() {
  ////////////////////////////////
  pinMode(6, INPUT_PULLUP);

  if (digitalRead(6) == LOW) {
    if (presscheck == 1) {

      // Mouse.press(MOUSE_RIGHT);
      place[presscount] = millis();
      Serial.println( place[presscount]);
      presscount = presscount + 1;
      delay(70);
      presscheck = 2;

    }

  }
  else {
    if (digitalRead(6) == HIGH) {
      // Mouse.release(MOUSE_RIGHT);
      presscheck = 1;
    }
  }

  pinMode(5, INPUT_PULLUP);
  if (digitalRead(5) == LOW) {


    for (int z = 0; z <= presscount; z = z + 1) {
      unsigned long flag = millis();
      int x = place[z];
   
      while (millis() < flag + x) {}
      Serial.println(place[z]);

    }
  }
}
















is that an issue? i wouldnt imagine it slowing down the 32u4 too much. im used to beefy esp8266

Sorry, 2.5k bytes. Still tight.

Slowing down?

for (int z = 0; z <= presscount; z = z + 1)

Why the extra loop?

What's a beefy 8266?

the loop means that is checks every value in the array. an esp8266 is a wifi board the size of a arduino nano with 4MB of storage.

...and then another

yep, thats the idea

No it is not. You are storing the millis() value:

place[presscount] = millis();

What you actually want to store is the DIFFERENCE in the millis() value from the last time the button was pressed.

The idea is to check an array element that hasn't been filled in yet?
Or have I missed something?

Look closely:

for (int z = 0; z <= presscount; z = z + 1)

This is wrong:

    for (int z = 0; z <= presscount; z = z + 1) {
      unsigned long flag = millis();
      int x = place[z];
      while (millis() < flag + x) {}
      Serial.println(place[z]);
    }

should be:

    for (int z = 0; z < presscount; z = z + 1) {
      unsigned long flag = millis();
      unsigned long x = place[z];
      while (millis() - flag <  x) {}
      Serial.println(place[z]);
    }

but actually the same as:

    for (int z = 0; z < presscount; z++) {
      delay(place[z]);
      Serial.println(place[z]);
    }