Go Down

Topic: if with milles does not work (Read 436 times) previous topic - next topic

dehein2

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!?

Code: [Select]
#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

AWOL

Quote
but after a while (few seconds)

Huge clue there - maybe about 32?

Check the return type of "millis"
Code: [Select]
int delay1 = 0;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dehein2

ahhhh ^^ ;)
newbie mistake^^

Thanks a lot

AWOL

You're not the first - it's a common one, particularly if you come from a world where "int"s are 32 bits wide.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Go Up