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;
}
}
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.
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.
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: