We Speak PC
  • Home
  • Resources
  • Gallery

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

Arduino Programming and Electronics

Post Image

Project 4: Extended Morse Code with on/off button (switch), and IF statements.



The great thing about programing an Arduino is that you can make things happen and you can interact with it. We are going to work on your Morse code again and simulate this with a button.



Your previous working project.

1 10K Ohm resistor

1 pushdown button




How to wire it up:



How the push button works:

pushbutton inside

- When the push button is not pressed, the 5V goes to ground. Pin 2 reads off (or LOW)

- When you press the button, All pins get connected and the 5V goes to Pin 2 because it is the path of least resistance. Pin 2 will at this point read on (or HIGH)


THE PROJECT: (Controlled SOS)

Plug in your arduino and let's start your first program.

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

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

3. We are using your previous program so the only things in red should be added/modified areas of your program. 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, Push Button and If Statements
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
int BUTTON = 2;

void setup ( ) { //set up the Pins we are going to use for the LED
pinMode  (led, OUTPUT);
pinMode (BUTTON, INPUT); //This tells the Arduino to check if the button is pressed

} /* 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.

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. Didn't I just say that?


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

if (digitalRead(BUTTON) == LOW){    // This tells the arduino that if you push the button then turn everything off . Notice the bracket , it means that if the button is pressed, do every command within the following brackets, otherwise ignore it.

digitalWrite(led, LOW);


else {     // this means that if the button is not pressed then do everything wtihin the following brackets

// S in morse code


//O in morse code


// S in morse code


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





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.

Same Activities for practice:

  1. Can you adust the program to only send the SOS signal when the button is pressed?
  2. Can you adjust the program to send out your initials? (First, Middle, Last name)