Into this section it is explained with the functioning of the ESP-8266-MOD to do SOS Morse Code in a LED.
In the next video it is recorded the SOS Arduino code with a LED.
In the next video it is the siimulation of the SOS code made at Trinkercad with a LED.
To make this blinking there have been created different codes with Arduino.
1 /* Created on 2022 2 * Last modification: 21st April 2022 3 * by Villares 4 */ 5 6 const int ledPin = 5; // LED connected to pin number 5 7 8 void setup() { // the setup routine runs once when you press reset
9 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as an output
10 }
11
12 void loop() { // the loop routine runs over and over again forever
13 //S -> ... (3 dots)
14 digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
15 delay(500); // wait for 0.5 seconds
16
17 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
18 delay(500); // wait for 0.5 seconds
19
20 digitalWrite(ledPin, HIGH);
21 delay(500);
22
23 digitalWrite(ledPin, LOW);
24 delay(500);
25
26 digitalWrite(ledPin, HIGH);
27 delay(500);
28
29 digitalWrite(ledPin, LOW);
30 delay(1500); // space between letters is three units
31
32 //O -> --- (3 dashes)
33 digitalWrite(ledPin, HIGH);
34 delay(1500);
35
36 digitalWrite(ledPin, LOW);
37 delay(500);
38
39 digitalWrite(ledPin, HIGH);
40 delay(1500);
41
42 digitalWrite(ledPin, LOW);
43 delay(500);
44
45 digitalWrite(ledPin, HIGH);
46 delay(1500);
47
48 digitalWrite(ledPin, LOW);
49 delay(1500);
50
52 //S -> ... (3 dots)
53 digitalWrite(ledPin, HIGH);
54 delay(500);
55
56 digitalWrite(ledPin, LOW);
57 delay(500);
58
59 digitalWrite(ledPin, HIGH);
60 delay(500);
61
62 digitalWrite(ledPin, LOW);
63 delay(500);
64
65 digitalWrite(ledPin, HIGH);
66 delay(500);
67
68 digitalWrite(ledPin, LOW);
69 delay(3500); // space between words is seven units
70 }
71
In the following code there are made three void functions (they are used in function declarations): the S and the O to make the SOS blinking and a void silence for making the seven units silence between words according to the Morse Code. Therefore, in the void loop there are only called the functions.
The advantage over the previous code is that the S letter, that in the SOS appears twice, has to be defined just once.
1 /* Created on 2022 2 * Last modification: 21st April 2022 3 * by Villares 4 */ 5 6 const int ledPin = 5; // LED connected to pin number 5 7 8 void setup() { // the setup routine runs once when you press reset
9 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as an output
10 }
11
12 void S() {
13 digitalWrite(ledPin>, HIGH); // turn the LED on (HIGH is the voltage level)
14 delay(500); // wait for 0.5 seconds
15
16 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
17 delay(500); // wait for 0.5 seconds
18
19 digitalWrite(ledPin, HIGH);
20 delay(500);
21
22 digitalWrite(ledPin, LOW);
23 delay(500);
24
25 digitalWrite(ledPin, HIGH);
26 delay(500);
27
28 digitalWrite(ledPin, LOW);
29 delay(1500);
30 }
31
32 void O() {
33 digitalWrite(ledPin, HIGH);
34 delay(1500);
35
36 digitalWrite(ledPin, LOW);
37 delay(500);
38
39 digitalWrite(ledPin, HIGH);
40 delay(1500);
41
44 digitalWrite(ledPin, LOW);
45 delay(500);
46
47 digitalWrite(ledPin, HIGH);
48 delay(1500);
49
50 digitalWrite(ledPin, LOW);
51 delay(1500);
52 }
53
54 void silence() { // the silence function is created for the space between words
55 digitalWrite(ledPin, LOW);
56 delay(2000);
57 }
58
59 void loop() { // the loop routine runs over and over again forever
60 S(); // the S function is called--> 3 dots
61 O(); // the O function is called--> 3 dashes
62 S(); // the S function is called--> 3 dots
63 silence(); // the silence function is called--> space between words is seven units
64 }
65
In the following code there are made only two void functions (they are used in function declarations): one for the SOS blinking and the other for the void silence for making the seven units silence between words according to the Morse Code. Therefore, in the void loop there are only called the SOS function and it is defined the delay for the different letters.
The advantage over the previous codes is that with a 14-lines function there can be defined both letters S and O, there is only needed to specify the delay of each letter.
1 /*Created on 2022 2 *Last modification: 21st April 2022 3 *by Villares 4 */ 5 6 const int ledPin = 5; // LED connected to pin number 5 7 8 void setup() { // the setup routine runs once when you press reset
9 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as an output
10 }
11
12 void SOS(int n) { // Function for doing S and O with a LED specifying the delay (S is three dots and O, three dashes)
13 digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
14 delay(n); // wait for n seconds
15
16 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
17 delay(500); // wait for n seconds
18
19 digitalWrite(ledPin, HIGH);
20 delay(n);
21
22 digitalWrite(ledPin, LOW);
23 delay(500);
24
25 digitalWrite(ledPin, HIGH);
26 delay(n);
27
28 digitalWrite(ledPin, LOW);
29 delay(1500);
30 }
31
32 void silence() { // Creates a function to create the space between words according to Morse Code (seven units)
33 digitalWrite(ledPin, LOW);
34 delay(2000);
35 }
36
37 void loop() { // the loop routine runs over and over again forever
38 SOS(500); // the delay is 0.5s, one unit--> three dots = S
39 SOS(1500); // the delay is 1.5s, three units--> three dashes = O
40 SOS(500); // the delay is 0.5s, one unit--> three dots = S
41 silence(); // this is the space between words: seven units
42 }
43
Code 4
The next code permits to write dots and dashes in a Serial Monitor and they are executed on the LED.
1 /*Created on 2022 2 *Last modification: 23rd April 2022 3 *This code permits to write dots and dashes in a Serial Monitor and they are executed on the LED 4 */ 5 6 const int ledPin = 5; // LED connected to pin number 5 7 8 void setup() { // the setup routine runs once when you press reset
9 pinMode(lightPin, OUTPUT); // Configures LED pin to behave as an output
10 digitalWrite(lightPin, LOW);
11 Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
12 }
13
14 void loop() { // the loop routine runs over and over again forever
15 if (Serial.available()) {
16 String input = Serial.readString();
17 String morse = filterMorse(input);
18 showMorse(morse);
19 }
20 }
21
22 String filterMorse(String input) {
23 int inputLength = input.lenght();
24 String outputString = "" ;
25
26 for (int i = 0; i < inputLength; i++) {
27 char c = input[i];
28 if (c == '-' || c == '.' ) {
29 outputString += c;
30 }
31 }
32
33 return outputString;
34 }
35
36 void showMorse(String morse) {
37 int morseLength = morse.lenght();
38
39 for (int i = 0; i < morseLength; i++) {
40 char c = morse[i];
41
42 digitalWrite(lightPin, HIGH);
43
44 if (c == '-') {
45 delay(1500); // dash
46 } else {
47 delay(500); // dot
48 }
49
50 digitalWrite(lightPin, LOW);
51 delay(500);
52 }
53 }
Code 5
There is created a void morse in whcih there are defined all the characters (latin letters and arabic numbers). So then, they can be written in the Serial Monitor and they are automatically executed in the LED.
1 /* Created on 2022 2 * Last modification: 26th April 2022 3 * This code permits to write latin letters and arabic numbers on a Serial Monitor and it is translated to Morse Code and executed in a LED. 4 */ 5 6 const int ledPin = 5, t = 500; // LED connected to pin number 5 and t is defined for 0.5s 7 8 void morse(char ch) {
9 }
10 if(ch=='a') {
11 digitalWrite(ledPin, HIGH);
12 delay(t);
13 digitalWrite(ledPin, LOW);
14 delay(t);
15
16 digitalWrite(ledPin, HIGH);
17 delay(3*t);
18 digitalWrite(ledPin, LOW);
19 delay(3*t);
20 }
21
22 else if(ch=='b') {
23 digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
24 delay(3*t); // wait for 1500 ms (dash)-> t has been defined for 500 ms!!
25 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
26 delay(t); // wait for 500 ms (space between parts of the same letter)
27
28 digitalWrite(ledPin, HIGH);
29 delay(t); // wait for 500 ms (dot)
30 digitalWrite(ledPin, LOW);
31 delay(t);
32
33 digitalWrite(ledPin, HIGH);
34 delay(t); // wait for 500 ms (dot)
35 digitalWrite(ledPin, LOW);
36 delay(t);
37
38 digitalWrite(ledPin, HIGH);
39 delay(t); // wait for 500 ms (dot)
40 digitalWrite(ledPin, LOW);
41 delay(3*t); // wait for 1500 ms (space between letters is 3 units)
42 }
43
44 // Following this pattern, all the 26 basic Latin letters (a through z) and the Arabic numbers are defined
45 else if(ch=='c') {
46 digitalWrite(ledPin, HIGH);
47 delay(3*t);
48 digitalWrite(ledPin, LOW);
49 delay(t);
50
51 digitalWrite(ledPin, HIGH);
52 delay(t);
53 digitalWrite(ledPin, LOW);
54 delay(t);
55
56 digitalWrite(ledPin, HIGH);
57 delay(3*t);
58 digitalWrite(ledPin, LOW);
59 delay(t);
60
61 digitalWrite(ledPin, HIGH);
62 delay(t);
63 digitalWrite(ledPin, LOW);
64 delay(3*t);
65 }
66
67 else if(ch=='d') {
68 digitalWrite(ledPin, HIGH);
69 delay(3*t);
70 digitalWrite(ledPin, LOW);
71 delay(t);
72
73 digitalWrite(ledPin, HIGH);
74 delay(t);
75 digitalWrite(ledPin, LOW);
76 delay(t);
77
78 digitalWrite(ledPin, HIGH);
79 delay(t);
80 digitalWrite(ledPin, LOW);
81 delay(3*t);
82 }
83
84 else if(ch=='e') {
85 digitalWrite(ledPin, HIGH);
86 delay(t);
87 digitalWrite(ledPin, LOW);
88 delay(3*t);
89 }
90
91 else if(ch=='f') {
92 digitalWrite(ledPin, HIGH);
93 delay(t);
94 digitalWrite(ledPin, LOW);
95 delay(t);
96
97 digitalWrite(ledPin, HIGH);
98 delay(t);
99 digitalWrite(ledPin, LOW);
100 delay(t);
101
102 digitalWrite(ledPin, HIGH);
103 delay(3*t);
104 digitalWrite(ledPin, LOW);
105 delay(t);
106
107 digitalWrite(ledPin, HIGH);
108 delay(t);
109 digitalWrite(ledPin, LOW);
110 delay(3*t);
111 }
112
113 else if(ch=='g') {
114 digitalWrite(ledPin, HIGH);
115 delay(3*t);
116 digitalWrite(ledPin, LOW);
117 delay(t);
118
119 digitalWrite(ledPin, HIGH);
120 delay(3*t);
121 digitalWrite(ledPin, LOW);
122 delay(t);
123
124 digitalWrite(ledPin, HIGH);
125 delay(t);
126 digitalWrite(ledPin, LOW);
127 delay(3*t);
128 }
129
130 else if(ch=='h') {
131 digitalWrite(ledPin, HIGH);
132 delay(t);
133 digitalWrite(ledPin, LOW);
134 delay(t);
135
136 digitalWrite(ledPin, HIGH);
137 delay(t);
138 digitalWrite(ledPin, LOW);
139 delay(t);
140
141 digitalWrite(ledPin, HIGH);
142 delay(t);
143 digitalWrite(ledPin, LOW);
144 delay(t);
145
146 digitalWrite(ledPin, HIGH);
147 delay(t);
148 digitalWrite(ledPin, LOW);
149 delay(3*t);
150 }
151
152 else if(ch=='i') {
153 digitalWrite(ledPin, HIGH);
154 delay(t);
155 digitalWrite(ledPin, LOW);
156 delay(t);
157
158 digitalWrite(ledPin, HIGH);
159 delay(t);
160 digitalWrite(ledPin>, LOW);
161 delay(3*t);
162 }
163
164 else if(ch=='j') {
165 digitalWrite(ledPin, HIGH);
166 delay(t);
167 digitalWrite(ledPin, LOW);
168 delay(t);
169
170 digitalWrite(ledPin, HIGH);
171 delay(3*t);
172 digitalWrite(ledPin, LOW);
173 delay(t);
174
175 digitalWrite(ledPin, HIGH);
176 delay(3*t);
177 digitalWrite(ledPin, LOW);
178 delay(t);
179
180 digitalWrite(ledPin, HIGH);
181 delay(3*t);
182 digitalWrite(ledPin, LOW);
183 delay(3*t);
184 }
185
186 else if(ch=='k') {
187 digitalWrite(ledPin, HIGH);
188 delay(3*t);
189 digitalWrite(ledPin, LOW);
190 delay(t);
191
192 digitalWrite(ledPin, HIGH);
193 delay(t);
194 digitalWrite(ledPin, LOW);
195 delay(t);
195
196 digitalWrite(ledPin, HIGH);
198 delay(3*t);
199 digitalWrite(ledPin, LOW);
200 delay(3*t);
201 }
202
else if(ch=='l') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='m') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='n') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='o') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='p') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='q') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='r') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='s') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='t') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='u') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='v') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='w') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='x') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='y') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='z') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='0') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='1') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='2') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='3') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='4') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='5') {
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='6') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='7') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='8') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch=='9') {
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(3*t);
digitalWrite(pin,LOW);
delay(t);
digitalWrite(pin,HIGH);
delay(t);
digitalWrite(pin,LOW);
delay(3*t);
}
else if(ch==' ') {
delay(4*t); // Space between words is 7 units
}
}
void setup() {
Serial.begin(9600); // starts serial communication (Arduino can send out commands through the USB connection). The value 9600 is how fast the data is sent
pinMode(pin,OUTPUT);
}
void loop() {
String input="";
if(Serial.available()>0) {
input=Serial.readString();
Serial.println(input);
int i=0;
char ip='a';
while(ip != '\0') {
ip=input.charAt(i); // This code gets the character at a particular location in the string.
Serial.println(ip);
morse(ip);
i++;
353 }
353 }
353 }
Array code
1 /* Created on 2022 2 * Last modification: 26th April 2022 3 * This code permits to write latin letters and arabic numbers on a Serial Monitor and it is translated to Morse Code and executed in a LED. 4 */ 5 6 unsigned int led_pin = 5; // Assign a name to the digital pin 5 7 8 // Declare an array named letters that holds addresses of string literals 9 char *letters[] = {
10 // The letters from A to Z in Morse code
11 ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
12 ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
13 "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
14 };
15 char *numbers[] = {
16 // The numbers from 0 to 9 in Morse code
17 "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
18 };
19
20 unsigned int dot_duration = 500; // Determine the duration of a dot in ms
21 bool done = false; // A bool holds one of two values, true or false
22
23 // Function runs only once, after each powerup or reset of the Arduino
24 void setup() {
25 // Set the LED to output
26 pinMode(led_pin, OUTPUT);
27 Serial.begin(9600); // Open the serial port and set the data transmission rate to 9600 bps (bits per second)
28 }
29 // Main function that drives the system
30 void loop() {
31 char ch;
32 // This loop waits for a string, displays it in Morse code, and only exits the loop if the sentinel is entered
33 while (!done) {
34 if (Serial.available()) { // Check to see if there are letters or numbers available to be read
35 ch = Serial.read(); // Read one letter or number at a time. Serial.read() returns the first (oldest) character in the buffer and removes that byte of data from the buffer
36 if (ch >= 'A' && ch <='Z') { // Check for uppercase letters
37 Serial.println(ch);
38 flash_morse_code(letters[ch - 'A']);
39 }
40 else if (ch >= 'a' && ch <='z') { // Check for lowercase letters
41 Serial.println(ch);
42 flash_morse_code(letters[ch - 'a']);
43 }
44 else if (ch >= '0' && ch <='9') { // Check for numbers
45 Serial.println(ch);
46 flash_morse_code(letters[ch - '0']);
47 }
// Check for numbers
else if (ch >= '0' && ch <= '9') {
Serial.println(ch);
flash_morse_code(numbers[ch - '0']);
}
// Check for space between words
else if (ch == ' ') {
// Put space between two words in a message...equal to seven dots
delay(dot_duration * 7);
}
// Check for sentinel value
else if (ch == '!') {
done = true;
Serial.println("Thank you! Your messages were sent successfully.");
Serial.println("Goodbye.");
}
}
}
// Do nothing
while(true) {}
}
void flash_morse_code(char *morse_code) {
unsigned int i = 0;
// Read the dots and dashes and flash accordingly
while (morse_code[i] != NULL) {
flash_dot_or_dash(morse_code[i]);
i++;
}
// Space between two letters equals to three dots
delay(dot_duration * 3);
}
void flash_dot_or_dash(char dot_or_dash) {
// Make the LED shine
digitalWrite(led_pin, HIGH);
if (dot_or_dash == '.') { // If it is a dot
delay(dot_duration);
}
else { // Has to be a dash...equal to three dots
delay(dot_duration * 3);
}
// Turn the LED off
digitalWrite(led_pin, LOW);
// Give space between parts of the same letter: equals to one dot
delay(dot_duration);
}