We Speak PC
  • Home
  • Resources
  • Gallery

  •              Allen@wespeakpc.com
                 Call us: 972-704-5114

Arduino Programming and Electronics

Post Image

Project 3: S.O.S. Libraries & Functions

History:

Before telephones existed, Morse Code was invented to be able to send signals to people over a long distance. The combination of tones would tell the person on the other line what letter they are trying to say.

S.O.S. is a very famous signal. it is a universal signal meaning HEEEEEELP!!! The tones for S.O.S. are 3 short beeps (S), followed by 3 long beeps(O) and finished with 3 short beeps again (S).

We are going to learn to change letters into blinking lights and sounds. We are also going to learn how to use what is called functions. You are not new to functions. You have been using them since tutorial 1. Do you remember these:

void setup ( )
void loop ()
digitalWrite ()
pinMode  ()

 

What is a Library, and what is a Function and why we need them:

A funtion is basically a set of code written to do a specific task. Like the Pinmode() function tells the Arduino how to use the pin. We give each function a name so that the arduino knows what to look for and what to do.

For example: If SOS is three short beeps, followed by 3 long beeps, followed by 3 short beeps again; we would have to write the code to turn on a beep, turn it off, turn it on, turn it off...... That is (maybe) over 100 lines of code writing for 3 letters and that gets pretty long and boring.

If we write/create a function for turning on short beeps, turning on long beeps, and turning off all beeps, then to write SOS, that would only be about 18 lines of code. We will discuss functions more later.

A Library is a collection of programs or functions that have already been written & saved somewhere for the arduino to use. This way when you need to use different functions, you don't have to rewrite them. We just need to make sure we tell the Arduino to look in a library. For this program we will use the library already built into the Arduino so nothing special needed for now.

 

MATERIALS NEEDED:

Arduino board

Arduino USB Cable

Computer

1 LED

1 Speaker

1 solderless breadboard

Jumper Wires

2 220 or 300 ohm resistors.

 

How to wire it up:

Click here to download a wiring diagram

 

How a Engineer may see it:

Project 3 Wiring

 

 

THE PROJECT: (SOS)

Plug in your arduino and let's start.

1. Double-click the Arduino icon on your desktop.

2. Click on file, Save as , then give it the name SOS.

3. Copy the following that is in red only. (Remember to be exact. Even the smallest mistake might make your program not work).  We will break it down as you write this program in normal ink since you should be familiar with most.

/*
My Project Name:  SOS
What my project does:Translates SOS letters to Morse code and showing it in lights and sound
*/

int led = 10; //This is the pin our LED will be plugged into
int spkr = 8;
// This is where the speaker will be plugged into
int musicnote = 1200;
// This will be how high of a sound we want
int dot = 100;
//how long we want the dot sound to play
int dash = 300;
//How long we want the dash sound to play

void setup ( ) { //set up the Pins we are going to use for the LED
pinMode  (led, OUTPUT);
} /* END SETUP */

//Now we want to create our new functions. We should create one for Dots, Dashes, and to turns off so that we can make a new sound.

void LEDoff () { //this is a function to turn off the LED
digitalWrite (led, LOW);
//turn off LED
noTone (spkr);
//turn off sound with a function Arduino already knows about
delay (dot); //keep the LED and Sound off for a brief moment
} /*END LEDoff */

void dots() { //this is a function for Dots
digitalWrite (led, HIGH);
//turn on our LED
tone (spkr, musicnote, dot);
//make a sound with a funtion Arduino already knows about
delay (dot);
//Keep the light on for a short time
LEDoff ();
//We can call a function within a funtion....NEAT HUH!?!?! saves us from rewriting a lot.
} /* END DOTS FUNCTION */

void dashes() {
//this is a function for dashes
digitalWrite (led, HIGH);
//turn on the LED
tone (spkr, musicnote, dash);
//make a sound with a funtion Arduino already knows about
delay (dash);
//Keep the light on for a short time
LEDoff ();
//We can call a function within a funtion....NEAT HUH!?!?! saves us from rewriting a lot.
} /* END DASH FUNCTION */

void loop (){
//keep saying SOS with lights and beeps

// S in morse code

dots();
dots();
dots();

//O in morse code

dashes();
dashes();
dashes();

// S in morse code

dots();
dots();
dots();

//Pause to show end of message until it repeats again

delay(1000);

} /* END LOOP FUNCTION */

 

***********************
Now it is time to see if it works.  Click in the check mark on the top left. If there are any errors it will start pointing them out to you.  if you just can’t figure it out please let me know and I will help you out.  once it says it is done, then click on the right arrow button next to it to copy your program from your computer to the Arduino board.

Activities:

  1. Can you adjust the program to change beep and sound lengths?
  2. Given the picture below, Can you code your name in morse code?: