user76504: I had written some code that involved doing something after 10 milliseconds had elapsed, however it was performing unexpectedly. Upon debugging, I think I have identified the error, and I wrote some code with just that block:
unsigned long int newtime=0;
unsigned long int prevtime=0;
int time;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
newtime=millis();
time=newtime-prevtime;
if(time==10){
  Serial.println(prevtime);
  prevtime+=10;
}
    
}

The serial monitor "freezes on 150. it shows the values 0,10,20...as expected but it freezes on 150...and then nothing happens.
What exactly is causing this?

user31668: your logic is just tricky to confuse. It is repeatable because along as you not change the if logic, the code is going to run till it displays 150. then it will stop. You can also check the same behaviour here
Once the time difference is not exactly equal to 10, the if part will not be executed. Hence, the prevtime will not catch up with the newtime anymore. newTime will soon grow to be bigger and the difference will always be greater than 10.
I suggest visiting https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay to get some hints on tracking time in a safer way.

user42015: The problem is, that you are filling up the Serial buffer rather fast, because you try to send ASCII numbers every 10ms at only 9600ms. When the buffer is full, Serial.println() will wait, until there is enough space free in the buffer for the current data. That makes your loop lasting significantly longer.
In that case the time, where the time is exactly 10 is missed. Thus the code does nothing at that point, until time rolls over and gets 10 again (48 days I think).
While you can mitigate the problem by using a higher baudrate, the main problem is still
if(time==10){

You have only provided an example code. Your real code might sometimes take longer than 10ms out of some other reason (actually this already gets critical, when the loop takes longer than 1ms, since it then might already miss the one moment, where time is 10). To futureproof your code and make sure, that you don't hunt down the same bug again and again, you should do, what you were told in the comments, and use
if(time>=10){

Depending on your code the event will still happen after 10ms the absolute most of the time. But this variant also doesn't break, if the loop takes longer than 10ms sometimes.

76504: <p>I had written some code that involved doing something after 10 milliseconds had elapsed, however it was performing unexpectedly. Upon debugging, I think I have identified the error, and I wrote some code with <em>just</em> that block:</p>
<pre><code>unsigned long int newtime=0;
unsigned long int prevtime=0;
int time;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
newtime=millis();
time=newtime-prevtime;
if(time==10){
  Serial.println(prevtime);
  prevtime+=10;
}
    
}
</code></pre>
<p>The serial monitor &quot;freezes on 150. it shows the values 0,10,20...as expected but it freezes on 150...and then nothing happens.</p>
<p>What exactly is causing this?</p>
 31668: <p>your logic is just tricky to confuse. It is repeatable because along as you not change the if logic, the code is going to run till it displays 150. then it will stop. You can also check the same behaviour <a href="https://wokwi.com/arduino/projects/297224412670722569" rel="nofollow noreferrer">here</a></p>
<p>Once the time difference is not exactly equal to 10, the if part will not be executed. Hence, the <code>prevtime</code> will not catch up with the <code>newtime</code> anymore. newTime will soon grow to be bigger and the difference will always be greater than 10.</p>
<p>I suggest visiting <a href="https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay" rel="nofollow noreferrer">https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay</a> to get some hints on tracking time in a safer way.</p>
 42015: <p>The problem is, that you are filling up the Serial buffer rather fast, because you try to send ASCII numbers every 10ms at only 9600ms. When the buffer is full, <code>Serial.println()</code> will wait, until there is enough space free in the buffer for the current data. That makes your loop lasting significantly longer.</p>
<p>In that case the time, where the <code>time</code> is exactly 10 is missed. Thus the code does nothing at that point, until time rolls over and gets 10 again (48 days I think).</p>
<p>While you can mitigate the problem by using a higher baudrate, the main problem is still</p>
<pre><code>if(time==10){
</code></pre>
<p>You have only provided an example code. Your real code might sometimes take longer than 10ms out of some other reason (actually this already gets critical, when the loop takes longer than 1ms, since it then might already miss the one moment, where <code>time</code> is 10). To futureproof your code and make sure, that you don't hunt down the same bug again and again, you should do, what you were told in the comments, and use</p>
<pre><code>if(time&gt;=10){
</code></pre>
<p>Depending on your code the event will still happen after 10ms the absolute most of the time. But this variant also doesn't break, if the loop takes longer than 10ms sometimes.</p>
