I have been re-writing Monk's examples to learn Arduino syntax. When I uploaded this one Sketch_05_02 it did not give a smooth sos in morse. So I just added a 200msec pause at beginning of the execution of the flash (int duration) function. It works, but I can't figure out why. Any help in understaning the flow of this sketch is appreciated.
Here is mine. All I added was a delay(duration) and I spaced the delay between the loop a bit longer to make it easier to evaluate my result.
//this sketch uses an array to flash sos
//Simon sketch has problems - timing is off - it needs a 200msec delay at beginning of last block
//I found by trial and error but I don't know why
int ledPin = 13;
int durations[] = {200, 200, 200, 500, 500, 500, 200, 200, 200};
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
for (int i = 0; i < 9; i++)
{
flash(durations[i]); //create function to point to positions 0-9 in the durations array for data
}
delay(2000);
}
void flash(int duration) //call the flash function and give the data in the durations array a local variable named duration
{
delay(duration);
digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(duration);
}
According to the wikipedia article on Morse Code:
A 'dash' should be three times the length of a 'dot'.
The time between dots or dashes in a letter should be that same as the time for a dot.
The time between letters should be three dot times.
I suspect that your changes got closer to the standard but it is still off. Try this version:
int ledPin = 13;
const int DOT = 150;
const int DASH = DOT * 3;
int durations[] = {DOT, DOT, DOT, DASH, DASH, DASH, DOT, DOT, DOT};
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 9; i++) {
flash(durations[i]);
if (i == 2 || i == 5)
delay(DASH - DOT); // inter-letter additional time
}
delay(1000);
}
void flash(int duration) {
digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(DOT);
}
Came across John's post just now (over three years later) when checking other apparent errors in Simon Monk's code from 'Programming Arduino'.
I think the 'inter-letter' delay should be as in my edit below. (I've also reflected the official inter-word standard, although it's almost identical to John's 1 s.)
// John Wasser's corrected version of Simon Monk's 5-03
// https://forum.arduino.cc/index.php?topic=444928.0
// My further edits
int ledPin = 13;
const int DOT = 150;
const int DASH = DOT * 3;
int durations[] = {DOT, DOT, DOT, DASH, DASH, DASH, DOT, DOT, DOT};
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 9; i++) {
flash(durations[i]);
if (i == 2 || i == 5)
delay(DOT); // inter-letter additional time // My edit
}
delay(DOT * 7); // inter-word delay
}
void flash(int duration) {
digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(DOT);
}
Terrypin:
I think the 'inter-letter' delay should be as in my edit below.
So you think the inter-letter spacing should be two dot times and not 3 as specified in the International Telecommunications Union standard referenced by Wikipedia?
2 Spacing and length of the signals
2.1 A dash is equal to three dots.
2.2 The space between the signals forming the same letter is equal to one dot.
2.3 The space between two letters is equal to three dots.
2.4 The space between two words is equal to seven dots.