Ideas on how to animate LEDs

Hi impers,


I have the ability to turn on and off my LEDs and I can blink them with waitfor timers as per the example code. But does anyone know how to animate in an event driven world? Let’s say I wanted to output morse code by passing in a string to a function or input class. How would I got about changing the LEDs in a sequence.

The only thing that comes to mind is have a global queue of events and pop them off the queue in sequence. So to do the letter A in morse you would queue up:
- light on, wait 50ms
- light off, wait 50ms
- light on, wait 150ms
- light off, wait 150ms

Then you would start a process to empty the queue.

Is this how its done usually? Does anyone have any reference on the web to techniques?

I’m no expert with this, and I’m sure Rob or Hugo will chime in with something better, but I would use imp.sleep() to achieve this.


Spoiler Alert: Full working code
Here’s how I would tackle it; this code assumes you have a Hannah; if not - adjust to blink your LED.

Morse Code - Hannah

Edit 1: updated code should work now. It’ll make all letters lowercase. Of course, you could do more checking and throw away bad characters, but this should get you started.

Edit 2: Updated the code again. Forgot the ever crucial LED Enable portion of the code. I was wondering why it was intermittent. Also, found a problem in the alpha-numeric array, the zero and nine were swapped. I think that should do it!

It appears to function properly, but I’m not that familiar with morse code at all, so, you tell me if it’s right. :slight_smile: But the concept should work.

Thanks for that. I managed to get mine working using a queue (int he form of an array) of “turn on/off, wait for x” items which works a treat. It has the advantage that there is no hard loop anywhere and can be interrupted or extended at any stage. for/while loops around sleeps always concern me in an event driven environment.


I can’t post it publicly now but I should be able to soon.

Quick question … I did it using a simple digital output port (write(0) and write(1)). Whats the advantage of using i2c89 analogue output?

I am using the onboard LED on the Hannah.

Yep, that will make a difference :slight_smile: