/*
To do:
* Add buzzer functions
* Abstract more if possible
*/
// Set pin 6 as pin controlling LED
const int ledPin = 6;
const int buzzPin = 5;
const int dot = 60;
const int dash = 3*dot;
const int freq = 262;
int onTime;
int offTime = dot;
String letter;
// Lists all characters in alphabet and again in Morse code
const String characters = String("ABCDEFGHIJKLMNOPQRSTUVWXYZ,.");
const String codes[28] = {
".-",
"-...",
"-.-.",
"-..",
".",
"..-.",
"--.",
"....",
"..",
".---",
"-.-",
".-..",
"--",
"-.",
"---",
".--.",
"--.-",
".-.",
"...",
"-",
"..-",
"...-",
".--",
"-..-",
"-.--",
"--..",
"--..--",
".-.-.-"
};
// these arrays hold the indices of the letters in each word
int helloSequence[] = {7, 4, 11, 11, 14};
int worldSequence[] = {22, 14, 17, 11, 3};
void flashbuzz(int ledPin, int onTime) {
/*
Turns the LED on, waits for onTime,
then turns off for offTime
*/
digitalWrite(ledPin, HIGH);
tone(buzzPin, freq);
delay(onTime);
digitalWrite(ledPin, LOW);
noTone(buzzPin);
delay(offTime);
}
String findLetter(int index){
String letter;
letter = codes[index];
return letter;
}
int dotordash(char input){
// Determines if onTime should be a dot or dash length
int onTime;
if(input == "."){
onTime = dot;
}
else if(input == "-"){
onTime = dash;
}
return onTime;
}
void setup() {
// LED pin is output
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
for(int i = 0; i< sizeof(helloSequence)/sizeof(helloSequence[0]);i++){
letter = findLetter(helloSequence[i]);
Serial.println(letter);
for(int j = 0; j< sizeof(letter)/sizeof(letter[0]);j++){
onTime = dotordash(letter[j]);
flashbuzz(ledPin, onTime);
delay(offTime);
}
}
delay(7*offTime);
for(int i = 0; i< sizeof(worldSequence)/sizeof(worldSequence[0]);i++){
letter = findLetter(worldSequence[i]);
Serial.println(letter);
for(int j = 0; j< sizeof(letter)/sizeof(letter[0]);j++){
onTime = dotordash((letter[j]));
flashbuzz(ledPin, onTime);
delay(offTime);
}
}
delay(2000);
}