Arduino Morse

For my first Arduino project (and my first project of any sort on Github) I chose something that could manipulate Arduino’s on-board LED — as per the “Blink” sample project, since it would require no additional hardware — but what can you do with a single light? Morse code, naturally.

Arduinos are generally programmed in C, and I’m a C# developer mainly, so this would be a fun evenings and weekends project. I wanted to define a string of text and have the LED blink that message out in Morse. The message would be hard-coded in the program itself, though if the input part of it were to ever become more dynamic (like from reading a Tweet, or something) then the rest should be mostly reusable.

The first version of the program didn’t take very long to write, but it wasn’t very elegant. I wanted to separate some of its various concerns into separate functions, so I tried again. Here I ran into some peculiarities with how C handles strings that was taking a lot of time to figure out. Other things came up and I put the project aside for a while.

I picked it up again finally a couple of weeks ago and finished it off. The string issues I had that weren’t immediately obvious included:

  • You can use char* to declare a string variable and initialize it later, but once initialized it’s read-only. You can’t treat it like a regular array and alter individual chars.
  • If you declare a char* with the intent to initialize it in another function, you need to pass it as a char**.
  • The aforementioned char pointer issues became moot when I found what I really wanted was to declare a string variable in one function, initialize it in another, and also manipulate its char elements. In C# you don’t have to think very hard to do this, you can do most anything you want to a string and it “just works.” In my case I found I had to use char arrays, and to treat these variables as buffers.

I’d seen this pattern often enough before, of declaring a buffer of some length and passing it into a function by reference, usually when doing some sort of I/O to/from a socket or file. I don’t know if that was the only option I had to work with, but in the end it did what I wanted.

arduino morse blink

It’s the orange light

For what it does, which is next to nothing, the program might be a bit overengineered. The main point of the project though was to learn something new and have fun with it. A next step along those lines might be to make it into a Morse library with some abstractions around possible input and output options, just to learn how C libraries work. I don’t know. I know other Morse programs for Arduino exist and I’ve purposefully ignored them so I can play this out for what its worth as a learning exercise. If anyone wants to work with this code and send me suggestions/corrections, definitely please do that.

This entry was posted in Programming, Uncategorized. Bookmark the permalink.

One Response to Arduino Morse

  1. Pingback: Arduino Morse – pt. 2 – only Morseo | Working Title

Leave a Reply