user26503: And first of all sorry for my bad English.
I am trying to write code that allows the user to change between continuous or manual mode, but I am having some issues getting it to work. The goal is to be able to switch from manual SOS (user pressing the button to SOS) and automatic SOS (the led automatically blinking SOS morse).
Currently, when I press down the button it loops once the automatic SOS and returns back to its normal state with LED on. 
How would I make it, for an example switch the mode to manual when holding down button and giving user some sort of indication when changing the mode (etc blinking the led for X amount of time?)
What I am looking to achieve is:

Turn LEDS on when pressing the button. 
Start looping automatic SOS signal
Press or hold down the button to change mode.


Blink LEDS to indicate the mode change.

Switch to manual SOS signal (user pressing the button to signal) 
Press or hold down the button to change mode.


Blink LEDS to indicate the mode change. 

Switch back to an automatic loop.

The code so far:
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin =  13; // the number of the LED pin
int mode;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  mode = digitalRead(buttonPin);

  // Manual SOS mode.
  buttonState = mode;

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH); // turn LED on:
  } else {
    digitalWrite(ledPin, LOW); // turn LED off:
  }

  // Automatic SOS mode.
  if (mode == HIGH) {
    // check if the pushbutton is pressed.
    // if it is, starts looping automatic SOS.
    int i = 0;
    for (i=0; i<3; i++) {
      digitalWrite(ledPin, HIGH);   // turn the LED on 
      delay(300); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(300); // wait for a second
    }

    for (i=0; i<3; i++){
      digitalWrite(ledPin, HIGH); // turn the LED on
      delay(1200); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(1200); // wait for a second
    }

    for (i=0; i<3; i++){
      digitalWrite(ledPin, HIGH); // turn the LED on 
      delay(300); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(300); // wait for a second
    }
    digitalWrite(ledPin, LOW);
    // waits 3 seconds before starting the loop again
    delay(3000); 
  }
}


user19867: Ok, I'll try to split the code into multiple sections for clarity, but all this goes into one single sketch.
First we define the PINs: defines are pre-processor macros, even better than const in terms of performance and space consumption:
#define BTN_PIN 2
#define LED_PIN 13

Let's initialize the board as per our requirements:
void setup() {
  pinMode(BTN_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
}

Now we define the code which emits and SOS signal in morse code using the standard timing:

a long pulse lasts 3 times a short one
a space lasts 6 times a short pulse

That boils down to the following definitions considering a standard unit of 300ms:
#define UNIT 300
#define SHORT (UNIT)
#define LONG (UNIT*3)
#define PAUSE (UNIT*3)
#define SPACE (UNIT*6)

I'm also going to define another macro to emit a generic light pulse, for simplicity I'll use the delay(...) function, but this will prevent detecting button presses while signalling:
#define PULSE(TYPE) digitalWrite(LED_PIN, HIGH);delay(TYPE); digitalWrite(LED_PIN, LOW);delay(UNIT)

With the above in place we can define a function to send an SOS which should be both very compact and also readable:
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);
}

Now, let's describe what the mode changing should be notified to the user:
#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);
  }
}

Now our main loop will have to check the button and act accordingly, right?
#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();
      }
    }
  }

The solution above has great space for improvement:

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)

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.

26503: <p>And first of all sorry for my bad English.</p>

<p>I am trying to write code that allows the user to change between continuous or manual mode, but I am having some issues getting it to work. The goal is to be able to switch from manual SOS (user pressing the button to SOS) and automatic SOS (the led automatically blinking SOS morse).</p>

<p>Currently, when I press down the button it loops once the automatic SOS and returns back to its normal state with LED on. </p>

<p>How would I make it, for an example switch the mode to manual when holding down button and giving user some sort of indication when changing the mode (etc blinking the led for X amount of time?)</p>

<p>What I am looking to achieve is:</p>

<ol>
<li>Turn LEDS on when pressing the button. </li>
<li>Start looping automatic SOS signal</li>
<li>Press or hold down the button to change mode.

<ul>
<li>Blink LEDS to indicate the mode change.</li>
</ul></li>
<li>Switch to manual SOS signal (user pressing the button to signal) </li>
<li>Press or hold down the button to change mode.

<ul>
<li>Blink LEDS to indicate the mode change. </li>
</ul></li>
<li>Switch back to an automatic loop.</li>
</ol>

