In: Computer Science
Using Arduino to blink an LED in morse code.
Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for 0.3 seconds and then off for 0.3 seconds; express one “dah” with LED on for 0.9 seconds and then off for 0.3 seconds; express the space between letters with LED off for 0.9 seconds; and express the space between words with LED off for 2.1 seconds.
Please up vote ,comment if any query . Thanks for question .Be safe .
Note : check attached image for simulation schematic . Code compiled in arduino ide . simulation tested in proteus VSM.
Program Plan :
Program :
#define morseLed 13 //define led pin 13 PB5
String phrase="We are students"; //string of we are students
String morsePhrase=".-- . / .- .-. . / ... - ..- -.. . -. - ...";
//morse code
void setup() {
pinMode(morseLed,OUTPUT); //led pin set OUTPUT
DDRB|=(1<<PB5)
//DDRB|=(1<<PB5);
delay(100);
}
void loop() {
//run a loop from 0 index to till length of string
moresephrase
for(int i=0;i<morsePhrase.length();i++)
{
if(morsePhrase[i]=='.') //if char is dit call dit message
dit();
else if(morsePhrase[i]=='-') //if char is dah call dah
message
dah();
else if(morsePhrase[i]==' ') //if char space call charSpace
message
charSpace();
else if(morsePhrase[i]=='/') //if char is / call wordspace
message
wordSpace();
}
delay(5000); //each iteration 5 seconds delay
}
void charSpace()
{
digitalWrite(morseLed,LOW); //set low on led
//PORTB&=~(1<<PB5);
delay(900);
}
void wordSpace()
{
digitalWrite(morseLed,LOW);
//PORTB&=~(1<<PB5);
delay(2100);
}
void dit()
{
digitalWrite(morseLed,HIGH);
//PORTB|=(1<<PB5);
delay(300);
digitalWrite(morseLed,LOW);
//PORTB&=~(1<<PB5);
delay(300);
}
void dah()
{
digitalWrite(morseLed,HIGH);
//PORTB|=(1<<PB5);
delay(900);
digitalWrite(morseLed,LOW);
//PORTB&=~(1<<PB5);
delay(300);
}
Schematic :
Please up vote ,comment if any query .