However, we are prohibited from using any loops except the provided loop() in arduino.
I don't think using delay( ) is a big issue in a Morse sender. The only thing in the code posted below which violates the prohibition is the while block when sending a character. As @westfw says, that will need to be turned into a routine which counts the dit and dahs and is called multiple times in loop until the character is completed.
void flashSequence (char* sequence)
{
int i = 0;
while (sequence[i] != NULL)
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); //gap between letters, international standard
}
//Sketch 5_05Test Morse Interpreter including punctuation
int ledPin = 13;
int dotDelay = 200; //set the delay to a quarter second
char ch;
byte newch;
char* letters[] =
{
".-", "-...", "-.-.", "..", ".", //A-Z
"..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--.."
};
char* numbers[] =
{
"-----", ".----", "..---", "...--", "....-", //numbers 0-9
".....", "-....", "--...", "---..", "----."
};
char* punct[] = // punctuation marks in order of frequency
{
".-.-.-", "--..--", ".----.", "..--..", // . , ' ?
"-.--.", "-.--.-", "-.-.--", "-.-.-.", // ( ) ! ;
"---...", ".-..-.", ".--.-.", "-....-", // : " @ -
"..--.-", ".-.-.", "...-..-", "-...-", // _ + $ =
".-..." // &
};
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("To send text to be displayed in Morse Code, type words above and press Send");
}
void loop()
{
if (Serial.available() > 0)
{
ch = Serial.read(); //read a single letter
if (ch >= 'a' && ch <= 'z') // is it lower case letter?
{
flashSequence(letters [ch - 'a']);
}
else if (ch >= 'A' && ch <= 'Z') // is it a capital letter?
{
flashSequence(letters [ch - 'A']);
}
else if (ch >= '0' && ch <= '9') // is it a number?
{
flashSequence (numbers[ch - '0']);
}
else if (ch >= 33 && ch <= 95) // is it punctuation?
{
punctuationTranspose();
flashSequence (punct[newch]);
}
else if (ch == ' ') // is it a blank space?
{
delay(dotDelay * 7); //gap between words
}
{
Serial.print(ch);
}
}
}
void flashSequence (char* sequence)
{
int i = 0;
while (sequence[i] != NULL)
{
flashDotOrDash(sequence[i]);
i++;
}
delay(dotDelay * 3); //gap between letters, international standard
}
void flashDotOrDash (char dotOrDash)
{
digitalWrite(ledPin, HIGH);
if (dotOrDash == '.')
{
delay (dotDelay);
}
else // must be a -
{
delay (dotDelay * 3); //long dah 3x dit international standard
}
digitalWrite(ledPin, LOW);
delay(dotDelay); //gap between flashes, international standard
}
void punctuationTranspose()
{
switch (ch)
{
case '.':
newch = 0;
break;
case ',':
newch = 1;
break;
case 39:
newch = 2;
break;
case '?':
newch = 3;
break;
case '(':
newch = 4;
break;
case ')':
newch = 5;
break;
case '!':
newch = 6;
break;
case ';':
newch = 7;
break;
case ':':
newch = 8;
break;
case '"':
newch = 9;
break;
case '@':
newch = 10;
break;
case '-':
newch = 11;
break;
case '_':
newch = 12;
break;
case '+':
newch = 13;
break;
case '$':
newch = 14;
break;
case '=':
newch = 15;
break;
case '&':
newch = 16;
break;
}
}