Here you will see a video showing the functioning of the ESP-WROOM-32 circuit with a SOS code
In this case, I will show you 6 codes with their corresponding explanations in each section so that you can see the variety of options that programming a thing, word or object has.
Firts code
/***
* LED Blinking
* Code Written by Miquel García and Xavier Garrido
*/
const int ledPin = 2; //The LED is connected in PIN D2
void setup() {
// setup pin 2 as a digital output pin because the light goes out, a digital input would be a sensor
pinMode (ledPin, OUTPUT);
}
void loop() { //This is a loop, which means that when the code has been recreated by the chip, once it is finished it will start over.
digitalWrite (ledPin, HIGH); // turn on the LED
delay (200); // wait for half a second or 200 milliseconds
digitalWrite (ledPin, LOW); // turn off the LED
delay (200); // wait for half a second or 200 milliseconds
digitalWrite (ledPin, HIGH);
delay (200);
digitalWrite (ledPin, LOW);
delay (200);
digitalWrite (ledPin, HIGH);
delay (200);
digitalWrite (ledPin, LOW);//This is the time between the fisrt letter and the second
delay (600);
digitalWrite (ledPin, HIGH);
delay (600);
digitalWrite (ledPin, LOW);
delay (200);
digitalWrite (ledPin, HIGH);
delay (600);
digitalWrite (ledPin, LOW);
delay (200);
digitalWrite (ledPin, HIGH);
delay (600);
digitalWrite (ledPin, LOW);//This is the time that separates the second letter from the third
delay (600);
digitalWrite (ledPin, HIGH);
delay (200);
digitalWrite (ledPin, LOW);
delay (200);
digitalWrite (ledPin, HIGH);
delay (200);
digitalWrite (ledPin, LOW);
delay (200);
digitalWrite (ledPin, HIGH);
delay (200);
digitalWrite (ledPin, LOW); //This waiting time is the longest, since it expresses that the word has finished and another word would start
delay (1400);
}
Second code
/***
* LED Blinking
* Code Written by Miquel García and Xavier Garrido
*/
int ledPin = 2; //The LED is connected in PIN D2
//The first step is defining the constants. Defining the constants means applying the module individually, when creating a letter, for example, only having to put the name assigned
//to each value, in this way if we make a mistake we only have to change it here and not at each point in which be applied.
const int dot = 200;
const int dash = dot*3;
const int space = dot*7;
void setup() {
// setup pin 2 as a digital output pin because the light goes out, a digital input would be a sensor
pinMode (ledPin, OUTPUT);
}
//In this case we also have a loop, but previously we are defining the letters, that is, we are giving a value
//to each letter, in this way creating a word is easier because in the loop you only have to put the letter and here we put what okay. A "more orderly" way.
void S() {
digitalWrite (ledPin, HIGH);
delay (dot);
digitalWrite (ledPin, LOW);
delay (dot);
digitalWrite (ledPin, HIGH);
delay (dot);
digitalWrite (ledPin, LOW);
delay (dot);
digitalWrite (ledPin, HIGH);
delay (dot);
digitalWrite (ledPin, LOW);
delay (dash);
}
void O() {
digitalWrite (ledPin, HIGH);
delay (dash);
digitalWrite (ledPin, LOW);
delay (dash);
digitalWrite (ledPin, HIGH);
delay (dash);
digitalWrite (ledPin, LOW);
delay (dash);
digitalWrite (ledPin, HIGH);
delay (dash);
digitalWrite (ledPin, LOW);
delay (dash);
}
void loop (){ //Now we find ourselves with the loop again, but in this case, as we can see, only the letters that we want to put are necessary, because the values have been given before.
S();
O();
S();
digitalWrite (ledPin, LOW);
delay (space);
}
Third code
/***
* LED Blinking
* Code Written by Miquel García and Xavier Garrido
*/
int ledPin = 2;
//In this code we will be using functions. The typical case for creating a function is when one needs to perform the same
//action multiple times in a program.
void setup() { // put your setup code here, to run once:
pinMode (ledPin, OUTPUT);
}
void flash(int nf, int d){ //Here I'm defining one parameter, in our case we will us it for the dot
for(int i = 0; i < nf; i++){
digitalWrite (ledPin, HIGH);
delay (d);
digitalWrite (ledPin, LOW);
delay (d);
}
}
void sent(int nf, int d){ //Here I'm defining one parameter, in our case we will us it for the dash
for(int i = 0; i < nf; i++){
digitalWrite (ledPin, HIGH);
delay (d);
digitalWrite (ledPin, LOW);
delay (d/3);
}
}
void loop() { // put your main code here, to run repeatedly:
flash(3,200); //This means that the letter S is made up of three dots, therefore we will need to flash three times as
//indicated in the parentheses and each dot for 200 milliseconds (morse code rules).
digitalWrite (ledPin, LOW); //Adding this LOW to separate the letter S with the O
delay (400);
sent(3,600); //In this second case, we use only the sent function because, as in the case of S, it is made up of only one
//group of functions, in this case three lines, which must be three times the duration of a dot, therefore 600 milliseconds.
digitalWrite (ledPin, LOW); //Adding this LOW to separate the letter O with the S
delay (400);
flash(3,200);
digitalWrite (ledPin, LOW); //Adding this LOW to indicate that the word has been finished.
delay (1200);
}
Fourth code
/***
* LED Blinking
* Code Written by Miquel García and Xavier Garrido
*/
const byte ledPin = 2;
void setup()
// Use ledPin as output
pinMode(ledPin, OUTPUT)
//Create a function for short blink
void shortBlink()
// Make a single short blin
digitalWrite(ledPin, HIGH)
delay(200)
digitalWrite(ledPin, LOW)
delay(200)
void longBlink()
// Make a single long blink
digitalWrite(ledPin, HIGH)
delay(600)
digitalWrite(ledPin, LOW)
delay(200);
}
void morseBlink(char character) {
// Translate character to Morse code
switch(character){
case ‘s‘:
shortBlink();
shortBlink();
shortBlink();
break;
case ‘o‘:
longBlink();
longBlink();
longBlink();
break;
}
}
void loop() {
// Start blinking SOS
morseBlink(‘s‘);
morseBlink(‘o‘);
morseBlink(‘s’);
}
Fifth code
//According to the wikipedia article on Morse Code:
//A ‘dash’ should be three times the length of a ‘dot’.
//The time between dots or dashes in a letter should be that same as the time for a dot.
//The time between letters should be three dot times.int ledPin = 13;
int ledPin = 2;
const int DOT = 150;
const int DASH = DOT * 3;
int durations[] = {DOT, DOT, DOT, DASH, DASH, DASH, DOT, DOT, DOT};
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 9; i++) {
flash(durations[i]);
if (i == 2 || i == 5)
delay(DASH - DOT); // inter-letter additional time
}
delay(1000);
}
void flash(int duration) {
digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(DOT);
}
Sixth code
// Morse code for blinking a LED
//The fact of this code is that it has the capacity to interpret in this case the words that we write in our language to Morse language, therefore,
//we have to define the letters one by one, just like the numbers and then a sequence of parameters with the ability to transform what we write.
int ledPin = 2;
int dotDelay = 200;
char* letters[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};
char* numbers[] = {
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char ch;
if (Serial.available() > 0)
{
ch = Serial.read();
if (ch >= 'a' && ch <= 'z')
{
flashSequence(letters[ch - 'a']);
}
else if (ch >= 'A' && ch <= 'Z')
{
flashSequence(letters[ch - 'A']);
}
else if (ch >= '0' && ch <= '9')
{
flashSequence(numbers[ch - '0']);
}
else if (ch == ' ')
{
delay(dotDelay * 4); // gap between words
}
}
}
void flashSequence(char* sequence)
{
int i = 0;
while (sequence[i] != NULL)
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); // gap between letters
}
void flashDotOrDash(char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == '.')
{
delay(dotDelay);
}
else // must be a dash
{
delay(dotDelay * 3);
}
digitalWrite(ledPin, LOW);
delay(dotDelay); // gap between flashes
}
Seventh Code
In this project we also tried to connect a speaker to the board so that when the led lights up when executing the SOS code, the speaker makes sounds at the same time as the flashing.
To do that, you need tone, which is what allows the speaker to sound. At first we had problems because the arduino application did not recognize the tone command, we searched the internet for a solution and found a person who had the same problem and created his own library so that "Tone32" could be recognized and work correctly.
Once that library was downloaded and applied, the application recognized the code completely and there was no error, but here came the second problem, we did not know which pin to connect the speaker to. We looked on different pages for the pinout of our board but none had the information we were looking for, which was a pin that had a PWM (pulse width modulation) output.
We tried to connect in different places, from pin 0-11 and when sending the code none reproduced sound, that is, the problem is no longer in the code, but in where to connect the speaker.
Finally, we got it to ring and light at the same time from two libraries found in the code number 7 ("Tone32" and "ESP32Servo"), those ones have allowed to create accesses that make the board and Arduino compatible.
Here you will see a video showing the functioning of the ESP-WROOM-32 circuit with a SOS code adding a speaker