delayMicroseconds does not delay

Hi everyone,

I need to delay something sub-millisecond therefore I try to use delayMicroseconds but the led I'm using to check if everything is ok tells me it's wayyyyyyy too fast.

(Both codes below have been slowed 1000 times)

Here is the code :

class RADIO {
  public:
    RADIO();

    void send(char * code){
      
      int codeLength = strlen(code);
      int cursor = 0;
      int bit = 0;
      this->header();
      while (cursor <= codeLength - 1){
        bit = 7;
        while(bit >= 0){
          this->transmit(code[cursor] &  1 << bit);
          bit--;
        }
        cursor++;
      }
      this->footer();
    }

    void setPin(int pin){
      pinMode(pin, OUTPUT);
      this->pin = pin;
    }
    
  private:
    int pin;
    int pulseLength;
    int repeatTransmit;
    int repeatBit;

    void transmit(int bit){
      int i = 0;
      for(i; i < this->repeatBit; i++){
        if(bit){
          digitalWrite(this->pin, HIGH);
          delayMicroseconds((3 * this->pulseLength) / 4);
          digitalWrite(this->pin, LOW);
          delayMicroseconds(this->pulseLength / 4);
        }
        else {
          digitalWrite(this->pin, HIGH);
          delayMicroseconds((this->pulseLength) / 4);
          digitalWrite(this->pin, LOW);
          delayMicroseconds((3 * this->pulseLength / 4));
        }
      }
    }

    void header(){
      digitalWrite(this->pin, HIGH);
      delayMicroseconds(this->pulseLength * 3);
      digitalWrite(this->pin, LOW);
      delayMicroseconds(this->pulseLength);
    }

    void footer(){
      digitalWrite(this->pin, HIGH);
      delayMicroseconds(pulseLength);
      digitalWrite(this->pin, LOW);
      delayMicroseconds(pulseLength * 3);
    }
    
};

RADIO::RADIO(){
  // Microseconds
  // Slowed 1000 times for dev
  this->pulseLength = 1000 * 1000;
  this->repeatBit = 3;
}

RADIO myRadio = RADIO();

void setup() {
  Serial.begin(9600);
  myRadio.setPin(7);
  myRadio.send("204");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Here the same code but delays are working normally

class RADIO {
  public:
    RADIO();

    void send(char * code){
      
      int codeLength = strlen(code);
      int cursor = 0;
      int bit = 0;
      this->header();
      while (cursor <= codeLength - 1){
        bit = 7;
        while(bit >= 0){
          this->transmit(code[cursor] &  1 << bit);
          bit--;
        }
        cursor++;
      }
      this->footer();
    }

    void setPin(int pin){
      pinMode(pin, OUTPUT);
      this->pin = pin;
    }
    
  private:
    int pin;
    int pulseLength;
    int repeatTransmit;
    int repeatBit;

    void transmit(int bit){
      int i = 0;
      for(i; i < this->repeatBit; i++){
        if(bit){
          digitalWrite(this->pin, HIGH);
          delay((3 * this->pulseLength) / 4);
          digitalWrite(this->pin, LOW);
          delay(this->pulseLength / 4);
        }
        else {
          digitalWrite(this->pin, HIGH);
          delay((this->pulseLength) / 4);
          digitalWrite(this->pin, LOW);
          delay((3 * this->pulseLength / 4));
        }
      }
    }

    void header(){
      digitalWrite(this->pin, HIGH);
      delay(this->pulseLength * 3);
      digitalWrite(this->pin, LOW);
      delay(this->pulseLength);
    }

    void footer(){
      digitalWrite(this->pin, HIGH);
      delay(pulseLength);
      digitalWrite(this->pin, LOW);
      delay(pulseLength * 3);
    }
    
};

RADIO::RADIO(){
  // Milliseconds
  // Slowed 1000 times for dev
  this->pulseLength = 1000;
  this->repeatBit = 3;
}

RADIO myRadio = RADIO();

void setup() {
  Serial.begin(9600);
  myRadio.setPin(7);
  myRadio.send("204");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Problem is I use 3/4 and 1/4 of the pulseLength somewhere so I can't use delay(1) because 1 can't be divided.

I know I'm rewriting RCSwitch but could not find what I wanted with existing libs.

Thanks

Ok I divided the speed by 4, use 4 pulses instead of 1 divided to transmit.

Anyway if you have a hint for delaymicros not working.. I'll take.

Thanks

How many credible reports do you find posted here that delayMicroseconds doesn't work?

Have you checked your arithmetic?
Checked the documentation for delayMicroseconds?

  this->pulseLength = 1000 * 1000;

What is the range of values that delayMicroseconds() takes?

     int pulseLength;

this->pulseLength = 1000 * 1000;

The range of value that fit in an int is?

PaulS:

     int pulseLength;

this->pulseLength = 1000 * 1000;



The range of value that fit in an int is?

That's what I did not spot.

Thanks