LED blinking project

Hey Everyone!

I’m still pretty new to the electric imp and I’m working on this very simple project where a user inputs a frequency and an LED flashes on and off to said frequency.

Currently the program can blink at any frequency, for any duty cycle, and for any specified amount of time. (All user inputs)

I wanted to see if there was a way to blink an LED a certain number of times but have one one of the “blinks” be of a different frequency.

In other words, imagine an LED blinking 5 times to a frequency of 5. A graph of the voltage/time might look something like this:

         ___________             __________             _________              __________              __________

| || || || || |_
(Obviously the blips would be equally spaced and sized)

I want the program to do something like this if I want a frequency of 500 on the 3rd blip.

         ___________             __________             |||||||||||||              __________              __________

| || |||||||||||||||| || |_

The same amount of time that the LED might normally be “on” for a third count would instead be spent with a frequency of 500.

I tried coding this in the most intuitive way possible:

hardware.pin.configure(PWM_OUT, period, 0.5); //As per the example: Pulse with period = (1/5)
imp.sleep((countlocation - 1.0) * period); //Continue up until the start of the 3rd blip
hardware.pin.configure(PWM_OUT, periodtwo, 0.5); //Pulse with period = (1/500)
imp.sleep(0.5 * period); //Continue for half a period (i.e. one "blip"
hardware.pin.configure(DIGITAL_OUT, 0); //Finish the rest of the period OFF
imp.sleep(0.5 * period): //Continue for other half of period
hardware.pin.configure(PWM_OUT, period, 0.5); //Pulse with period = (1/5)
imp.sleep((totalcount - countlocation) * period); //Continue until total counts are reached
hardware.pin.configure(DIGITAL_OUT, 0); //Stop

Using a Tektronix TDS 640A oscilloscope, I tested the program to make sure everything was happening at the correct time.

However, I got some pretty strange results. The times that the LED spend on or off during the larger frequency was always off by a few milliseconds. There is absolutely no correlation between the errors on different frequencies.

Am I going about this the right way?
Is there a better solution to this problem that allows for exact time delays as I want them?

Obviously a small amount of error is understandable, however I’ll be using a range from 1-300 Hz. So those few milliseconds make a noticeable difference.

Any help would be greatly appreciated!
Thanks!

Whoops, those text images didn’t format the way I wanted them to. Here’s what I meant

I suspect the best way to do this would be to use the ffdac to output zero or full-scale (ie using a DAC as a binary output). You’d set the ffdac to 1000Hz (so you could output 500Hz square waves) and then prepare buffers which contain each part of the waveform and queue them in your desired order.

This can then essentially execute in the background.

Worth a try?

Hi Hugo! Thanks so much for responding!

What do you mean by FFDAC?

Oh whoops. Fixed Frequency, got it. This solution looks like it might work! My biggest question is how to prepare the buffers. I’ve never really used these before.

The buffers are just binary blobs; if you configure the DAC with A_LAW_DECOMPRESS then you get to prepare them fairly easily: set a byte to 0x7F to get a logic 1 and 0xFF to get a logic 0.

eg:
local b=blob[1000]; for(local a=0;a<b.len();a+=2) { b[a] = 0x7f; b[a+1] = 0xff; }

…should give you a buffer with a square wave in it, to be clocked out at whatever frequency you want.

Awesome! This worked great! Thanks!