@@ -22,7 +22,7 @@ void IRsend::sendRaw (unsigned int buf[], unsigned char len, unsigned char hz
2222void IRsend::mark (int time)
2323{
2424TIMER_ENABLE_PWM; // Enable pin 3 PWM output
25- if (time > 0 ) custom_delay_ms (time);
25+ if (time > 0 ) custom_delay_usec (time);
2626}
2727
2828// +=============================================================================
@@ -33,7 +33,7 @@ void IRsend::mark (int time)
3333void IRsend::space (int time)
3434{
3535TIMER_DISABLE_PWM; // Disable pin 3 PWM output
36- if (time > 0 ) IRsend::custom_delay_ms (time);
36+ if (time > 0 ) IRsend::custom_delay_usec (time);
3737}
3838
3939
@@ -71,14 +71,16 @@ void IRsend::enableIROut (int khz)
7171// +=============================================================================
7272// Custom delay function that circumvents Arduino's delayMicroseconds limit
7373
74- void IRsend::custom_delay_ms (unsigned int time) {
75- if (time)
76- {
77- if (time > 16000 )
78- {
79- delayMicroseconds (time % 1000 );
80- delay (time / 1000 );
81- }
82- else delayMicroseconds (time);
83- }
74+ void IRsend::custom_delay_usec (unsigned long uSecs) {
75+ if (uSecs > 4 ) {
76+ unsigned long start = micros ();
77+ unsigned long endMicros = start + uSecs - 4 ;
78+ if (endMicros < start) { // Check if overflow
79+ while ( micros () > start ) {} // wait until overflow
80+ }
81+ while ( micros () < endMicros ) {} // normal wait
82+ } else {
83+ __asm__ (" nop\n\t " ); // must have or compiler optimizes out
84+ }
8485}
86+
0 commit comments