Adjusting Time on RTC Module with Buttons

I need some help getting some buttons working but I’m getting an error code.

This Clock project is a large seven segment clock.

The clock is currently working but there is some drift in the time, in order to adjust this drift I would like to add a button interface.

My thought was that I could easily create a button that would add to the seconds integer and another button that would subtract from the seconds integer (i.e.

if (digitalRead(Button1)==HIGH){
int s = int s+1;
}
if (digitalRead(Button2)==HIGH){
int s = int s-1;
}
if (digitalRead(Button3)==HIGH){
int m = int m+1;
}
if (digitalRead(Button4)==HIGH){
int m = int m-1;
}

unfortunately I am getting an error “expected primary-expression before ‘int’”

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

: error: expected primary-expression before 'int'

int s = int s+1;

^

Clock_SecMinBoardBTNUPDT:138:13: error: expected primary-expression before 'int'

int s = int s-1;

^

Clock_SecMinBoardBTNUPDT:141:13: error: expected primary-expression before 'int'

int m = int m+1;

^

Clock_SecMinBoardBTNUPDT:144:13: error: expected primary-expression before 'int'

int m = int m-1;

^

exit status 1
expected primary-expression before 'int'

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

#include <SparkFunDS1307RTC.h>
#include <Wire.h>

//commemt out line below if you want month printed before date. 
//E.g. October 13, 2020: 10/13/20 vs 13/10/20
#define PRINT_USA_DATE

#define SQW_INPUT_PIN 2 //Input pin to read SQW
#define SQW_OUTPUT_PIN 13 // LED to indicate SQW's state

// Toggle for Anode or Cathode Display
#define SEGMENT_ON HIGH
#define SEGMENT_OFF LOW

//************Button*****************//
const int Button1 = 6; // button for second +
const int Button2 = 7; // button for second -
const int Button3 = 8; // button for minute +
const int Button4 = 9; // button for minute -

//---seconds Digit Segments (00:00:0X)
int seg_secA = 23; // segment 1, digit 1
int seg_secB = 25;
int seg_secC = 27;
int seg_secD = 29;
int seg_secE = 31;
int seg_secF = 33;
int seg_secG = 35;
//----10's seconds (00:00:X0)
int seg_secA1 = 22;
int seg_secB1 = 24;
int seg_secC1 = 26;
int seg_secD1 = 28;
int seg_secE1 = 30;
int seg_secF1 = 32;
int seg_secG1 = 34;
//---- minutes (00:0X:00)
int seg_minA = 41;
int seg_minB = 43;
int seg_minC = 45;
int seg_minD = 47;
int seg_minE = 49;
int seg_minF = 51;
int seg_minG = 53;
//---- 10's minutes (00:X0:00)
int seg_minA1 = 40;
int seg_minB1 = 42;
int seg_minC1 = 44;
int seg_minD1 = 46;
int seg_minE1 = 48;
int seg_minF1 = 50;
int seg_minG1 = 52;

void setup() {

Serial.begin(9600);
pinMode(SQW_INPUT_PIN, INPUT_PULLUP);
rtc.begin(); // initialize the library
rtc.writeSQW(SQW_SQUARE_1);//1Hz square wave
//Getting information from RTC (RealTimeClock)-------------
rtc.update(); // Update RTC data

pinMode(Button1, INPUT);  //add second
pinMode(Button2, INPUT);  //remove second
pinMode(Button3, INPUT);  //add minute
pinMode(Button4, INPUT);  //remove minute

// Read the time:
int s = rtc.second();
int m = rtc.minute();
int h = rtc.hour();

// Read the day/date:
int dy = rtc.day();
int da = rtc.date();
int mo = rtc.month();
int yr = rtc.year();

//----Setting up pinModes for output to segements
//---- Seconds ---- (00:00:0X)
pinMode(seg_secA,OUTPUT);
pinMode(seg_secB,OUTPUT);
pinMode(seg_secC,OUTPUT);
pinMode(seg_secD,OUTPUT);
pinMode(seg_secE,OUTPUT);
pinMode(seg_secF,OUTPUT);
pinMode(seg_secG,OUTPUT);
//---- Seconds, 10's ---- (00:00:X0)
pinMode(seg_secA1,OUTPUT);
pinMode(seg_secB1,OUTPUT);
pinMode(seg_secC1,OUTPUT);
pinMode(seg_secD1,OUTPUT);
pinMode(seg_secE1,OUTPUT);
pinMode(seg_secF1,OUTPUT);
pinMode(seg_secG1,OUTPUT);
//---- Minutes ---- (00:0X:00)
pinMode(seg_minA,OUTPUT);
pinMode(seg_minB,OUTPUT);
pinMode(seg_minC,OUTPUT);
pinMode(seg_minD,OUTPUT);
pinMode(seg_minE,OUTPUT);
pinMode(seg_minF,OUTPUT);
pinMode(seg_minG,OUTPUT);
//---- Minutes, 10' ---- (00:X0:00)
pinMode(seg_minA1,OUTPUT);
pinMode(seg_minB1,OUTPUT);
pinMode(seg_minC1,OUTPUT);
pinMode(seg_minD1,OUTPUT);
pinMode(seg_minE1,OUTPUT);
pinMode(seg_minF1,OUTPUT);
pinMode(seg_minG1,OUTPUT);
}

void loop() 
{
  static int8_t lastSecond = -1;
  
  // Call rtc.update() to update all rtc.seconds(), rtc.minutes(),
  // etc. return functions.
  rtc.update();

  if (rtc.second() != lastSecond) // If the second has changed
  {
    printTime(); // Print the new time
    
    lastSecond = rtc.second(); // Update lastSecond value
  }
  
  //Button Actions if pressed
  if (digitalRead(Button1)==HIGH){
    int s = int s+1;
  }
  if (digitalRead(Button2)==HIGH){
    int s = int s-1;
  }
  if (digitalRead(Button3)==HIGH){
    int m = int m+1;
  }
  if (digitalRead(Button4)==HIGH){
    int m = int m-1;
  }
  

  // Read the state of the SQW pin and show it on the
  // pin 13 LED. (It should blink at 1Hz.)
  digitalRead(SQW_INPUT_PIN);

//turn all digits off
  digitalWrite(seg_secA, SEGMENT_OFF);
  digitalWrite(seg_secB, SEGMENT_OFF);
  digitalWrite(seg_secC, SEGMENT_OFF);
  digitalWrite(seg_secD, SEGMENT_OFF);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_OFF);
  digitalWrite(seg_secG, SEGMENT_OFF);

//Seconds Digit ------------------------------------------00:00:0[x]
 if (rtc.second()%10 == 0){
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_ON);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_OFF);
 }
 else if (rtc.second()%10 == 1){
  digitalWrite(seg_secA, SEGMENT_OFF);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_OFF);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_OFF);
  digitalWrite(seg_secG, SEGMENT_OFF);
 }
