Into this section it is the video, simulation and code to make an SOS Morse Code blink with a Buzzer and a LED at the same time.
1 /* Created on 2022 2 *Last modification: 25th April 2022 3 *by Villares 4 */ 5 6 const int ledPin = 5; // LED connected to pin number 5 (D1) 7 const int pinBuzzer = 4; // Buzzer connected to pin number 4 (D2) 8 9 void setup() { // the setup routine runs once when you press reset
10 pinMode(ledPin, OUTPUT); // Configures LED pin to behave as an output
11 pinMode(pinBuzzer, OUTPUT); // Configures BUZZER pin to behave as an output
12 }
13
14 void SOS(int n) { // the SOS function is created, substituting the value n we can create 3 dots (S) or 3 dashes (O)
15 digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
16 tone(pinBuzzer, 440); // turn the Buzzer on at a frequency of 440 Hz
17 delay(n); // wait for n seconds
18
19 digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
20 noTone(pinBuzzer); // turn the Buzzer off
21 delay(500); // wait for 0.5 seconds
22
23 digitalWrite(ledPin, HIGH);
24 tone(pinBuzzer, 440);
25 delay(n);
26
27 digitalWrite(ledPin, LOW);
28 noTone(pinBuzzer);
29 delay(500);
30
31 digitalWrite(ledPin, HIGH);
32 tone(pinBuzzer, 440);
33 delay(n);
34
35 digitalWrite(ledPin, LOW);
36 noTone(pinBuzzer);
37 delay(1500); // space between letters is three units
38 }
39
40 void silence() { // the silence function is created for the space between words
41 digitalWrite(ledPin, LOW);
42 noTone(pinBuzzer);
43 delay(2000); // space between words is seven units (in the SOS function there are already 3 units, so this delay is for 4 units)
44 }
45
46 void loop() { // the loop routine runs over and over again forever
47 SOS(500); // for the SOS function, we substitute the value n per 0.5s--> 3 dots
48 SOS(1500); // for the SOS function, we substitute the value n per 1.5s--> 3 dashes
49 SOS(500); // for the SOS function, we substitute the value n per 0.5s--> 3 dots
50 silence(); // the silence function is called--> space between words is seven units
51 }
52