Hello,
I am a newbie and I'd like to ask for a piece of advice.
I am trying to play a binary sequence at a sample rate of 32768 Hz.
Manual says that IntervalTimer is for precise timing control, so I tried to use it.
In the example code below the sequence {1 0 1} is repeated once a second.
This is just to simplify illustration: in real application I need to play long MLS (M-sequence).
In a perfect world I'd expect to see at output pin a signal looking like this:
I checked the real life output by connecting oscilloscpope to the ouput pin and recording waveform a few times.
What I saw is like this:
And here is zoom of the zone around the first drop:
As one can see, sometimes timing is off up to about 3 us which is 10% of my sampling period and is not acceptable.
Am I doing something wrong?
How can I get more precise timing?
Thank you!
I am a newbie and I'd like to ask for a piece of advice.
I am trying to play a binary sequence at a sample rate of 32768 Hz.
Manual says that IntervalTimer is for precise timing control, so I tried to use it.
In the example code below the sequence {1 0 1} is repeated once a second.
This is just to simplify illustration: in real application I need to play long MLS (M-sequence).
Code:
int pin = 9; // output pin
int L = 3; // length of sequence
boolean seq[3] = {1 0 1}; // sequence to be played
int Fs = 32768; // sampling rate
IntervalTimer t;
volatile int ind = 0;
void next_digit(){
if (ind<L){
digitalWrite(pin,seq[ind]);
}
else if (ind<=Fs){
digitalWrite(pin,0);
}
else{
ind=-1;
}
ind++;
}
void setup() {
pinMode(pin, OUTPUT);
t.begin(next_digit,1./Fs);
}
void loop() {
}
In a perfect world I'd expect to see at output pin a signal looking like this:
I checked the real life output by connecting oscilloscpope to the ouput pin and recording waveform a few times.
What I saw is like this:
And here is zoom of the zone around the first drop:
As one can see, sometimes timing is off up to about 3 us which is 10% of my sampling period and is not acceptable.
Am I doing something wrong?
How can I get more precise timing?
Thank you!