<p>The code so far:</p>

<pre><code>// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin =  13; // the number of the LED pin
int mode;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  mode = digitalRead(buttonPin);

  // Manual SOS mode.
  buttonState = mode;

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH); // turn LED on:
  } else {
    digitalWrite(ledPin, LOW); // turn LED off:
  }

  // Automatic SOS mode.
  if (mode == HIGH) {
    // check if the pushbutton is pressed.
    // if it is, starts looping automatic SOS.
    int i = 0;
    for (i=0; i&lt;3; i++) {
      digitalWrite(ledPin, HIGH);   // turn the LED on 
      delay(300); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(300); // wait for a second
    }

    for (i=0; i&lt;3; i++){
      digitalWrite(ledPin, HIGH); // turn the LED on
      delay(1200); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(1200); // wait for a second
    }

    for (i=0; i&lt;3; i++){
      digitalWrite(ledPin, HIGH); // turn the LED on 
      delay(300); // wait for a second
      digitalWrite(ledPin, LOW); // turn the LED 
      delay(300); // wait for a second
    }
    digitalWrite(ledPin, LOW);
    // waits 3 seconds before starting the loop again
    delay(3000); 
  }
}
</code></pre>
 19867: <p>Ok, I'll try to split the code into multiple sections for clarity, but all this goes into one single sketch.</p>

<p>First we define the PINs: defines are pre-processor macros, even better than <code>const</code> in terms of performance and space consumption:</p>

<pre><code>#define BTN_PIN 2
#define LED_PIN 13
</code></pre>

<p>Let's initialize the board as per our requirements:</p>

<pre><code>void setup() {
  pinMode(BTN_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
}
</code></pre>

<p>Now we define the code which emits and SOS signal in morse code using the standard timing:</p>

<ol>
<li>a long pulse lasts 3 times a short one</li>
<li>a space lasts 6 times a short pulse</li>
</ol>

<p>That boils down to the following definitions considering a standard unit of 300ms:</p>

<pre><code>#define UNIT 300
#define SHORT (UNIT)
#define LONG (UNIT*3)
#define PAUSE (UNIT*3)
#define SPACE (UNIT*6)
</code></pre>

<p>I'm also going to define another macro to emit a generic light pulse, for simplicity I'll use the <code>delay(...)</code> function, but this will prevent detecting button presses while signalling:</p>

<pre><code>#define PULSE(TYPE) digitalWrite(LED_PIN, HIGH);delay(TYPE); digitalWrite(LED_PIN, LOW);delay(UNIT)
</code></pre>

<p>With the above in place we can define a function to send an SOS which should be both very compact and also readable:</p>

<pre><code>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);
}
</code></pre>

<p>Now, let's describe what the mode changing should be notified to the user:</p>

<pre><code>#define QUICK 100
bool autoMode = true;

void switchMode() {
  autoMode= !autoMode;
  for (unsigned int i = 0; i &lt; 25; i++) {
    digitalWrite(LED_PIN, HIGH);
    delay(QUICK);
    digitalWrite(LED_PIN, LOW);
    delay(QUICK);
  }
}
</code></pre>

<p>Now our main loop will have to check the button and act accordingly, right?</p>

<pre><code>#define LONGPRESS 500
unsigned long elapsed;
bool clicked = false;
unsigned long duration;

void loop() {
  if (autoMode) {
    sendSOS();
  }
  elapsed = millis();
  while(clicked || millis() - elapsed &lt;= 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 &lt;= LONGPRESS) {
        // switch mode on long press
        switchMode();
      } else if (!autoMode) {
        // short press has a meaning only when not in auto mode
        sendSOS();
      }
    }
  }
</code></pre>

<p>The solution above has great space for improvement:</p>

<ul>
<li>there is no de-bouncing on the button</li>
<li>the board is unresponsive while signalling</li>
<li>keeping the button pressed forever will prevent any signalling to occur in any mode</li>
<li>the code is afflicted by <code>millis()</code> rollover if you keep the button pressed for long periods (around 49 days)</li>
</ul>

<p><strong>UPDATE</strong>: I've added the inter-letter pause as suggested in the comments.</p>

<p><strong>UPDATE</strong>: apparently <code>auto</code> is a reserved word, so I changed it to <code>autoMode</code>.</p>
