I’ve got another mystifying problem. Say I take this simple bit of code (note that it seems arbitrary, but my application does actually need this sort of setup) on manual threading:
ATOMIC_BLOCK() {
digitalWriteFast(D1,HIGH); //for timing
while(i<(10)){
i++;
if(i==1){
variable=true;
}
}
digitalWriteFast(D1,LOW); //for timing
}
Just counting up to 10, setting a variable when it gets to 1, and timing it. This takes 200 nanoseconds to run. I can make the loop run an arbitrary number of times with almost no change in timing. (tried while(i<(100000)){, still 200ns). Perfect. Next, I try:
ATOMIC_BLOCK() {
digitalWriteFast(D1,HIGH); //for timing
while(i<(10)){
i++;
if(i==1){
analogWrite(DAC1, 4000);
}
}
digitalWriteFast(D1,LOW); //for timing
}
Just counting up to 10, and setting a DAC value (this doesn’t actually matter, I’ve tried it with digitalwrites etc.) when it reaches 1. On an oscilloscope, it takes about 4.5 us to run. My timing estimates show that DAC write alone takes 4 us, so that’s perfectly fine.
Now, say I make it loop 100,000 times here. This should, according to my intuition, only take 4 us + 200ns, because it’s only setting the DAC value when it counts to 1.
And yet, this is not what I see. I get a time of 7.5 ms, as though it’s actually setting the DAC value 2000 times instead of once. I’ve replicated this on multiple P1s.