else if (rtc.second()%10 == 2){
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_OFF);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_ON);
  digitalWrite(seg_secF, SEGMENT_OFF);
  digitalWrite(seg_secG, SEGMENT_ON);
}
else if (rtc.second()%10 == 3) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_OFF);
  digitalWrite(seg_secG, SEGMENT_ON);
 }
else if (rtc.second()%10 == 4) {
  digitalWrite(seg_secA, SEGMENT_OFF);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_OFF);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_ON);
}
else if (rtc.second()%10 == 5) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_OFF);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_ON);
}
else if (rtc.second()%10 == 6) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_OFF);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_ON);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_ON);
}
else if (rtc.second()%10 == 7) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_OFF);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_OFF);
  digitalWrite(seg_secG, SEGMENT_OFF);
}
else if (rtc.second()%10 == 8) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_ON);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_ON);
}
else if (rtc.second()%10 == 9) {
  digitalWrite(seg_secA, SEGMENT_ON);
  digitalWrite(seg_secB, SEGMENT_ON);
  digitalWrite(seg_secC, SEGMENT_ON);
  digitalWrite(seg_secD, SEGMENT_ON);
  digitalWrite(seg_secE, SEGMENT_OFF);
  digitalWrite(seg_secF, SEGMENT_ON);
  digitalWrite(seg_secG, SEGMENT_ON);
}
...

 }
