0

I am trying to write an Arduino Library that easily outputs Morse code on a digital pin. I followed a tutorial but then tried to add a bit myself. When I compile and upload my code to the arduino, it compiles fine with no errors but when it gets run on the arduino, it properly outputs the first letter and then stops.

here is my .ino file:

#include <Morse.h>

Morse morse(13);

void setup()
{
  morse.writeStr("Wow");
  delay(3000);
}

void loop()
{

}

and here is my library header file (.h):

/*
  Morse.h - Library for flashing Morse code.
  Created by Adrian Wowk, December 11, 2019.
  Released into the public domain.
*/
#ifndef Morse_h
#define Morse_h

#include "Arduino.h"

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
    void writeStr(char message[6]);
  private:
    int _pin;

};

#endif

and here is my library C++ file:

/*
  Morse.cpp - Library for flashing Morse code.
  Created by Adrian Wowk, December 11, 2019.
  Released into the public domain.
*/

#include "Arduino.h"
#include "Morse.h"

Morse::Morse(int pin)
{
    pinMode(pin, OUTPUT);
    _pin = pin;
}

void Morse::dot()
{
    digitalWrite(_pin, HIGH);
    delay(250);
    digitalWrite(_pin, LOW);
    delay(250);
}

void Morse::dash()
{
    digitalWrite(_pin, HIGH);
    delay(1000);
    digitalWrite(_pin, LOW);
    delay(250);
}

void Morse::writeStr(char message[6])
{
    for (byte i = 0; i < sizeof(message) - 1; i++)
    {
        switch (message[i])
        {
        case 1:
            dot();
            dash();
            dash();
            dash();
            dash();
            break;
        case 2:
            dot();
            dot();
            dash();
            dash();
            dash();
            break;
        case 3:
            dot();
            dot();
            dot();
            dash();
            dash();
            break;
        case 4:
            dot();
            dot();
            dot();
            dot();
            dash();
            break;
        case 5:
            dot();
            dot();
            dot();
            dot();
            dot();
            break;
        case 6:
            dash();
            dot();
            dot();
            dot();
            dot();
            break;
        case 7:
            dash();
            dash();
            dot();
            dot();
            dot();
            break;
        case 8:
            dash();
            dash();
            dash();
            dot();
            dot();
            break;
        case 9:
            dash();
            dash();
            dash();
            dash();
            dot();
            break;
        case 0:
            dash();
            dash();
            dash();
            dash();
            dash();
            break;
        case 'a':
            dot();
            dash();
            break;
        case 'A':
            dot();
            dash();
            break;
        case 'b':
            dash();
            dot();
            dot();
            dot();
            break;
        case 'B':
            dash();
            dot();
            dot();
            dot();
            break;
        case 'c':
            dash();
            dot();
            dash();
            dot();
            break;
        case 'C':
            dash();
            dot();
            dash();
            dot();
            break;
        case 'd':
            dash();
            dot();
            dot();
            break;
        case 'D':
            dash();
            dot();
            dot();
            break;
        case 'e':
            dot();
            break;
        case 'E':
            dot();
            break;
        case 'f':
            dot();
            dot();
            dash();
            dot();
            break;
        case 'F':
            dot();
            dot();
            dash();
            dot();
            break;
        case 'g':
            dash();
            dash();
            dot();
            break;
        case 'G':
            dash();
            dash();
            dot();
            break;
        case 'h':
            dot();
            dot();
            dot();
            dot();
            break;
        case 'H':
            dot();
            dot();
            dot();
            dot();
            break;
        case 'i':
            dot();
            dot();
            break;
        case 'I':
            dot();
            dot();
            break;
        case 'j':
            dot();
            dash();
            dash();
            dash();
            break;
        case 'J':
            dot();
            dash();
            dash();
            dash();
            break;
        case 'k':
            dash();
            dot();
            dash();
            break;
        case 'K':
            dash();
            dot();
            dash();
            break;
        case 'l':
            dot();
            dash();
            dot();
            dot();
            break;
        case 'L':
            dot();
            dash();
            dot();
            dot();
            break;
        case 'm':
            dash();
            dash();
            break;
        case 'M':
            dash();
            dash();
            break;
        case 'n':
            dash();
            dot();
            break;
        case 'N':
            dash();
            dot();
            break;
        case 'o':
            dash();
            dash();
            dash();
            break;
        case 'O':
            dash();
            dash();
            dash();
            break;
        case 'p':
            dot();
            dash();
            dash();
            dot();
            break;
        case 'P':
            dot();
            dash();
            dash();
            dot();
            break;
        case 'q':
            dash();
            dash();
            dot();
            dash();
            break;
        case 'Q':
            dash();
            dash();
            dot();
            dash();
            break;
        case 'r':
            dot();
            dash();
            dot();
            break;
        case 'R':
            dot();
            dash();
            dot();
            break;
        case 's':
            dot();
            dot();
            dot();
            break;
        case 'S':
            dot();
            dot();
            dot();
            break;
        case 't':
            dash();
            break;
        case 'T':
            dash();
            break;
        case 'u':
            dot();
            dot();
            dash();
            break;
        case 'U':
            dot();
            dot();
            dash();
            break;
        case 'v':
            dot();
            dot();
            dot();
            dash();
            break;
        case 'V':
            dot();
            dot();
            dot();
            dash();
            break;
        case 'w':
            dot();
            dash();
            dash();
            break;
        case 'W':
            dot();
            dash();
            dash();
            break;
        case 'x':
            dash();
            dot();
            dot();
            dash();
            break;
        case 'X':
            dash();
            dot();
            dot();
            dash();
            break;
        case 'y':
            dash();
            dot();
            dash();
            dash();
            break;
        case 'Y':
            dash();
            dot();
            dash();
            dash();
            break;
        case 'z':
            dash();
            dash();
            dot();
            dot();
            break;
        case 'Z':
            dash();
            dash();
            dot();
            dot();
            break;
        case ' ':
            delay(250);
            break;

        default:
            delay(1000);
            break;
        }
    }
}
Wowkster
  • 197
  • 3
  • 9

1 Answers1

0

In your Morse::writeStr function there is a problem. Doing sizeof(message) should throw the following error:

warning: 'sizeof' on array function parameter 'message' will return size of 'char*'

You cannot find the actual length of the message by looking at the size of message in the way you do. The variable message in your writeStr function is passed as a pointer to the char array, the size of the pointer is equal to 1.

Next to this you're iterating from i = 0 to i < sizeof(message) - 1. Since the size of message IS 1, you're iterating from i = 0 to i < 0 and the for loop should not even execute once.

Next to the pointer to the char array, you should also pass the length of the array and use that in in your for loop. For example like so:

void Morse::writeStr(char *message, int size_)
{
    for (char i = 0; i < size_; i++)
    {
        ...
    }
}

Remember to also change this function declaration in your header file. Then, in your loop or setup function you can send a char array to the function like this:

Morse morse(13);
char msg[] = "test";
morse.writeStr(msg, 4);
Anteino
  • 1,044
  • 7
  • 28