Pages: [1]   Go Down
Author Topic: if with milles does not work  (Read 282 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but after a while (few seconds)
Huge clue there - maybe about 32?

Check the return type of "millis"
Code:
int delay1 = 0;
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ahhhh ^^ smiley-wink
newbie mistake^^

Thanks a lot
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley-wink
Thanks again
Logged

Pages: [1]   Go Up
Jump to: