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, ...
Jump to solution
4 Replies
ZacckOsiemo
ZacckOsiemo7d ago
Only bad is it's blocking. Perhaps you have a timer you can use to wait? And let your uC do other things?
Solution
RED HAT
RED HAT6d ago
@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.
RED HAT
RED HAT6d ago
you can use alternatives like usleep or nanosleep in C (POSIX Systems), HAL_Delay in STM32 HAL Library, and std::this_thread::sleep_for in C++1 depending on which is more compatible with your program sorry for some of the blunders english is not my first language
Navadeep
Navadeep4d ago
Thanks for the inputs. I have switched to a timer generated delay now from the 4MHz clock freq. It is included under the microchip code configurator generation.
No description
Want results from more Discord servers?
Add your server
More Posts
Power Supply BoardMy name is Barileloo and I'm an electronics ethusiast. I'm currently working on a printed and doing Trying to access the sensor's registers using memory-mapped I/O in a flat memory modelHi, I'm working on a small embedded system project using x86-64 assembly on an Intel Atom E3845 micrAre you looking for a developer?plz contact me if you need a developerDoes anyone know if there are any special steps to using SPI1 on the STM32F407-DISC1 board?Does anyone know if there are any special steps to using SPI1 on the STM32F407-DISC1 board. SpecificHow can I scan for available I2C devices on the bus?@Middleware & OS @everyone How can I scan for available I2C devices on the bus? Am getting the errorBalancing Innovation and Regulation in IoT: Ensuring Security, Privacy, and Sustainability* What role should government regulations play in IoT infrastructure development to balance innovatiHELP: Need to configure MCP2515 using Raspberry pi's SPI 0 bus.I am using Raspberry pi 3b+, MCP2515 (CAN Controller), TCAN1042 (CAN Transreceiver) . --------------MPLAB VSCode extension! official announcement by MicrochipThe VS Code extensions merge the capabilities of MPLAB X IDE with the flexibility of VS Code to creaI want to update/flash the firmware on an STM32G4 using another ESP32 microcontroller over I2C.@Middleware & OS I want to update/flash the firmware on an STM32G4 using another ESP32 microcontrolCan i use Edge Impulse to collect data, train a model and deploy it on an Arduino Nano 33 BLE Sense@Middleware & OS @everyone Hello guys, can I use Edge Impulse to collect data, train a model, and de