1st ARDUINO PROJECT: SOS with LEDs and audio

We are going to create several projects based on Arduino using the following tools:

Here's how to make a list:

We will start with the blinking code, because it is a hello world example producing the LED to switch ON and OFF

Before the code we need to be sure that the Arduino board is recognised by the computer. When Arduino software is opened and the board is not recognised we need to go to device manager (Administrador de Dispositivos) found at Control Panel. From the device manager we will see the Arduino board with a question mark. After that we will use the right button to install the drivers found in the Arduino folder. Finally we need to check the ports and must be selected beforehand in the Arduino software.

It is also interesting to check the blinking code found in Arduino software under the example tab in order to be sure that our system Arduino-computer is working.

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  //LED_BUILTIN is pin 13 because it has an internal resistor and you can plug in a LED in the board respecting polartity
  pinMode(LED_BUILTIN, OUTPUT); //We can define as an INPUT or OUTPUT
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Find the rules of Morse code and create a function for S and for O using that rules:


/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
byte led = 13;
int dot = dot;
float note = 261.63;
//TYPES OF VARIABLES:
/*Int is an integer variable, in Arduino variables aren't called var nor let
depending on the type of variable could be int, char for characters,float means
floating point numbers(decimals), long for very long numbers, boolean is a 
boolean variable containing to values true or false, byte is an integer number
from 0 to 255*/
//13 is an Arduino pin
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output because it could be also an input
  //and we want an output because a LED needs output of energy to work
  pinMode(13, OUTPUT);  
  pinMode(8, OUTPUT);   
}
void S(){
    //dot
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  //HIGH means maximum value that is 255 because it is a behind the scenes analog
  //output of 8 bits,2^8=256 possible values, from 0 to 255
  //digitalWrite means only two possible digital values HIGH and LOW
  delay(dot)              // wait for a dot time
  digitalWrite(13, LOW);// turn the LED off by making the voltage LOW
  noTone(8);
  delay(dot);               // wait for a dot 
  //dot
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  delay(dot);               // wait for a dot 
  digitalWrite(13, LOW);
  noTone(8);// turn the LED off by making the voltage LOW
  delay(dot);               // wait for a dot 
  //dot
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  delay(dot);               // wait for a dot
  digitalWrite(13, LOW);
  noTone(8);// turn the LED off by making the voltage LOW
  delay(dot);               // wait for a dot
  }
void O(){
  //dash
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  delay(dot*3);               // wait for a dash
  digitalWrite(13, LOW);
  noTone(8);// turn the LED off by making the voltage LOW
  delay(dot*3);               // wait for a dash
  //dash
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  delay(dot*3);               // wait for a dash
  digitalWrite(13, LOW);
  noTone(8);// turn the LED off by making the voltage LOW
  delay(dot*3);               // wait for a dash
  //dash
  digitalWrite(13, HIGH);
  tone(8, note);// turn the LED on (HIGH is the voltage level)
  delay(dot*3);               // wait for a dash
  digitalWrite(13, LOW);// turn the LED off by making the voltage LOW
  noTone(8);
  delay(dot*3);               // wait for a dash
}
// the loop routine runs over and over again forever:
void loop() {
  S();
  O();
  S();

}