Delay in imp

Hello I’m trying to do a delay to generate code of an IR control in a function with imp.sleep() but I need that the delay are the most exactly possible and when I use imp.sleep(.009) for example, the real result is 1.5 ms or imp.sleep(.000560) the result is .000984 seg, and the control doesn´t work. So my question is if exist some way to improve the precision or if exist another way to do a delay with more exactly this problem

thanks!! :slight_smile:

Can you show us (or describe) the wave form you’re trying to create?

You might be able to this with PWM or SPI?

EDIT: I’m actually writing some code that might be applicable, and if I know what you’re trying to do I can probably give a better example :slight_smile:

Sounds like something that may be possible with some clever use of the FixedFrequencyDAC Class?

I’m trying to reproduce the code of a IR control in my case I use a NEC protocol this is my code

function Start(){
hardware.pin9.write(0.25);
imp.sleep(0.009);
hardware.pin9.write(0.0);
imp.sleep(0.0045);
}

function Uno(){
hardware.pin9.write(0.25);
imp.sleep(0.000560);
hardware.pin9.write(0.0);
imp.sleep(0.00169);
}

function Cero(){
hardware.pin9.write(0.25);
imp.sleep(0.000560);
hardware.pin9.write(0.0);
imp.sleep(0.000560);
}

but the times in imp.sleep() are higher and causes that the control doesn’t work
thank you for your previous answers

As squirrel is an interpreted language, this isn’t going to work; you can’t hold timings that tight. I’d suggest you build your waveform as a bitstream, and use SPI to clock it out of a MOSI pin at a suitable rate; you can then hold much better timings.

Either that, or use the fixedfrequencydac to do a similar job.