/*Using a Single Button, create mutliple options based on how long the button is pressed.
 *So the board is turned into a morse-code device with two options: a free mode and an SOS mode. 

 2018 Xavier Klein WTFPL

 Based on this code:
 Created DEC 2014 by Scuba Steve
 Modified JAN 2015 by Michael James
 Both members of https://programmingelectronics.com
 
 */


/////////Declare and Initialize Variables////////////////////////////

//We need to track how long the momentary pushbutton is held in order to execute different commands
//This value will be recorded in seconds
float pressLength_milliSeconds = 0;


int Short = 500;
 int Long = 1000;

// Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;       
int optionThree_milliSeconds = 4000;  

//The Pin your button is attached to
int buttonPin = 3;

//Pin your LEDs are attached to
int led_pin = 7;

void setup(){

  // Initialize the pushbutton pin as an input pullup
  // Keep in mind, when pin 2 has ground voltage applied, we know the button is being pressed
  pinMode(buttonPin, INPUT_PULLUP);     

  //set the LEDs pins as outputs
  pinMode(led_pin, OUTPUT); 

  //Start serial communication - for debugging purposes only
  //Serial.begin(9600);                                     

} // close setup


void loop() {

  //Record *roughly* the tenths of seconds the button in being held down
while (digitalRead(buttonPin) == LOW ){ 

    delay(100);  //if you want more resolution, lower this number 
    pressLength_milliSeconds = pressLength_milliSeconds + 100;   



  }




  //Different if-else conditions are triggered based on the length of the button press
  //Start with the longest time option first

  //Option 2 - Execute the third option if the button is held for 3 seconds, it's spelling Hello World in morse.
  if (pressLength_milliSeconds >= optionThree_milliSeconds){

H();
E();
L();
L();
O();
Space();
W();
O();
R();
L();
D();

  } 

  //Option 1 - Execute the first option, it's free mode for morse coding.

   else if(pressLength_milliSeconds >= optionTwo_milliSeconds){
    S();
O();
S();
   }


 else if(pressLength_milliSeconds >= optionOne_milliSeconds){

digitalWrite(led_pin,HIGH);
    delay(100);
digitalWrite(led_pin,LOW);


  }//close if options



  //every time through the loop, we need to reset the pressLength_Seconds counter
  pressLength_milliSeconds = 0;

} 

//////Functions for Short and Long

void ShortBlink(){

digitalWrite(led_pin,HIGH);
delay(Short);
digitalWrite(led_pin,LOW);
delay(Short);
  
}

void LongBlink(){

digitalWrite(led_pin,HIGH);
delay(Long);
digitalWrite(led_pin,LOW);
delay(Long);
  
}

////Functions for letters

void H(){
ShortBlink();
ShortBlink();
ShortBlink();
ShortBlink();
delay(1500);
}

void E(){
  ShortBlink();
delay(1500);
}

void L(){
ShortBlink();
  LongBlink();
  ShortBlink();
ShortBlink();
delay(1500);
}

void Space(){
  delay(3000);

}

void W(){
  ShortBlink();
  LongBlink();
    LongBlink();
    delay(1500);
}

void R(){
  ShortBlink();
  LongBlink();
    ShortBlink();
    delay(1500);

}

void D(){
  LongBlink();
  ShortBlink();  
  ShortBlink();
    delay(1500);
}

void S(){

ShortBlink();
ShortBlink();
ShortBlink();
delay(1500);
}

void O(){

LongBlink();
LongBlink();
LongBlink();
delay(1500);
}