Arduino Workshop for the Curious/Lesson 2

From Electromagnetic Field

Functions

Functions are like variables, but instead of storing data, they store code. This is useful if you executing some code lots of times in different parts of your program, because it stops you having to repeat yourself. You just have to put the code inside a function and use the function everywhere instead.

For example, since we're making a LED-Based Morse Code Emitter we'll probably be creating light pulses very often. It will be useful if the code to do this was placed into a function, so let's have a look at how that is done:

void pulse() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
}

This function is called pulse and creates a one second pulse on the LED. The word void means that this function doesn't return an answer - it just runs the code inside and that's it. Some functions do return data and so wouldn't have the word void there.

The problem with this function is that it always creates pulses of the same length. We want to create pulses of different lengths (for the dots and dashes of Morse Code.) To achieve this, we can provide the function with an integer number as input, and it will then use that to create a pulse of the correct length. This is how it can be done:

void pulse(int length) {
  digitalWrite(led, HIGH);
  delay(length);
  digitalWrite(led, LOW);
}

The whole code for creating dot-dash-dot-dash pulses on the LED is now:

int led = 13;

void setup() {                
  pinMode(led, OUTPUT);     
}

void loop() {
  pulse(1000);
  delay(500);
  pulse(500);
  delay(500);
}

void pulse(int length) {
  digitalWrite(led, HIGH);
  delay(length);
  digitalWrite(led, LOW);
}

Challenge Yourself.jpg

(1) Create a dot() and a dash() function which cause cause the LEDs to flash a dot and a dash.

(2) Use a variable to hold the length of a dot (around 200ms). Use the rules below to create the dash length from the dot length.

Morse Code Rules

  • A dash is equal to 3 dots
  • The space between parts of the same letter is equal to one dot
  • The space between letters is equal to three dots
  • The space between words is equal to seven dots

If-Statements

Now we're going to make yet another function, which will have the job of converting letters to morse code. To keep things simple, we will pretend the alphabet has only the letters S and O right now. More letters can be added later. S in morse code is "ยทยทยท" and O in morse code is "---".

The logic for this function is as follows: If we are given an S as input then we will output 3 dots. If we are given an O as input then we will output 3 dashes. Otherwise (else) we will output nothing. At the end we will output an inter-letter space (equal to 3 dots in length.)

void morse(char letter) {
  if(letter == 'S') {
    dot(); dot(); dot();
  }
  if (letter == 'O') {
    dash(); dash(); dash();
  }
  delay(3*dotLength);
}

In order to make the dots and dashes easy to see we placed them on the same line in the code with semicolons separating them. Separate C-statements are normally placed on separate lines, but in this case I thought it looked better on one line. If you're unclear about the if statement, you can see an explanation here.

Challenge Yourself.jpg

(1) Copy the code above into your program and see if you can get the Arduino outputting SOS.

(2) Add the space character to your morse code function, and then get the Arduino outputting SOS SOS SOS..." (note the spaces between the words).

If you're having trouble at this stage, you can find the code for this section here.

Personal tools