Project 1: Morse Code
The CS purposes of this project are to give you experience with writing code, using functions, writing functions, and working with your Arduino and breadboard. The topic of the lab is to generate Morse code signals using light and sound.
Part 1: Code
- Open the Arduino IDE and start with a new file. Put your name, Project 1, and a date at the top of the file. Save the file/sketch to your project1 directory.
- Start by writing a main function that starts
the same way as the file you created at the end of the lab. It
should have two variables: one defines the active output pin,
the other defines the unit of time for the Morse code
signals. Start with your output pin as 13. The main function
should start by calling the
init()function, then set up the pin by calling
pinMode()with the pin number and OUTPUT as the arguments.
- Your Arduino can send messages back to the host computer using
serial communication (serial communication means sending one bit at a time). To set
up serial communication at the start of your main function, use the code:
To test the serial communication, in your main function add the code:
Serial.write("Hello world");and then upload and test. To see the serial output, use the Tools::Serial Monitor.
- The IDE allows you to split your code across several files. For most projects it's a good idea to have the main function in one file and then split the other functions you create across one or more other files. For this project we'll keep all of the other functions in a second file. Click on the down arrow on the right of the IDE to create a new Tab and give the tab a name (e.g. morseLibrary).
- Recreate your dot and dash functions from
lab in the morseLibrary file.
As you complete tasks, it's always good to test your code and make sure everything is working. Call your dash and dot dot functions from the main function and then upload and test your program.
- Create a function
void sendH(int unit, int pin)that makes the given pin blink an H, which is four dots. At the end of the letter, the function should delay for three units total (remember, the dash and dot functions already delay by one unit). Once the letter is sent, use the
Serial.write()function to write the letter just transmitted.
Create additional functions for E, L, O, W, R, and D.
- Create a function for the word HELLO (e.g.
void sendHELLO(int unit, int pin)). Create a second function for the word WORLD. The word functions should delay a total of seven units after finishing the word (remember, a letter pauses for 3 units). Have the word functions send a space on the serial port after completing the word.
- Have your main function send the message
HELLO WORLD in Morse code. You should see the LED blink the
correct pattern, and the same message should appear on the
Required video 1: Take a video of your board blinking the HELLO WORLD message in Morse code.
- Create your own message, adding whatever letter and word functions you need to your morseLibrary code. Add the message to your main function so it sends both HELLO WORLD and your message.
Part II: Board
- Find your breadboard (A), 3 LEDs of the same color (B),
the piezo-electric speaker, and one each of a 560 Ohm (C), 2.2
kOhm (D), and 10 kOhm (E) resistor. You can read the value of
a resistor using the color
codes. You will also want six wires. By convention, black
wires are generally used for ground (or LOW), red wires for 3.3V or 5V (HIGH),
and other colors for signals that carry information. Following
these conventions, especially for ground and hot wires, can help
you avoid damaging circuit elements.
Breadboard Green LEDs Piezo-electric
Set up three LED circuits. Use an LED and resistor for
each circuit. The longer pin of the LED should be
connected to the driving voltage (i.e. the driving pin
from the board). The resistor should connect the short pin
of the LED to ground.
Your breadboard contains connections between pins in a certain pattern. For more information, you can check out this quick tutorial.
One method of wiring the circuit is to have the LED straddle the center divider, connect one of the power rails to ground, and then connect a resistor from the short side of the LED to the ground rail. Use one of the wires to connect the Metro board pins to the long pin on the LED. Your final circuit might look something like the image below.
3 LED circuit
You can use any of the digital output pins for the LEDs. I used pins 2, 4, and 6, as below. We'll be using pin 9 later, so don't use it for an LED.
3 LED circuit
- Create a new file in the Arduino IDE. Save it as leds in your project 1 directory. Create a main function that calls init(), as above.
- Write the code to create three int variables--
pin560--corresponding to each of the three LED circuits and their associated resistor values. Assign to each variable the value of its corresponding pin (e.g. 2, 4, and 6 if you followed the diagramb above).
Add code to call
pinMode()for each of the pins, setting them all to OUTPUT mode. Then use
digitalWrite()to write a HIGH value to each pin. Delay five seconds, then set all of the pin values to LOW.
- Run your program. Note the intensity of each
LED. Which one is brightest? Which is the most dim?
Required image 1: take picture of your circuit to include in your report.
- Find the piezo-electric speaker/buzzer
element in your kit. Connect one of its leads to pin 9 and
the other to ground [GND]. The leads are interchangeable
for this circuit element. Your breadboard should look
something like the following.
3 LED + Speaker circuit
- In your main function, add code to set up
pin 9 as an OUTPUT. When you set the LED values to HIGH, use
tone()function to tell pin 9 to provide a 440Hz signal for 1000ms. Upload your code. The speaker should give a 1s sound. Try changing the frequency argument and see what happens.
Return to your Morse code files. In your main function,
modify the LED pin to use your brightest LED. Also in
your main function, set up pin 9 as an OUTPUT. Then, in
your dot function, call the
tone()function for pin 9 to play at 220Hz for the amount of time specified by
unit. In your dash function, call the
tone()function for pin 9 to play for 3 times the time specified by
unit. Upload and run your code. It should flash the brightest LED, play a tone, and send letters back to the serial port.
Required image 2: take a screen shot of your serial console with the output of your program displayed. Include this image in your report.
Required video 2: take a short video of your Morse code circuit working with both HELLO WOLRD and your custom message..
- What was the relationship between resistor size and brightness in your LED circuits?
- What is your hypothesis about the cause of the relationship between LED brightness and resistor size?
- How many parameters does digitalWrite( ) accept, what type are they (e.g. int, float, or String), and what do their values describe? Note their units (e.g. meters or seconds) if applicable. Use the Arduino reference page to guide your answer.
- How many parameters does delay( ) accept, what type are they, and what do their values describe? Note their units (e.g. meters or seconds) if applicable.
- What rules of Morse code allow you to differentiate letters and words? How does this relate to prefix codes?
Projects are your opportunity to learn by doing. They are also an opportunity to explore and try out new things. Rather than list out every activity for a project, the defined part of each project will be about 85% of the assignment. You are free to stop there and hand in your work. If you do all of the required tasks and do them well, you will earn a B/B+.
To earn a higher grade, you can undertake one or more extensions. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. Extensions are your opportunity to customize the assignment by doing something of interest to you. Each week we will suggest some things to try, but you are free to choose your own.
A common question is "how much is an extension worth?" The answer to that question depends on the extension, how well it is done, and how far you pushed it. As teachers, we prefer to see one significant extension, done well, where you explore a topic in some depth. But doing 2-3 simpler extensions is also fine, if that's what you want to do. Choose extensions that you find interesting and challenging.
The following are a few suggestions on things you can do as extensions to this assignment. You are free to choose other extensions, and we are happy to discuss ideas with you.
- Explore other ways of controlling the
speaker device. You can, for example, use the analog pins
and the function
analogWrite(). Explore the differences in using analog control versus the tone() function.
- Have your speaker play a tune. Make the LEDs flash to the beat. Use functions to represent sequences of notes or LED patterns and then combine calls to those functions in other functions to create complex combinations.
- Figure out how to make a set of instructions repeat, either continuously or a fixed number of times. Use that capability to do more complex things with the LEDs or speaker.
- Explore using the push button, which will involve making a pin be an INPUT. Use the value of the input to control the signal being passed to the LED or speaker.
Each week you will write a brief report about your project. In general, your intended audience for your write-up is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your wiki report should explain to friends what you accomplished in this project and to give them a sense of how you did it.
Your project report should contain the following elements.
- Abstract: a brief summary (200 words or less) of
the task, in your own words. give the reader context and
identify the key purpose(s) of the project. You can assume the
reader has read your prior assignments.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application (e.g. encoding Morse code)?
- Does it describe your the solution or how it was developed (e.g. what code did you write/circuits did you build)?
- Does it describe the results or outputs (e.g. did your code and circuit work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
- A description of your solution to the tasks. This should be a description of the form and functionality of your final code and the design of your breadboard circuits. Try to describe your algorithm or code without including actual code in your report. Using 1-2 lines as an example is acceptable. Using simple diagrams or pictures of your board may be helpful when describing your circuits. Note any unique computational solutions or hardware circuits you developed.
- A description of any extensions you undertook, including images, videos, or diagrams demonstrating those extensions. If you added any functions, or other design components, note their structure and the algorithms you used.
- The answers to any follow-up questions.
- A brief description (1-3 sentences) of what you learned.
- A list of people you worked with, including TAs, and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
- Don't forget to label your writeup so that it is easy for others to find. For this lab, use cs153f18project1
Mount the Courses volume. Navigate to the Private sub-directory. Create a new folder called project01. (It's best to avoid spces in the directory name.) Each week, the following items should be submitted here.
- Code that should graded and any required supporting materials. This week, put your morse.ino, morseLibrary.ino, and leds.ino files here. Code from lab exercises will generally not be graded unless it is used as part of the project tasks.
- Videos of your work. You can also submit videos as part of your report on the wiki, but please put a copy of the video here.
Your report should be submitted as a wiki page with the appropriate label, as noted above.