Problem displaying variable using a LCD

Hello,
I am working on a project, and part of it involves using an LCD screen to display two variables. The variables are supposed to change when I press a button. The code needs to reset(as it is a tracker) every day and week. The code works fine until I added in the code to reset the variables(starting at line 72), and now it is constantly saying that the variables are equal to zero. I am using the Elegoo Uno R3 board. The LED is just suposed to turn off when the button is pushed, and works fine in both with and without the code after line 72.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int daily = 0;
int weekly = 0;
int ledPin = 13;
int buttonBpin = 3;
int Time = 0;
int day = 0;
int multi = 0;
int totalDay = 1;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Daily:");
  lcd.setCursor(9, 0);
  lcd.print("/100");

  lcd.setCursor(0, 1);
  lcd.print("Weekly:");
  lcd.setCursor(10, 1);
  lcd.print("/721");

  pinMode(ledPin, OUTPUT);
  pinMode(buttonBpin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  lcd.setCursor(6, 0);
  lcd.print(daily);
  lcd.setCursor(7, 1);
  lcd.print(weekly);


  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, LOW);
    daily = daily + 1;
    weekly = weekly + 1;
    delay(1000);
    Serial.print(daily);
    Serial.print(",");
    Serial.print(weekly);
    Serial.print(",");
    Serial.println (totalDay);
  }

  else
  {
    digitalWrite(ledPin, HIGH);
  }

  if (daily > 999)
    daily = 999;
  lcd.setCursor(9, 0);
  lcd.print("/100");

  if (weekly > 999)
    weekly = 999;
  lcd.setCursor(10, 1);
  lcd.print("/721");


  Time = millis() / 1000;
  multi = 86400 * totalDay;

  if (Time >= multi)
  { day = day + 1;
    daily = 0;
    totalDay = totalDay + 1;
  }

  if (day = 7)
  { weekly = 0;
    day = 0;
    daily = 0;
  }

}

Any help explaining this would be great.
Thanks

The multi and Time variables are declared as int, which has a maximum value of 32767, so you are having integer overflow. Use unsigned long when dealing with millis() values.

  if (day = 7)

Whoops !

Did you mean to test whether day equals 7 ?

david_2018:
The multi and Time variables are declared as int, which has a maximum value of 32767, so you are having integer overflow. Use unsigned long when dealing with millis() values.

Could you explain how to do this further?

tvogtman:
Could you explain how to do this further?

Look at the Blink Without Delay example sketch that comes with the IDE.

I changed the int to unsigned long, it now looks like this, but still does not work.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int daily = 0;
int weekly = 0;
int ledPin = 13;
int buttonBpin = 3;
unsigned long Time = 0;
int day = 0;
unsigned long multi = 0;
int totalDay = 1;

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Daily:");

  lcd.setCursor(0, 1);
  lcd.print("Weekly:");

  pinMode(ledPin, OUTPUT);
  pinMode(buttonBpin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  lcd.setCursor(8, 0);
  lcd.print(daily);
  lcd.print("/100");
  lcd.setCursor(8, 1);
  lcd.print(weekly);
  lcd.print("/721");

  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, LOW);
    daily = daily + 1;
    weekly = weekly + 1;
    delay(500);
    Serial.print(daily);
    Serial.print(",");
    Serial.print(weekly);
    Serial.print(",");
    Serial.println (totalDay);
  }

  else
  {
    digitalWrite(ledPin, HIGH);
  }

Time = millis() / 1000;
  multi = 86400 * totalDay;

  if (Time >= multi)
  { day = day + 1;
    daily = 0;
    totalDay = totalDay + 1;
  }

  if (day = 7)
  { weekly = 0;
    day = 0;
    daily = 0;
  }
 

  
}

tvogtman:
I changed the int to unsigned long, it now looks like this, but still does not work.

See reply #2

tvogtman:

 multi = 86400 * totalDay;

This is prone to issues as well. The value 86400 does not fit in a 16-bit value (the default size used), totalDay is an int, so the compiler likely does not use 32-bit integers for this calculation.
Try this instead:

 multi = 86400ul * totalDay;

Hi,
Welcome to the forum.

Did you fix the if statement pointed out in post#2?

Tom... :slight_smile:

david_2018:
See reply #2

Thank you for pointing that out, that did the trick! The code runs just as intended. Thank you all for helping out.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.