I'm new - I wrote a Morse code blink sketch

I am programming on a Tiny85 board which doesn't really have good serial output capabilities.

In the case of a simple configuration error, I thought of blinking a brief description of the error state in Morse code instead of a Serial.println(). So, here is my Morse code sketch. I hope somebody finds it useful.

Bonus: explain to me how to make this into a library rather than just a standalone sketch.

Arduino Morse Code Blink
Copyright © 2021 Douglas Held

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

/*
  Morse Blinking library for an LED
*/

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode( LED_BUILTIN, OUTPUT );
}

// General speed of coding in ms. Length of one 'dit'.
const int BEAT = 175;

void on( int ms ){
  digitalWrite( LED_BUILTIN, HIGH );   // turn the LED on (HIGH is the voltage level)
  delay( ms ); 
}
void off( int ms ){
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay( ms );  
}
void dit(){
  on( BEAT );
  off( BEAT );
}
void dah(){
  on( BEAT * 3 );
  off( BEAT );
}

void blink( char c ){
  switch (c) {
    case 'a': dit(); dah(); break;
    case 'b': dah(); dit(); dit(); dit(); break;
    case 'c': dah(); dit(); dah(); dit();  break;
    case 'd': dah(); dit(); dit(); break;
    case 'e': dit(); break;
    case 'f': dit(); dit(); dah(); dit(); break;
    case 'g': dah(); dah(); dit(); break;
    case 'h': dit(); dit(); dit(); dit(); break;
    case 'i': dit(); dit(); break;
    case 'j': dit(); dah(); dah(); dah(); break;
    case 'k': dah(); dit(); dah(); break;
    case 'l': dit(); dah(); dit(); dit(); break;
    case 'm': dah(); dah(); break;
    case 'n': dah(); dit(); break;
    case 'o': dah(); dah(); dah(); break;  
    case 'p': dit(); dah(); dah(); dit(); break;  
    case 'q': dah(); dah(); dit(); dah(); break;  
    case 'r': dit(); dah(); dit(); break;
    case 's': dit(); dit(); dit(); break;
    case 't': dah(); break;
    case 'u': dit(); dit(); dah(); break;
    case 'v': dit(); dit(); dit(); dah(); break;
    case 'w': dit(); dah(); dah(); break;
    case 'x': dah(); dit(); dit(); dah(); break;
    case 'y': dah(); dit(); dah(); dah(); break;
    case 'z': dah(); dah(); dit(); dit(); break;

    case '1' :  dit(); dah(); dah(); dah(); dah(); break;
    case '2' :  dit(); dit(); dah(); dah(); dah(); break;
    case '3' :  dit(); dit(); dit(); dah(); dah(); break;
    case '4' :  dit(); dit(); dit(); dit(); dah(); break;
    case '5' :  dit(); dit(); dit(); dit(); dit(); break;
    case '6' :  dah(); dit(); dit(); dit(); dit(); break;
    case '7' :  dah(); dah(); dit(); dit(); dit(); break;
    case '8' :  dah(); dah(); dah(); dit(); dit(); break;
    case '9' :  dah(); dah(); dah(); dah(); dit(); break;
    case '0' :  dah(); dah(); dah(); dah(); dah(); break;

    case ' ': off( BEAT * 4 ); break; // timing between words should total 700ms

  }
  off( BEAT * 3 ); // timing to separate a character
}

void loop() {
  blink( 's' );
  blink( 'o' );
  blink( 's' );
  blink( ' ' );
  blink( 'a' );
  blink( 'b' );
  blink( 'c' );
  blink( 'd' );
  blink( 'e' );
  blink( ' ' );
}
1 Like

Funny enough, the official Arduino guide on making a simple library follows the theme of morse code too: https://www.arduino.cc/en/Hacking/libraryTutorial. It explains how the .h and .cpp files are linked and some basic object oriented programming. Hopefully that will give you some pointers?

1 Like

Apart from the educational experience of turning that into a library, I really don't see the value of the sketch as a debug aid.

You need to hard code every possible state into a message of some sort, like the "sos abcde" that you have there.

A simpler way would surely be to decide on the states that you want to notify the user about, and have a bunch of leds that indicate each state. With (say) 3 leds you could have 8 states (000-111).

Thank you. I was thinking of developer debugging (like, the driver didn't load), not end user exception management.

It will indeed! THank you very much.

That's what I meant too- user as in the user of the debugging tool.

I just can't think of a case where having the message blinked in Morse code would trump the use of a few leds to indicate a state.

But all of that's off topic, which was about turning your code into a library.

You are using delay, your library will be worthless for using in conjunction with anything else. Might as well keep it as a sketch.

1 Like

MyOpinion: Libraries are way overused; functions put into ArduinoIDE "tabs" provide better encapsulation when it comes to backing up a complete project. Arduino libraries are often in a state of flux and the changes are not always beneficial to older sketches.

Added: example of merging libraries into sketch folder

I thought maybe you were referring to multiple threads. Now I've seen the BlinkWithoutDelay sketch and I think I understand what you mean.

You are correct, the blink() method I created blocks until finished.

It did take me a little while to understand what you are suggesting. What I found is completely unexpected.

  1. Open a sketch, e.g. A.ino.
  2. Select "New Tab" - user is prompted for a new filename, e.g. "foo"
  3. a new foo.ino file is created in the sketch directory
  4. any functions defined in foo.ino are available automatically in A.ino, without importing the file.

This is a truly weird programming environment. But this feature does make sense; what you're saying is that you copy functions into your sketch this way, without the function definitions cluttering up your main sketch file. Yes, I can understand the appeal in that. Thank you for the advice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.