//Minutes Digit----------------------------------------- 00:0[x]:00
...

}
void printTime()
{
  Serial.print(String(rtc.hour()) + ":"); // Print hour
  if (rtc.minute() < 10)
    Serial.print('0'); // Print leading '0' for minute
  Serial.print(String(rtc.minute()) + ":"); // Print minute
  if (rtc.second() < 10)
    Serial.print('0'); // Print leading '0' for second
  Serial.print(String(rtc.second())); // Print second

  if (rtc.is12Hour()) // If we're in 12-hour mode
  {
    // Use rtc.pm() to read the AM/PM state of the hour
    if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
    else Serial.print(" AM");
  }
  
  Serial.print(" | ");

  // Few options for printing the day, pick one:
  Serial.print(rtc.dayStr()); // Print day string
  //Serial.print(rtc.dayC()); // Print day character
  //Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
  Serial.print(" - ");
#ifdef PRINT_USA_DATE
  Serial.print(String(rtc.month()) + "/" +   // Print month
                 String(rtc.date()) + "/");  // Print date
#else
  Serial.print(String(rtc.date()) + "/" +    // (or) print date
                 String(rtc.month()) + "/"); // Print month
#endif
  Serial.println(String(rtc.year()));        // Print year
}

If anyone could provide any help it would be greatly appreciated.

No
if (digitalRead(Button1)==HIGH){
int s = int s+1;
}


int sec; // make this global
. . .

if (digitalRead(Button1)==HIGH)
{
sec = sec + 1;
}


You really need to look at using ‘switch change’ instead of using ‘switch level’.

The keyword “int” creates a new variable of type “int”. You must* have only ONE “int X” for each variable named “X” that you use in your program.

At the top of your program declare a global…

int s;

Then everywhere else…

s = 10;
s = s + 1;

etc.

This code…

int s = 10;

Declares a NEW variable “s”, which replaces any previous one with the same name, AND assigns “s” the value 10.
Do NOT ever do this if you already have a previous “int s” in your program. It is the programming equivalent of having two children and calling them both “Bob”. * It’s perfectly legal, but you’d be crazy to try and do it.

Also this code…

int s = int s + 1;

Is nonsense, as the definition of a variable should be on the left of an expression (not the right of an equals).
Edit: And even then attempting to declare a new variable (“int s”) and then also use the same (as yet undefined) variable at the same time in the assignment expression (“s + 1”) makes no sense.

BTW Code tags for formatting your code on the forums look like this: [​code][​/code]. You can insert them with the </> button on the formatting menu.

pcbbc:
The keyword “int” creates a new variable of type “int”. You must* have only ONE “int X” for each variable named “X” that you use in your program.

At the top of your program declare a global...

int s;

Then everywhere else...

s = 10;

s = s + 1;



etc.

This code...


int s = 10;



Declares a NEW variable “s”, which replaces any previous one with the same name, AND assigns “s” the value 10.
Do NOT ever do this if you already have a previous “int s” in your program. It is the programming equivalent of having two children and calling them both “Bob”. * It’s perfectly legal, but you’d be crazy to try and do it.

Also this code...


int s = int s + 1;



Is nonsense, as the definition of a variable should be on the left of an expression (not the right of an equals).
Edit: And even then attempting to declare a new variable (“int s”) and then also use the same (as yet undefined) variable at the same time in the assignment expression (“s + 1”) makes no sense.

I see what you are saying about defining the integer s more that once being an issue. What I am attempting to do is add to integer s which = rtc.second(); as seen in the code.

  int s = rtc.second();

How would you recommend adding or subtracting time to the rtc module?

larryd:
No
if (digitalRead(Button1)==HIGH){
int s = int s+1;
}


int sec; // make this global
. . .

if (digitalRead(Button1)==HIGH)
{
sec = sec + 1;
}


You really need to look at using ‘switch change’ instead of using ‘switch level’.

Thank you for this, I appreciate the guidance on creating a global int and using 'switch change' but I am still struggling to actually add a second to the rtc.second

Excuse me, drift in time?

TheUNOGuy:
Excuse me, drift in time?

I am experiencing drift in time, the clock module seems to be counting seconds ever so slightly to quickly. and want to have some buttons to combat this drift.

Here is a tutorial on setting the RTC time.

pcbbc:
Here is a tutorial on setting the RTC time.

This tutorial unfortunately utilizes a different RTC library, so I would have to reconfigure my already functioning code.

As I have never done anything quite like this could you guide me in creating a temporary value storage so that I can pull the stored time and date values from memory and populate a new "rtc.setTime" with those values with time added? Or is that a stupid way of going about it?

Which RTC Module (DS3231?), which Library (RTClib.h?), and which Arduino (UNO?) are you using?

Which DPins are you using for "INC Button" and "DEC Button"?

j_hewitt:
This tutorial unfortunately utilizes a different RTC library, so I would have to reconfigure my already functioning code.

The principle is the same though. Read the time, adjust it with the buttons, write it back.

Is there some aspect of the example you don’t understand? Or some aspect of the library you are using you don’t understand?