Bizarre blinking


Okay so here’s the deal

I have a program that does a couple different things
-It can ask for a frequency value from the user and subsequently blink an LED on and off to that frequency
-It can allow the duty cycle to spend whatever percentage of time on or off. (i.e. If you wanted 75% of the period on and 25% off, it can do that.)
-It can loop that frequency forever OR you can set a count in which case the LED will only blink the set amount of times that you tell it to. (i.e. If you have a count of 10 with a frequency of 1 Hz, The LED will blink 10 times at 1 complete blink (on/off) a second)
-Finally, you can simulate noise by allowing one of the counts to be a higher frequency than the others. (i.e. If I have an oscillation with a 40Hz frequency that only lasts for 10 blinks, I want the duration of the 10th blink (on) to have a frequency of 500Hz)

All of these features work except for the last one that only kinda works.

Being that I was going to be dealing with some pretty high frequencies, I decided to hook up my circuit (which is the basic electric imp startup circuit with LED) to a Tektronix TDS 640A digitizing oscilloscope.

This way, I was able to test and make sure that all of the features above worked

However, I noticed something about the test with the added noise.

Suppose the frequency is 8 Hz
The count is 10
The location of the noise is on count 10
And the frequency of the noise is 50Hz

This should blink 9 times with a frequency of 8Hz, and on the 10th blink have a frequency of 50Hz.
(Just to be clear, the 10th blink is the same duration as the other 9 blinks. The period here would be 1/8, So each blink spends 1/16 seconds on and 1/16 seconds off. On the 10th blink it would 1/16 seconds having a frequency of 50Hz.)


The FIRST blink is several milliseconds SMALLER than it’s supposed to be
The time off between the 9th blink and the 10th blink is SMALLER than it’s supposed to be
AND the 10th blink with the noise is LONGER than it’s supposed to be
Everything else is fine

i.e. Blink 1 (on) < 1/16
i.e. Blink 9 (off) < 1/16
i.e. Blink 10 (on) > 1/16
i.e. Blink Everything else = 1/16

I’ve checked my code over and over again and the calculations should be giving me 1/16! But the scope reads something different.

Here’s what I’m sure it ISN’T
-Pretty sure it’s not my scope. It can read a lot faster than this and has been tested against other devices and works fine
-Pretty sure it’s not any limitations within my code
(I’m using imp.sleep which has a resolution of about a microsecond, so we should be good there)

Here’s what my code looks like: (ignore slideron, that’s the 50% on 50% off thing, that won’t change)

        hardware.pin8.configure(PWM_OUT, period, slideron);
        imp.sleep((period * (countlocation - 1.0) * 1.0));
        hardware.pin8.configure(PWM_OUT, periodtwo, slideron);
        imp.sleep((period * 0.5) - periodtwo); 
        hardware.pin8.configure(DIGITAL_OUT, 0);
        imp.sleep(period * 0.5);

These differences are small, only a couple milliseconds. But I need to test with big frequencies so those milliseconds can make a difference.

I apologize if I’ve explained this poorly
It’s such a strange error in a process that otherwise works, so I figured I should try and explain everything I can
But please feel free to respond with any questions and I’ll do my best to explain it better

Thank You for reading my obnoxiously long post! Sorry, I can be unnecessarily verbose :stuck_out_tongue:
And Thanks so much for your help!
Greatly Appreciated!

Oh I forgot one thing.

This only happens on the runs with the added noise

Just having the LED blink with a frequency for a certain number of blinks (or any other feature) works flawlessly.

Sorry, there’s a typo in there.

In my code it should NOT be
imp.sleep((period * 0.5) - periodtwo);

It should just be imp.sleep(perod * 0.5);

That was from a test I was trying