 	B10000011  /* Z is --.. */
 };
 
-void dot() {
+static void dot() {
 	digitalWrite(pin, HIGH);
 	delay(dotLength_ms);
 	digitalWrite(pin, LOW);
 }
 
-void dash() {
+static void dash() {
 	digitalWrite(pin, HIGH);
 	delay(dashLength_ms);
 	digitalWrite(pin, LOW);
+}
+
+size_t messageIdx = 0;
+static void advanceChar() {
+	messageIdx++;
+	if (messageIdx >= strlen(message)) {
+		delay(wordSpaceLength_ms);  /* add implicit space before looping */
+		messageIdx = 0;
+	}
 }
 
 void setup() {

 *
 * Blink a given message onto a pin, presumable holding an LED in series with a resistor.
 */
const unsigned int pin = 8;
const char message[] = "NICK HEARTS MARIE STOP";

/*
 * Morse code
 * NB: delay() takes unsigned long
 */
const unsigned long dotLength_ms = 100;  /* this is the only configurable knob; 50 ms is standard */
const unsigned long dashLength_ms = 3 * dotLength_ms;
const unsigned long blinkSpaceLength_ms = dotLength_ms;
const unsigned long charSpaceLength_ms = 3 * dotLength_ms;
const unsigned long wordSpaceLength_ms = 7 * dotLength_ms;

/* The 3 high bits are length; 5 low bits are dots (0) or dashes (1).
   The LSB is the first symbol, so you read them right to left. */
#define NUM_MORSE_SYMBOLS 36  /* capital letters plus numerals */
const byte morseEncoding[] = {
	B10111111,  /* 0 is ----- */
	B10111110,  /* 1 is .---- */
	B10111100,  /* 2 is ..--- */

	pinMode(pin, OUTPUT);
}

/* proceed one character per cycle */
void loop() {
	byte encodedChar;
	byte charNumSymbols;

	/* read next char */
	char messageChar = message[messageIdx];

	/* handle special cases and validate input */
	if (messageChar == ' ') {  /* space */
		delay(wordSpaceLength_ms - charSpaceLength_ms - blinkSpaceLength_ms);
		advanceChar();
		return;
	} else if (messageChar < '0' ||
		       (messageChar > '9' && messageChar < 'A') ||
		       messageChar > 'Z') {  /* Invalid char! Skip. */
		advanceChar();
		return;
	}

	/* unpack current character encoding */
	encodedChar = morseEncoding[messageChar - '0'];
	charNumSymbols = encodedChar >> 5;  /* top three bits */

	/* iterate over symbols in char frorm right to left */
	while (charNumSymbols--) {
		if (encodedChar & 1) dash();
		else dot();
		delay(blinkSpaceLength_ms);
		encodedChar >>= 1;
	}

	/* terminate char */
