 # Variable set to constant 0

I want to display a clock on a LiquidCrystal Display. Therefore i created this program in order to always set the seconds to 0 every time it equals to 60. As well as set the seconds int back to 0 it is supposed to set the minute int to 1 every time seconds is equal to 60.

The actual timing works ok but when i add the if statement in order to set the int Seconds back to 0 when it reaches 60, the program doesnt work out and the int is set to a constant 0. What is it that im doing wrong here? To me this seems like it should work but it doesnt. Im pretty new to programming in Arduino so a simple explanation would be best if possible.

`````` int zero = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.clear();

Serial.begin(9600);
}

void loop() {

Serial.print(y);

second();
minute();

lcd.setCursor(0,0);
lcd.print(hours);
lcd.setCursor(3,0);
lcd.print(":");
lcd.setCursor(4,0);
lcd.print(minutes);
lcd.setCursor(7,0);
lcd.print(":");
lcd.setCursor(8,0);
lcd.print(seconds);
}

void second(){
delay(1000);
seconds=++y;
}

void minute(){
if (seconds == 60);
{
minutes = ++x;
seconds = 0;
}
}
``````
``````y=++y;
``````

This has undefined behavior.

Use this:

``````++y;
``````

or:

``````y = y + 1;
``````

But don't try to combine the two

Ok thanks, however the problem still persists. I should have maybe explained this a little better but when i run the program it seems that when i use if (seconds == 60) the value is always 60 and the seconds int will be set to 0 as well as starting the minutes count.

``````void minute(){
if (seconds == 60);
{
minutes = ++x;
seconds = 0;
}
``````

Because you have another semicolon where it doesn’t belong. The one at the end of the if statement there terminated the conditional block. So this says, if (seconds == 60) do nothing. Then increment minutes and set seconds to 0 no matter what.

Should be:

``````void minute(){
if (seconds == 60)
{
minutes = ++x;
seconds = 0;
}
``````

I also noticed that x and y aren't defined anywhere in your sketch. Aren't they giving you an error?

Ok thanks that seems to have fixed it. Thought it was a minor noob mistake.

Delta_G: I also noticed that x and y aren't defined anywhere in your sketch. Aren't they giving you an error?

In my actual program i defined them as x, y and z because i thought the name might be giving a problem however to easier understand me i changed most of the int's here to seconds and minutes

arduinouser111: Ok thanks that seems to have fixed it. Thought it was a minor noob mistake. In my actual program i defined them as x, y and z because i thought the name might be giving a problem however to easier understand me i changed most of the int's here to seconds and minutes

When you don't post the same code here that you are having problems with, then the help you get may end up not being pertinent to your actual code. You should always post the actual code you are having issue with.

untested

``````int x = 0, y = 0, z = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop() {

x++;
delay(1000);

Running();

lcd.setCursor(0, 0);        // maybe you have to adjust the cursors here
lcd.print(z);
lcd.setCursor(3, 0);
lcd.print(":");
lcd.setCursor(4, 0);
lcd.print(y);
lcd.setCursor(7, 0);
lcd.print(":");
lcd.setCursor(8, 0);
lcd.print(x);

Serial.print(z);
Serial.print(":");
Serial.print(y);
Serial.print(":");
Serial.println(x);
}

void Running() {

if (x == 60)
{
y ++;
x = 0;
}
else if (y == 60)
{
z++;
y = 0;
}

else if (z == 24)
{

z = 0;
}

}
``````
``````void Running() {

if (x == 60)
{
y ++;
x = 0;
}
else if (y == 60)
{
z++;
y = 0;
}

else if (z == 24)
{

z = 0;
}

}
``````

Should those really be else if? If seconds roll over, you don't want to check to see if they caused minutes to roll too?

else is not right

you are right, pal

i just wrote it on the fly also I have just tested it

I saw 0:60:0 before seeing 1:0:0

Let me remove the else and try again

thanks

@ arduionuser

``````#include <LiquidCrystal.h>

int seconds = 0, minutes = 0, hours = 0;

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

void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
}

void loop() {

seconds++;
delay(1000);

Running();

lcd.setCursor(0, 0);
lcd.print(hours);
lcd.setCursor(3, 0);
lcd.print(":");
lcd.setCursor(4, 0);
lcd.print(minutes);
lcd.setCursor(7, 0);
lcd.print(":");
lcd.setCursor(8, 0);
lcd.print(seconds);

Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
}

void Running() {

if (seconds == 60)
{
minutes ++;
seconds = 0;
}
if (minutes == 60)
{
hours++;
minutes = 0;

}

if (hours == 24)
{
hours = 0;
}
}
``````