SOS code by Mr S. Monk Page 71 sketch 5 - 02

Hello,
I have found this code interesting because when I copied it from Mr S. Monks book & observed it work I was sure that it was not what I have experiences of what an SOS code ought to be.

So I slowed down the process so that I could watch the inbuilt led flash more easily, (example one below) I also deleted one of the 200 commands as written by Mr S. Monk.)

''{200, 200, 200, 500, 500, 500, 200, 200, 200}; ''

This line of command makes the Uno that I am using flash 4 times quickly, & not the allocated command of three quick flashes that is meant to represent S at the start of the code.
Then one sees three longer flashes to represent the O.
Finally, one experiences a different rate of flashes for the supposedly S at the end of the code that is meant to represent SOS!

Has any one observed similar results whilst experimenting with this code from Mr S. Monk?

Thank you
Dafyddclaud.

–---------------------------------------------------------------------------------------

// My modified code to prove a point.

int ledPin = 13;

int durations [] = {400, 400, 1000, 1000, 1000, 400, 400, 400,};

void setup ()
{
pinMode (ledPin, OUTPUT);
}

void loop ()
{
for (int i = 0; i < 9; i++)
{
flash (durations*);*

  • }*
  • delay (2000);*
    }
    void flash(int delayPeriod)
    {
  • digitalWrite(ledPin, HIGH);*
  • delay(delayPeriod);*
  • digitalWrite(ledPin, LOW);*
  • delay(delayPeriod);*
    }
    –----------------------------------------------------------------
    // S.Monk's sketch 5 – 02 from his web site & book.
    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*);_
    _
    }_
    _
    delay(1000);_
    _
    }_
    void flash(int duration)
    _
    {_
    _
    digitalWrite(ledPin, HIGH);_
    _
    delay(duration);_
    _
    digitalWrite(ledPin, LOW);_
    _
    delay(duration);_
    _
    }*_

You really need to edit that post, and put in the code tags you forgot.

// My modified code to prove a point.

All that code proves is that you can't count to nine.

Hi

Neither of those programs will flash correctly.

Firstly, when calling the flash() function, you need to access the array element:

for (int i = 0; i < 9; i++)
  {
    flash (durations[i]);
  }

Then, in your modified sketch, you need 9 durations, not 8:

int durations [] = {400, 400, 400, 1000, 1000, 1000, 400, 400, 400};

But there is also a problem with the flash() function in both programs. It turns the LED on for a given period, then turns it off for the same period. Morse code does not work like that. For the SOS prosign, the pattern should be:

@_@_@_@@@_@@@_@@@_@_@_@_____

where @ indicates LED on and _ is LED off.

The gaps between the dashes is the same as the gaps between the dots.

Regards

Ray

I just hate magic numbers in source code, so why not write the for loops and other statements that need the size of an array as:

#define ARRAYELEMENTS(x) (sizeof(x) / sizeof(x[0]))

// code to the first for loop...

   for (int i = 0; i < ARRAYELEMENTS(durations); i++) 
   {
// The rest of the code...

The advantage is that, if you change the size of an array, your for loops (and other code) that use the array size change automatically.