#define QUICK 100
bool autoautoMode = true;
void switchMode() {
auto =autoMode= !auto;autoMode;
for (unsigned int i = 0; i < 25; i++) {
digitalWrite(LED_PIN, HIGH);
delay(QUICK);
digitalWrite(LED_PIN, LOW);
delay(QUICK);
}
}
#define LONGPRESS 500
unsigned long elapsed;
bool clicked = false;
unsigned long duration;
void loop() {
if (autoautoMode) {
sendSOS();
}
elapsed = millis();
while(clicked || millis() - elapsed <= SPACE) {
// keep waiting to emit a SPACE or until the button is released
if (digitalRead(BTN_PIN)) {
// button has been clicked: trace it and start counting time
clicked = true;
duration = millis();
} else if (clicked) {
// button has been released: stop tracing and verify click duration
clicked = false;
if (millis() - duration <= LONGPRESS) {
// switch mode on long press
switchMode();
} else if (!autoautoMode) {
// short press has a meaning only when not in auto mode
sendSOS();
}
}
}
UPDATE: I've added the inter-letter pause as suggested in the comments.
UPDATE: apparently auto
is a reserved word, so I changed it to autoMode
.