Got it working. The problems were the delayMicroseconds
input being too large (see comments) and the frequency mismatches.
I set up a script to time the code segments of interest and that allowed me to select the best wait duration. It looks like this:
while (i < 256) {
/* your code to time here */
i++;
}
i = 0;
unsigned long time = 10000000 + micros();
Serial.println(time);
Make sure to time the empty loop and subtract that from your readings w/ code. Take the difference between time
read outs, subtract the printing statement overhead (which is dependent on how many digits it is printing, hence the 10000000 + micros()
to keep # of digits constant) and divide by 256.
I measured 4 different combinations to find the closest frequency match:
Command Type: | Wait duration (micro seconds): | Frequency (Hz): | % difference from 38 kHz |
---|---|---|---|
Port Manipulation | 14 | 36990 | -52.37 % |
Port Manipulation | 13 | 39960 | 5.2 % |
digitalWrite | 11 | 35980 | -25.73 % |
digitalWrite | 10 | 38790 | 2.1 % |
The final code ended up looking a lot like the one from this link; counting off cycles of the on-off 38 kHz pulse.