Here is a more complex example: sending two morse messages concurrently. Note where cases: are located in this sketch: a case can be declared within a while. ( a if, or a for loop). Nice!
For those interested, I have written an alarm clock with setting buttons, display, beeper etc. One can easily and transparently send 2 morse messages while processing the alarm clock... while regulating a temperature, etc.
HAVE FUN!
[code]
#include "Timer.h"
//--------------------------------CLASS MORSE SENDER---------------------------------
char morseTable[][9] = { //Note: 9 because the longest morse char has 8 symbols (dots and dashes)1=dot, 2=dash, 3=pause
{'A', 1, 2, 0}, //a dot dash. The last 0 indicates the end of character.
{'B', 2, 1, 1, 1, 0}, //b is dash dot dot dot
{'C', 2, 1, 2, 1, 0}, //c is dash dot dash dot
{'D', 2, 1, 1, 0}, //d is dash dot dot
{'E', 1, 0}, //e is dot
{'F', 1, 1, 2, 1, 0}, //f is dot dot dash dot
{'O', 2, 2, 2, 0}, //o dash dash dash
{'S', 1, 1, 1, 0}, //s dot dot dot
{' ', 3, 0}, //a space char is 2 pauses
//space between characters
// etc.. code all letters
{0,} //end of table
};
//----------------------------------------
class morseSender {
public:
char msg[33] = {"abcdef sos " }; //A null terminated string, 32 char max see arduino-reference\string
// //A null at index 0 indicates there is no more message to send.It aborts the task.
//initialize variables with ...begin
private:
int morsePin = 13; //pin number that activates a bip
unsigned int dotLen = 0; //dot duration
unsigned int dashLen = 0; //dash duration
unsigned int pauseLen = 0; //the pause (time) between 2 char
timer timer; //a local timer named timer.
int state = 0;
int msgIndex = 0; //an index in msg pointing the char beeing sent / to send.
int lineIndex = 0; //an index pointing a line in morseTable
int columnIndex = 0; //an index pointing a column in morseTable
int len = 0; //the len of the bip beeing sent (dotLen or dashLen): to allow calculating this len once
//----------------------------------------
int findMorseCode(char x) { //returns an index in morseTable for the char, or -1 if the char does not exist.
int index = 0; //an index for searching in morseTable
while (morseTable[index][0] != 0) {
if (morseTable[index][0] == x) {
return index; //returns the index if char is found in morseTable
}
else index++;
}
return -1; //otherwise returns -1.
}
public:
//----------------------------------------
void begin(int aMorsePin, int aDotLen, int aDashLen, int apauseLen ) {
//Serial.println("initializing...");
morsePin = aMorsePin;
dotLen = aDotLen;
dashLen = aDashLen;
pauseLen = apauseLen;
state, msgIndex, columnIndex = 0;
pinMode(aMorsePin, OUTPUT);
}
//----------------------------------------
int msgBuffLen() {
int x = sizeof(msg);
return (x);
}
//----------------------------------------
bool sendingMorse() {
if (msg[0] != 0 ) {
switch (state) {
case 0:
while (msg[msgIndex] != 0) { //while there are char to send
if (isAlpha(msg[msgIndex])) {
bitClear(msg[msgIndex], 5); //make this alpha char lower case.
}
lineIndex = findMorseCode(msg[msgIndex]); //find the code to send for the char.
if (lineIndex >= 0) { //the char has been found in morseTable.
// Serial.print("sending "); Serial.println(morseTable[lineIndex][0]);
columnIndex = 1; //lineIndex is set, columnIndex to the next bip to send.
while (morseTable[lineIndex][columnIndex] != 0) { // while there are bips to send,
if (morseTable[lineIndex][columnIndex] < 3) { //if a dot or a dash
if (morseTable[lineIndex][columnIndex] == 1) { //SET LEN FOR A DASH OR A DOT
len = dotLen; //Serial.println(" DOT");
}
else {
len = dashLen; //Serial.println(" DASH");
}
digitalWrite(morsePin, HIGH);
timer.start();
state = 1;
case 1:
while (timer.counting(len)) return true; //send a len bip
digitalWrite(morsePin, LOW);
timer.start();
state = 2; //Serial.println("...");
case 2:
while (timer.counting(dotLen)) return true; //send a dotLen silence after a bip
}//6
else {
digitalWrite(morsePin, LOW);
timer.start();
state = 3; //Serial.println("SPACE");
case 3:
while (timer.counting(pauseLen)) return true; //send a pauseLen silence
}
columnIndex++; // next morse symbol
} //while symbol to send
} //all symbols have been sent
timer.start();
state = 4; //Serial.println("Pause");
case 4:
while (timer.counting(pauseLen)) return true; //make a pause between characters
msgIndex++;
//Serial.println("next char");
}
state = 0;
// msg[0] = 0;
msgIndex = 0;
columnIndex = 0;
}
}
return false;
}
};
//----------------------------------------
morseSender Tom; //Create a morseSender called Tom.
morseSender Jack; //Create a morseSender called Jack.
//------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Tom.begin(3, 80, 240, 400);
Jack.begin(2, 60, 180, 300);
strcpy(Jack.msg, "sos abc ");
}
//------------------------------------------------------------------------------------
void loop() {
Tom.sendingMorse();
Jack.sendingMorse();
}
//------------------------------------------------------------------------------------
[/code]