The superloop
Our ESP32 runs a superloop: loop() calls three jobs in order, then starts again, forever. The CPU, the part that runs code, can only do one job at a time. Check the button, read the temperature, draw the screen, repeat.
The DS18B20 sensor needs up to 750 ms to measure at full 12-bit resolution. Our readTemp() asks, then waits with delay(750). For those 750 ms the CPU does nothing useful, and no other job can run.
drawScreen() sends 1,024 bytes to the screen over I2C, about 23 ms at 400 kHz. Checking the button takes almost no time. So one pass of loop() is 750 + 23 = 773 ms: each job runs only once every 773 ms.
Where it breaks
Press the button just after checkButton() has looked. Nothing notices until the loop comes round again: up to 773 ms later. People feel a delay above about 100 ms, so the device feels slow. A quick tap may even end unseen.
An interrupt catches the press the instant it happens, so no tap is missed. Its ISR (interrupt service routine) must be short and cannot wait, so it only sets a flag. The real reaction still runs in loop(), up to 773 ms later.
Rewrite readTemp() so it never waits: start a reading, return, and collect it on a later pass once 750 ms have passed. A pass drops to about 23 ms. But every job must be cut up by hand, and one blocking job spoils it.
Tasks and priorities
An RTOS, a real-time operating system such as FreeRTOS, lets each job be its own task: a little loop with its own stack. Each task gets a priority, higher number meaning more urgent: button 3, screen 2, temp 1.
While the sensor measures, the temp task calls vTaskDelay(pdMS_TO_TICKS(750)): sleep 750 ms, counted in ticks, the RTOS's clock beats. It is Blocked, using no CPU time. The scheduler, the part of FreeRTOS that picks who runs, gives the CPU to the screen task.
A press fires the ISR, which gives a semaphore with xSemaphoreGiveFromISR. That wakes the button task, priority 3, above the running screen task. FreeRTOS switches as the ISR ends: the press is handled at once, then the screen carries on.
Each task needs its own stack. Ours get 2,048 bytes, so three take 6 KB: fine in the ESP32's 520 KB, too much for an Uno's 2 KB. Shared data also needs care. Keep the superloop while one pass beats your tightest deadline.
What happens
;; step 1/10 · superloop
superloop loop() runs its jobs in order, forever
line 2 checkButton()
line 3 readTemp()
line 4 drawScreen()
one core: while one job runs, the others wait
// ESP32 (Arduino core): button on pin 0, DS18B20, SSD1306 OLED
void readTemp() {
startConversion(); // Convert T on the 1-Wire bus
delay(750); // DS18B20: up to 750 ms at 12 bits
temp = readScratchpad();
}
void loop() {
checkButton();
readTemp(); // 750 ms
drawScreen(); // 23 ms
}
The card shows loop(); the panel shows the whole sketch. startConversion() and readScratchpad() stand for the usual OneWire calls (Convert T, then Read Scratchpad). checkButton() takes microseconds, so we count it as 0 ms; 773 ms is the sum of the other two jobs. 23 ms is a lower bound: 1,024 bytes x 9 clocks x 2.5 µs, the SSD1306's fastest I2C clock (400 kHz). The address and control bytes, and a slower 100 kHz bus, make a real redraw take longer. The ESP32 has two cores, and ESP-IDF's FreeRTOS can run two tasks at the same moment. To keep the picture simple we follow one core; the rule "the highest-priority Ready task runs" holds on each.