Hello all! I'm new to arduino and coding in general and for my first project I decided to make a simple one button macro keyboard that runs a loop indefinitely until the button is pressed again, with the objective of controlling a character in an online videogame. The code mostly works, but eventually key presses start going out of order which messes up the entire sequence requiring me to stop and start the macro.
The macro is supposed to press "1" three times, hold alt and press "q", "w", "e", release alt, press "c", press enter (RETURN), release enter and loop until the button is pressed again. If you guys have any idea why the key presses start getting out of order after a while it would be much appreciated.
#define pinBotao 9
#include <Keyboard.h>
boolean botaoAtu = true;
boolean botaoAnt = true;
boolean estadoMacro = false;
static unsigned long intervaloMacro = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pinBotao, INPUT_PULLUP);
Keyboard.begin();
}
void loop() {
// put your main code here, to run repeatedly:
botaoAtu = digitalRead(pinBotao);
delay(1);
if (!botaoAtu && botaoAnt){
Keyboard.releaseAll();
intervaloMacro = millis();
estadoMacro = !estadoMacro;
}
botaoAnt = botaoAtu;
if (estadoMacro){
iniciodamacro:
if ((millis() - intervaloMacro) == 1000){
Keyboard.write('1');
delay(1);
}
if ((millis() - intervaloMacro) == 2000){
Keyboard.write('1');
delay(1);
}
if ((millis() - intervaloMacro) == 3000){
Keyboard.write('1');
delay(1);
}
if ((millis() - intervaloMacro) == 3300){
Keyboard.press(130);
delay(1);
}
if ((millis() - intervaloMacro) == 3600){
Keyboard.write('q');
delay(1);
}
if ((millis() - intervaloMacro) == 4300){
Keyboard.write('w');
delay(1);
}
if ((millis() - intervaloMacro) == 4600){
Keyboard.write('e');
delay(1);
}
if ((millis() - intervaloMacro) == 4900) {
Keyboard.release(130);
delay(1);
}
if ((millis() - intervaloMacro) == 5400){
Keyboard.write('c');
delay(1);
}
if ((millis() - intervaloMacro) == 5800){
Keyboard.press(176); //código ascii é sem aspa
delay(1);
}
if ((millis() - intervaloMacro) == 6100){
Keyboard.release(176);
delay(1);
}
if ((millis() - intervaloMacro) >= 6400){
intervaloMacro = millis();
delay(1);
goto iniciodamacro;
}
}
}