Variable issues when using time libary

hey team, have started playing around with the time library (https://playground.arduino.cc/Code/Time) but am running into a couple problems when trying to take the time value and place it into another variable. This is what I have at the moment:

#include <TimeLib.h>

int secondvar = 0; 

void setup() 
{
  Serial.begin(9600);  
}

void clockDisplay()
{
  Serial.print(hour());
  Serial.print(minute());
  Serial.print(second());
  Serial.print("\n");
}

void loop() 
{
    if(second() =! secondvar)
    {
      clockDisplay();
      delay (1000);
      second(t) = secondvar; 
    }
}

In my head, the code should set second var to 0, then, once the time in seconds increases to 1, the if statement should be correct and follow through with its contents. I am getting this error message though for line 20:

Arduino: 1.8.3 (Windows 10), Board: "Arduino Mega ADK"

Arduino_test:21: error: lvalue required as left operand of assignment

     if(second(t) =! secondvar)

                  ^

Multiple libraries were found for "TimeLib.h"
 Not used: C:\Program Files (x86)\Arduino\libraries\Time-master
 Not used: C:\Program Files (x86)\Arduino\libraries\Time-master
 Not used: C:\Program Files (x86)\Arduino\libraries\Time-master
 Not used: C:\Program Files (x86)\Arduino\libraries\Time-master
exit status 1
lvalue required as left operand of assignment

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Is this due to my choice in variable types or is it more difficult to take a time value and whack it straight into a random variable?

Cheers

The clue is in the error message when it refers to "assignment".

if(second() =! secondvar)

This code takes the integer secondvar, converts it to a bool, applies the not operator ! to it to invert its value, then attempts to assign it to the result of a function call. It won't work.

The operator you were looking for is !=

"=!" ?

if(second(t) != secondvar)

arduarn: The operator you were looking for is !=

Even had it right the first time then changed it as I thought it looked wrong!

arduarn: This code takes the integer secondvar, converts it to a bool, applies the not operator ! to it to invert its value, then attempts to assign it to the result of a function call. It won't work.

What would you suggest to avoid this issue?

chickwolf: What would you suggest to avoid this issue?

Just fix the operator to != I was only explaining how the compiler was interpreting things so you could see the connection between the assignment error and the code as written.

arduarn: Just fix the operator to != I was only explaining how the compiler was interpreting things so you could see the connection between the assignment error and the code as written.

Ahhhhh ok, I understand. Say I wanted to do something like:

hour() = hour() + 1;

How would I go about doing that? It seems to just return errors.

chickwolf: Ahhhhh ok, I understand. Say I wanted to do something like:

hour() = hour() + 1;

How would I go about doing that? It seems to just return errors.

The function call returns a temporary variable which doesn't exist after the statement, so it doesn't make sense to assign to it. You could have the function return a pointer which you then dereference and assign to, but it would be bad style.

If you want a copy of of the value of hour(), then assign it to a variable. If you want to modify the value that hour() returns, then use a corresponding setHour(hour() + 1) function or similar.

(in a general style sense by the way, obviously the TimeLib that you are using will probably be incrementing the time for you)

arduarn:
The function call returns a temporary variable which doesn’t exist after the statement, so it doesn’t make sense to assign to it.
You could have the function return a pointer which you then dereference and assign to, but it would be bad style.

If you want a copy of of the value of hour(), then assign it to a variable.
If you want to modify the value that hour() returns, then use a corresponding setHour(hour() + 1) function or similar.

(in a general style sense by the way, obviously the TimeLib that you are using will probably be incrementing the time for you)

Great reply, thanks alot!

The Time Library does increment for me, but I wanted to be able to link in a couple switches to increment the time manually to set it and what not. Have tried implementing the setHour(hour()+1), but I am still just returning errors, are you able to see where it is I am going wrong?

#include <TimeLib.h>

int secondvar = 0;
int setHour (0);
char AM;
char PM;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool debounceSwitch = false;


void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  attachInterrupt (4, incrementHour, CHANGE);
  attachInterrupt (5, decrementHour, CHANGE);
}
void debounce()
{
  lastDebounceTime = millis();
  if (digitalRead(3) == HIGH && debounceSwitch == false)
  {
    clockDisplay();
    delay(debounceDelay);
    debounceSwitch = true;
  }
  if (digitalRead(3) == LOW && debounceSwitch == true)
  {
    delay(debounceDelay);
    debounceSwitch = false;
  }
}
void clockDisplay()
{
  if (hour() == 0) {
    Serial.print("12AM\n");
  }
  if (hour() == 1) {
    Serial.print("1AM\n");
  }
  if (hour() == 2) {
    Serial.print("2AM\n");
  }
  if (hour() == 3) {
    Serial.print("3AM\n");
  }
  if (hour() == 4) {
    Serial.print("4AM\n");
  }
}

