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!