How to delay() easily when you don't have delay()?

I wanted to have some delay between I2C read/write functions. Ended up using this. How good/bad is it and any pitfalls?

float temperature = readTemperature(); printf("Temperature: %.2f °C\n", temperature); for(int i=0; i<29999; i++); //some delay float humidity = readHumidity(); printf("Humidity: %.2f %\n", humidity); printf("\n"); for(int i=0; i<29999; i++); //some more delay
Solution
@Navadeep there are many disadvantages for using a for loop as you just did like,
The actual delay duration can vary depending on the compiler optimization settings and the CPU speed. this means the delay might not be consistent across different systems or even different runs on the same system,

The for loop delay is a busy-wait loop, meaning the CPU is doing nothing useful during this time but still consuming power. this can be inefficient, especially in battery-powered or resource-constrained environments,

External factors like interrupts or other high-priority tasks can affect the timing, making this method non-deterministic in real-time applications and

This approach is not scalable for longer delays or more complex timing requirements.
Was this page helpful?