void incrementHour()
{
  setHour(hour()++);
}
void decrementHour()
{
  setHour(hour()--
}
void loop()
{
  debounce();
}

There is probably alot of nonsense in the code as I am trying out some different things, just wondering about the changing of the hour variable though

int secondvar = 0;
int setHour (0);

Try to be consistent, it is easier for a reader.

  if (hour() == 0) {
    Serial.print("12AM\n");
  }
...
  if (hour() == 4) {
    Serial.print("4AM\n");
  }

There are lots of ways to do this better.

void incrementHour()
{
  setHour(hour()++);
}
void decrementHour()
{
  setHour(hour()--
}

hour()++ is (sort of, but not quite) shorthand for hour() = hour() + 1 which, as we already discussed, will not work because the return value of hour() is temporary.

I presented the setHour() function to describe in a general sense what would be better style, rather than suggesting it was the correct thing in this case. Based on the link you posted, there is no setHour() function, so that won't work either.

You have these functions available:

 setTime(t); // Set the system time to the
 // given time t
 setTime(hr,min,sec,day,month,yr); // Another way to set
 // the time
 adjustTime(adjustment); // Adjust system time by adding
 // the adjustment value

Probably the second option is best for you. You will need to use hour() + 1 as one of the parameters for adding an hour, and the other parameters just the corresponding plain get...() functions.

Great advice, implemented the second option like you suggested which seems to be working perfectly, only issue now is that the code just seems to loop treat pin three as constantly high, and run through the code quite rapidly. I have a switch currently linked up to pin 3 which has been working fine so I think it is more likely an issue with the code.

#include <TimeLib.h>

int secondvar = 0;
int setHour = 0;
char AM;
char PM;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool debounceSwitch = false;


void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  attachInterrupt (4, incrementHour, CHANGE);
  attachInterrupt (5, decrementHour, CHANGE);
}
void debounce()
{
  lastDebounceTime = millis();
  Serial.print("test point 1");
  if (digitalRead(3) == HIGH && debounceSwitch == false)
  {
    clockDisplay();
    delay(debounceDelay);
    debounceSwitch = true;
  }
  if (digitalRead(3) == LOW && debounceSwitch == true)
  {
    delay(debounceDelay);
    debounceSwitch = false;
  }
}
void clockDisplay()
{
  if (hour() >= 0 || hour() <= 11) {
    Serial.print(hour());
    Serial.print("AM\n");
  }
  else if (hour() >= 12 || hour() <= 23) {
    Serial.print(hour());
    Serial.print("PM\n");
  }

}

void incrementHour()
{
  setTime(hour() + 1, minute(), second(), day(), month(), year());
  clockDisplay();
}
void decrementHour()
{
  setTime(hour() - 1, minute(), second(), day(), month(), year());
  clockDisplay();
}
void loop()
{
  debounce();
}

Any ideas?

  if (hour() == 0) {
    Serial.print("12AM\n");
  }
...
  if (hour() == 4) {
    Serial.print("4AM\n");
  }

This was just temporary testing, think it looks a bit better now.

chickwolf: Great advice, implemented the second option like you suggested which seems to be working perfectly, only issue now is that the code just seems to loop treat pin three as constantly high, and run through the code quite rapidly. I have a switch currently linked up to pin 3 which has been working fine so I think it is more likely an issue with the code.

Do you have an appropriate pull-up or pull-down resistor so that the input is not floating when the button is not pushed?

chickwolf: This was just temporary testing, think it looks a bit better now.

Definitely an improvement.