Skip to main content
added 103 characters in body
Source Link
#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.

#define QUICK 100
bool auto = true;

void switchMode() {
  auto = !auto;
  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 (auto) {
    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 (!auto) {
        // 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.

#define QUICK 100
bool autoMode = true;

void switchMode() {
  autoMode= !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 (autoMode) {
    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 (!autoMode) {
        // 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.

There is a rollover problem only if the button is held down for 49 days.
Source Link
Edgar Bonet
  • 44.3k
  • 4
  • 41
  • 79
  • there is no de-bouncing on the button
  • the board is unresponsive while signalling
  • keeping the button pressed forever will prevent any signalling to occur in any mode
  • the code is afflicted by millis() rollover if you leavekeep the board runningbutton pressed for long periods (around 49 days)
  • there is no de-bouncing on the button
  • the board is unresponsive while signalling
  • keeping the button pressed forever will prevent any signalling to occur in any mode
  • the code is afflicted by millis() rollover if you leave the board running for long periods (around 49 days)
  • there is no de-bouncing on the button
  • the board is unresponsive while signalling
  • keeping the button pressed forever will prevent any signalling to occur in any mode
  • the code is afflicted by millis() rollover if you keep the button pressed for long periods (around 49 days)
added 191 characters in body
Source Link
#define UNIT 300
#define SHORT (UNIT)
#define LONG (UNIT*3)
#define PAUSE (UNIT*3)
#define SPACE (UNIT*6)
void sendSOS() {
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
  PULSE(PAUSE); // inter letter pause
  PULSE(LONG);PULSE(LONG);PULSE(LONG);
  PULSE(PAUSE); // inter letter pause
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
}

UPDATE: I've added the inter-letter pause as suggested in the comments.

#define UNIT 300
#define SHORT (UNIT)
#define LONG (UNIT*3)
#define SPACE (UNIT*6)
void sendSOS() {
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
  PULSE(LONG);PULSE(LONG);PULSE(LONG);
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
}
#define UNIT 300
#define SHORT (UNIT)
#define LONG (UNIT*3)
#define PAUSE (UNIT*3)
#define SPACE (UNIT*6)
void sendSOS() {
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
  PULSE(PAUSE); // inter letter pause
  PULSE(LONG);PULSE(LONG);PULSE(LONG);
  PULSE(PAUSE); // inter letter pause
  PULSE(SHORT);PULSE(SHORT);PULSE(SHORT);
}

UPDATE: I've added the inter-letter pause as suggested in the comments.

Source Link
Loading