I had no issues building the circuit. I combined some of the example code with some of my morse code prorgam from project 1 to create a program that buzzes my name (Adam) in morse code with the tone of the buzzing set by the combination of buttons pressed and the potentiometer controlling the volume. Pressing only one of the buttons sets the tone to either c, e, or g. Pressing the buttons for tones c and e at the same time sets the tone to d, pressing the buttons for tones e and g at the same time sets the tone to f, and pressing the buttons for tones c and g or pressing all three buttons at the same time sets the tone to e. Here is the code:
//set the pins for the button and buzzer
int firstKeyPin = 2;
int secondKeyPin = 3;
int thirdKeyPin = 4;
int buzzerPin = 10;
void setup() {
//set the button pins as inputs
pinMode(firstKeyPin, INPUT_PULLUP);
pinMode(secondKeyPin, INPUT_PULLUP);
pinMode(thirdKeyPin, INPUT_PULLUP);
//set the buzzer pin as an output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
letterA();
longSpace();
letterD();
longSpace();
letterA();
longSpace();
letterM();
longerSpace();
}
int frequency(){
if(button1() && !button2() && !button3()){ //if only the first key is pressed
return 262; //play the frequency for c
}
else if(button1() && button2() && !button3()){ //if the first and second keys are pressed
return 294; //play the frequency for d
}
else if((button1() && button2() && button3()) || //if all three keys are pressed
(!button1() && button2() && !button3()) || //or if the first and third keys are pressed
(button1() && !button2() && button3())){ //or if only the second key is pressed
return 330; //play the frequency for e
}
else if(!button1() && button2() && button3()){ //if the second and third keys are pressed
return 349; //play the frequency for f
}
else if(!button1() && !button2() && button3()){ //if only the third key is pressed
return 392; //play the frequency for g
}
else {
return 0; //if no key is pressed turn the buzzer off
}
}
boolean button1(){
return digitalRead(firstKeyPin) == LOW;
}
boolean button2(){
return digitalRead(secondKeyPin) == LOW;
}
boolean button3(){
return digitalRead(thirdKeyPin) == LOW;
}
int ditDuration(){
return 100; //100 milliseconds
}
void letterA(){
dit();
space();
dah();
}
void letterD(){
dah();
space();
dit();
space();
dit();
}
void letterM(){
dah();
space();
dah();
}
void dit(){
if(frequency() == 0){
noTone(buzzerPin);
} else {
tone(buzzerPin, frequency());
}
delay(ditDuration()); // The duration of a dit is set by the ditDuration() function.
}
void dah(){
if(frequency() == 0){
noTone(buzzerPin);
} else {
tone(buzzerPin, frequency());
}
delay(3*ditDuration()); // The duration of a dah is 3 times that of a dit.
}
void space(){
noTone(buzzerPin);
delay(ditDuration()); // The duration of a space is equal to that of a dit.
}
void longSpace(){
noTone(buzzerPin);
delay(3*ditDuration()); // The duration is 3 times that of a dit, used between characters.
}
void longerSpace(){
noTone(buzzerPin);
delay(7*ditDuration()); // The duration is 7 times that of a dit, used between words.
}
This program's code is a combination of the SIK Guide example code and the code from my morse code programs from project 1.
Here is a video of the program in action:
One issue I had while writing the porgram was that I originally had tone() calls with the frequency parameter set to zero in the space(), longSpace(), and longerSpace() functions. This resulted in a faint buzzing being produced when there should have been no buzzing in the morse code produced by the buzzer. I fixed this problem by using the noTone() function instead of the tone() function.
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.