// Pin config const int button = 2; const int led = 4; // values for morse code, https://morsecode.world/international/timing.html const int unit = 100; // base time unit for morse code, smaller is faster const int dot = unit; // same as dit (.) const int dash = unit * 3; // same as dah (-) const int intra_char_space = unit; // the gap between dits and dahs within a character const int inter_char_space = unit * 3; // the gap between the characters of a word const int word_space = unit * 7; // the gap between two words // status values of button int pressed = 0; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); pinMode(button, INPUT); } // functions of breathing light void breathing() { for (int i = 0; i < 255; i++) { analogWrite(led, i); delay(1); } for (int i = 255; i > 0; i--) { analogWrite(led, i); delay(1); } } // functions of morse code // "_" is used for spaces in between words. Every char already ends with a inter_char_space of 3 time units. void _() { delay(word_space - inter_char_space); } void s() { // ... analogWrite(led, 255); delay(dot); analogWrite(led, 0); delay(intra_char_space); analogWrite(led, 255); delay(dot); analogWrite(led, 0); delay(intra_char_space); analogWrite(led, 255); delay(dot); analogWrite(led, 0); delay(inter_char_space); } void o() { // --- analogWrite(led, 255); delay(dash); analogWrite(led, 0); delay(intra_char_space); analogWrite(led, 255); delay(dash); analogWrite(led, 0); delay(intra_char_space); analogWrite(led, 255); delay(dash); analogWrite(led, 0); delay(inter_char_space); } void loop() { pressed = digitalRead(button); if (pressed == LOW) { // if pressed = true // SOS ···---··· s(); o(); s(); _(); } else { // if pressed = false breathing(); } }