https://itp.nyu.edu/classes/prototypingelectronicdevices/wp-content/uploads/sites/105/2022/10/Class-9-Multitasking.pdf
<aside>
What exactly is delay() doing in Arduino? in ArduinoCore-avr Github page, the definition is as below:
void delay(unsigned long ms)
{
uint32_t start = micros();
while (ms > 0) {
yield();
while ( ms > 0 && (micros() - start) >= 1000) {
ms--;
start += 1000;
}
}
}
Ways to utilize the delay time —> use millis() to replace delay().
</aside>
<aside>
if we look into the structure and definition of avr, searching for avr gcc, we see that the size for “long” is 4 bytes (32 bits)

Arduino millis() and micros() are both defined as “long”, see the below definition in Arduino documentation:
unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
}
unsigned long micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;
cli();
m = timer0_overflow_count;
#if defined(TCNT0)
t = TCNT0;
#elif defined(TCNT0L)
t = TCNT0L;
#else
#error TIMER 0 not defined
#endif
#ifdef TIFR0
if ((TIFR0 & _BV(TOV0)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV0)) && (t < 255))
m++;
#endif
SREG = oldSREG;
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
millis() can store about 50 days of data before overflow: 2^32/1000/60/60/24=49.71
micros() can store about 1 hour of data before overflow: 2^32/1000/1000/60/60=1.193 </aside>
<aside>
void init(){
}
External Interrupt this function can be utilized when there’s a time consuming and un-breakdownable function in the code
delay(1000);
checkButtonPressed();
During the 1 second of delay, if I still want to use the checkButtonPressed(), and detect the state of the button, I have to use external interrupt, so that when certain condition is met (in this case, button is pressed), the delay() would be postponed for just a small amount of time to mark the button as pressed. For Uno, this function only exists in pin 2/3.
Real time usage: a button check during WIFI connection (button check is quick; WIFI connection takes a lot of time)
However, if the data that is being read when external interrupt happens is more than 1 byte(Uno can read only 1 byte at a time), if the interrupt happens directly between reading different bytes, the data will be incorrect. —> the simplest way is to use volatile and atomic to avoid this.
Internal ways of stopping external interrupt in Arduino logic:
As the code comment, the cli() will disable all interrupts when reading time, so data is precise.
</aside>
unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
}
<aside>
question:
I want to know about how to use documentation for Arduino/pi on GitHub. What info can we use?
</aside>