The Arduino Code

Since I was designing and building the first digitally-controlled heliograph, there was no code to control it, even if I succeeded in building it.

Fortunately, there are many versions of existing code (including Arduino code) to convert text to Morse Code. But I could not find any that would directly control a solenoid or a relay. I eventually selected one that turned the Arduino LED on and off. But I would need to modify that code.

I found one tiny example of Adruino code that could energize and de-energize a relay, but it had nothing to do with Morse Code.

I was able to combine the two code fragments to have the Arduino control a relay (which then controlled the solenoid, which tilted the heliograph mirror, to produce coded pulses of reflected sunlight).

The message to be sent is at the bottom of the code. The program converts one character of the message at a time into Morse code, using the table. The program has a defined length for dots, dashes, and pauses, which are sent out through Arduino terminal 2 to the relay. Anything on the same line following two slashes // is a comment, and is ignored when the program runs.

Once the code is uploaded to the Arduino, it will remain there until it is replaced, even when power is disconnected from the Arduino. That was convenient for science fairs, since I did not need to bring a computer with me, in addition to the trifold and the heliograph.

This is the Arduino code:

//

// DIGITAL INTERISLAND SOLAR COMMUNICATION

//Ver 4e

#define RELAY_ON 0

#define RELAY_OFF 1

#define RELAY_1 2 //digital terminal 2

void setup()

{

// Set pin for output.

pinMode(RELAY_1, OUTPUT);

// start with the relay off

digitalWrite(RELAY_1, RELAY_OFF);

}

struct t_mtab { char c, pat; } ;

struct t_mtab morsetab[] = {

{'.', 106},//.-.-.-

{',', 115},//--..--

{'?', 76},//..--..

{'/', 41},//-..-.

{'A', 6},//.-

{'B', 17},//-...

{'C', 21},//-.-.

{'D', 9},//-,,

{'E', 2},//.

{'F', 20},//..-.

{'G', 11},//--.

{'H', 16},//....

{'I', 4},//..

{'J', 30},//.---

{'K', 13},//-.-

{'L', 18},//.-..

{'M', 7},//--

{'N', 5},//-.

{'O', 15},//---

{'P', 22},//.--.

{'Q', 27},//--.-

{'R', 10},//.-.

{'S', 8},//...

{'T', 3},//-

{'U', 12},//..-

{'V', 24},//...-

{'W', 14},//.--

{'X', 25},//-..-

{'Y', 29},//-.--

{'Z', 19},//--..

{'1', 62},//.----

{'2', 60},//..---

{'3', 56},//...--

{'4', 48},//....-

{'5', 32},//.....

{'6', 33},//-....

{'7', 35},//--...

{'8', 39},//---..

{'9', 47},//----.

{'0', 63}//-----

} ;

#define N_MORSE (sizeof(morsetab)/sizeof(morsetab[0]))

#define SPEED (4) //orig 12

#define DOTLEN (1200/SPEED)

#define DASHLEN (3*(1200/SPEED))

void

dash()

{

digitalWrite(RELAY_1, HIGH) ;

delay(DASHLEN);

digitalWrite(RELAY_1, LOW) ;

delay(DOTLEN) ;

}

void

dit()

{

digitalWrite(RELAY_1, HIGH) ;

delay(DOTLEN);

digitalWrite(RELAY_1, LOW) ;

delay(DOTLEN);

}

void

send(char c)

{

int i ;

if (c == ' ') {

Serial.print(c) ;

delay(7*DOTLEN) ;

return ;

}

for (i=0; i<N_MORSE; i++) {

if (morsetab[i].c == c) {

unsigned char p = morsetab[i].pat ;

Serial.print(morsetab[i].c) ;

while (p != 1) {

if (p & 1)

dash() ;

else

dit() ;

p = p / 2 ;

}

delay(2*DOTLEN) ;

return ;

}

}

// if unknown character, send ?

Serial.print("?") ;

}

void

sendmsg(char *str)

{

while (*str)

send(*str++) ;

Serial.println("");

}

void loop() {

//sendmsg("UA MAU KE EA O KA AINA I KA PONO . THE LIFE OF THE LAND IS PERPETUATED IN RIGHTEOUSNESS. ") ;

sendmsg("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG ") ;

//sendmsg("WE ARE THE NAVIGATORS AND WE LEAD THE WAY ") ;

//sendmsg("OR TYPE ANY MESSAGE HERE AND UNCOMMENT IT AND COMMENT OUT THE OTHER ONE") ;

delay(3000) ;

}