Using millis() to toggle an LED every 500 milliseconds
if (!(millis() % 500)) ...


There are two issues with this. The first, and most obvious, is that the
condition will be true for a full millisecond. During that millisecond
you will be toggling the LED on and off very fast. Whether you end up
doing an odd or even number of toggles is anyone's guess.

The second and less obvious problem is that millis() skips some
values. Roughly one integer every 42 is skipped over. This is because
the millis() counter is incremented once every 1024&nbsp;µs, getting
24&nbsp;µs late on each increment. Once all those delays add up to a
full millisecond, millis() catches up by incrementing the counter by
two milliseconds instead of one. This is why you should never condition
something on millis() having one specific value.

The almost correct solution to this problem is shown in Juraj's answer:


  if (millis() &gt; next) ...


This is not quite correct because it fails each time millis() rolls
over back to zero. The correct solution is

static unsigned long last;
if (millis() - last &gt;= 500) {
    last += 500;
    ...
}


Because the subtraction follows the rules of modular arithmetic, this is
guaranteed to work across the rollover events.