// Bilkent IEEE RAS 2014 Fall Arduino Trainings // Lab1-Morse Code Solutions // For questions or comments please contact Ahmet Safa Ozturk - ahmetsafaozturk@gmail.com // This solution will be analysed at the beginning of the 2nd session. int alphamorse [36][9] = { { 1, 1, 3, 0, 0, 0, 0, 0, 0},//a { 3, 1, 1, 1, 1, 1, 1, 0, 0},//b { 3, 1, 1, 1, 3, 1, 1, 0, 0},//c { 3, 1, 1, 1, 1, 0, 0, 0, 0},//d { 1, 0, 0, 0, 0, 0, 0, 0, 0},//e { 1, 1, 1, 1, 3, 1, 1, 0, 0},//f { 3, 1, 3, 1, 1, 0, 0, 0, 0},//g { 1, 1, 1, 1, 1, 1, 1, 0, 0},//h { 1, 1, 1, 0, 0, 0, 0, 0, 0},//i { 1, 1, 3, 1, 3, 1, 3, 0, 0},//j { 3, 1, 1, 1, 3, 0, 0, 0, 0},//k { 1, 1, 3, 1, 1, 1, 1, 0, 0},//l { 3, 1, 3, 0, 0, 0, 0, 0, 0},//m { 3, 1, 1, 0, 0, 0, 0, 0, 0},//n { 3, 1, 3, 1, 3, 0, 0, 0, 0},//o { 1, 1, 3, 1, 3, 1, 1, 0, 0},//p { 3, 1, 3, 1, 1, 1, 3, 0, 0},//q { 1, 1, 3, 1, 1, 0, 0, 0, 0},//r { 1, 1, 1, 1, 1, 0, 0, 0, 0},//s { 3, 0, 0, 0, 0, 0, 0, 0, 0},//t { 1, 1, 1, 1, 3, 0, 0, 0, 0},//u { 1, 1, 1, 1, 1, 1, 3, 0, 0},//v { 1, 1, 3, 1, 3, 0, 0, 0, 0},//w { 3, 1, 1, 1, 1, 1, 3, 0, 0},//x { 3, 1, 1, 1, 3, 1, 3, 0, 0},//y { 3, 1, 3, 1, 1, 1, 1, 0, 0},//z { 1, 1, 3, 1, 3, 1, 3, 1, 3},//1 { 1, 1, 1, 1, 3, 1, 3, 1, 3},//2 { 1, 1, 1, 1, 1, 1, 3, 1, 3},//3 { 1, 1, 1, 1, 1, 1, 1, 1, 3},//4 { 1, 1, 1, 1, 1, 1, 1, 1, 1},//5 { 3, 1, 1, 1, 1, 1, 1, 1, 1},//6 { 3, 1, 3, 1, 1, 1, 1, 1, 1},//7 { 3, 1, 3, 1, 3, 1, 1, 1, 1},//8 { 3, 1, 3, 1, 3, 1, 3, 1, 1},//9 { 3, 1, 3, 1, 3, 1, 3, 1, 3},//0 }; char alphabet [36] = "abcdefghijklmnoprstuvwxyz1234567890"; int ledPin = 13; // assigning pin 13 to the led int t = 1000; // time of one dot void setup() { pinMode( ledPin, OUTPUT); // setting ledPin as output Serial.begin(9600); // starting the serial - assume 9600 as a constant } void loop() { char ch; // declining a new char ch = Serial.read(); // reading char from the user morse( ch); // using my morse function delay( 3000); // time between two letters } void morse( char ch) // morse function which converts the char to the led morse code { int index = 0; // index of the letter or the number // finding where is the entered character for( int i = 0; i < 36; i++) { if( alphabet[i] == ch) { index = i; break; } } // converting this character to the morse code for( int i = 0; i < 10; i+=2) { digitalWrite(ledPin, HIGH); delay( t*alphamorse[index][i]); if( i != 8) { digitalWrite(ledPin, LOW); delay( t*alphamorse[index][i+1]); } } digitalWrite(ledPin, LOW); // after every character theres is a 2 dot time delay in morse code delay( t*2); }