if with milles does not work

Hi,
i created the following code to read some sensor values and print them to a LCD. It works wuite good, apart from the delay to show new values every 500ms.
It works at first, the diaply is updated 2/sec, but after a while (few seconds) the if seems always to be true and the value is updated as fast as possbile. I dont really understand why!?

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int SharpPin1 = 0;            
int delay1 = 0;
int OptoPin = 15;

void setup() {
  
  pinMode(SharpPin1, INPUT);
  pinMode(OptoPin, INPUT);

  lcd.begin(20, 4);

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.setCursor(0, 2);
  lcd.print("S1:");
  lcd.setCursor(7, 2);
  lcd.print("O1:");  
}

void loop() {
  lcd.setCursor(2, 0);
  lcd.print(millis()/1000);
  
  
  if(millis()-delay1>500){
    lcd.setCursor(3, 2);
    lcd.print(analogRead(SharpPin1));
    lcd.setCursor(10, 2);
    lcd.print(analogRead(OptoPin));
    delay1 = millis();  
  }
}

Thanks

but after a while (few seconds)

Huge clue there - maybe about 32?

Check the return type of "millis"

int delay1 = 0;

ahhhh ^^ ;) newbie mistake^^

Thanks a lot

You’re not the first - it’s a common one, particularly if you come from a world where "int"s are 32 bits wide.

;) Thanks again