6. Practice programming Arduino¶
In this lesson, we will practice using the programming techniques we learned in the previous lesson. I advise keeping that window open for reference as you work through this lesson, which mostly consists of practice.
To further demonstrate how software allows for rapid prototyping, we will use exactly the same physical setup, at least to begin with, as we used in the blink exercise.
Follow-along exercise 2: Alien contact¶
Now that we have the pieces of the language in place, let’s put them to use!
In the 1997 film Contact (based on the Carl Sagan book of the same name), alien beings from the Vega star system make contact with people on Earth. They do it by sending a sequence of prime numbers. You can see the discovery in this video. In the video, the aliens send the prime numbers as pulses one second apart, with a two-second pause between primes. After they get to 101, they pause for 43 seconds, and then send prime numbers again, starting at 2.
Our task now is to use the same setup we had for our blink exercise to make the LED flash the prime numbers like the aliens did in the movie. We do not need to change the board configuration, only the code.
The following Arduino code accomplishes the extraterrestrial communication.
// Set pin 6 as pin controlling LED
const int ledPin = 6;
// List of all primes up to and including 101
const int nPrimes = 26;
const int primes[nPrimes] = {
2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101
};
void flash(int ledPin) {
/*
* Flash the LED over one second.
*/
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
void flashSequence(int ledPin, int nFlashes) {
/*
* Flash the LED nFlashes times, with one second per flash.
*/
for (int i = 0; i < nFlashes; i++) flash(ledPin);
}
void setup() {
// LED pin is output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Loop through each prime
for (int i = 0; i < nPrimes; i++) {
// Flash prime number of times
flashSequence(ledPin, primes[i]);
// Wait two seconds until the next prime
delay(2000);
}
// Wait 43 seconds until transmitting again, just like in the movie
delay(43000);
}
Everything in this sketch could be constructed from the notes in Lesson 5. Please carefully read it and make sure you understand it. Then, upload it to the Arduino Uno, and send your LED signals just like the aliens!
There is no need to turn in this follow-along exercise, since you will expand upon it momentarily in a do-it-yourself exercise.
Do-it-yourself exercise 1: Alien contact with audio¶
A piezo buzzer is a device that can make sound by varying an electric field around a sheet of piezoelectic material. Piezoelectrics undergo mechanical deformations in response to accumulation of electric charge. By varying the voltage periodically, the sheet vibrates in the piezo buzzer, producing sound.
You should have at least one piezo buzzer in your kit. It is black and disk-shaped. Add it to your set up. Be sure to connect the red lead to a digital pin of the Arduino and the black lead to ground (you do not need resistors).
Update your code such that the buzzer is buzzing when the LED is on. Hint: Read the documentation in the Arduino language reference about the
tone()
andnoTone()
functions. Another hint: You need to choose a frequency for the buzzer. Middle C is 262 Hz.
Now, put the device in action!
This is the first exercise you will submit. Remember to submit your exercise as a ZIP file according to the course policies.
Do-it-yourself exercise 2: A proper Hello, world.¶
Our “Hello, world.” exercise was Blink. But let’s properly say “Hello, world.” Leaving your physical setup the same as in the previous exercise, say “Hello, world.” with Morse code. Your light and piezo buzzer should be activated at the same time so that the Morse code message is received both visually and audibly.
Here are some rules for Morse code.
The unit of time is a dot duration. I suggest using 60 milliseconds, which is common.
A letter or punctuation is comprised of one or more dots-and-dashes in succession.
A dot lasts, you guessed it, one dot duration.
A dash lasts three times as long as a dot.
Each dot and dash in a letter or punctuation mark is separated by one dot duration.
A pause equal to three dot durations is used between letters in a word.
A pause equal to seven dot durations is used between words.
The following aligned arrays give characters and their corresponding Morse code representations.
const String characters = String("ABCDEFGHIJKLMNOPQRSTUVWXYZ,.");
const String codes[28] = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--..",
"--..--",
".-.-.-"
};
This exercise is a bit more challenging, and I suspect you will want to refer to the Aduino language reference as you go through, in particular looking up which useful methods are available for String
instances.
When you submit this exercise, you do not need to show a schematic, since this schematic is the same as in the previous exercise.