micros accuracy pulse?

Is there a way to get better accuracy using micros.
this test code i have been testing with the scope the pulses drift abit …
some of my readings // 271us. 481us. 277us. 475us. 487us
should i be doing something different ??

unsigned long previousM, currentM;      
long interval = 275;   
long interval1 = 475;   
int PULSE = 0;

void setup() 
{
  
}

void loop() 
{
  currentM = micros();
  if(currentM - previousM >= interval && PULSE == 0 ) { PULSE = 1; PORTB |= _BV(PB5); previousM = currentM; } // D13 // HIGH
  if(currentM - previousM >= interval && PULSE == 1 ) { PULSE = 2; PORTB &= ~_BV(PB5); previousM = currentM; } // D13 // LOW
  if(currentM - previousM >= interval1 && PULSE == 2 ) { PULSE = 3; PORTB |= _BV(PB5); previousM = currentM; } // D13 // HIGH
  if(currentM - previousM >= interval1 && PULSE == 3 ) { PULSE = 0; PORTB &= ~_BV(PB5); previousM = currentM; } // D13 // LOW
}

You MUST NEVER use a micro if you want “accuracy”, What ever accuracy may mean!

Mark

The micros() timer is only accurate to 4 microseconds. The processor can only execute 16 instructions per microsecond so it is unlikely your loop() will run faster than a few microseconds.

If you want a specific pattern you can use nop; to delay one instruction cycle and insert enough in your code to get the timing you want. TO get consistent timing you will need to call noInterrupts() to disable interrupts. This will prevent your code from getting interrupted.

You can also do some pulse generation in hardware with a Timer/Counter.