Need help with the code combine

Me and my teammate are going to make a alarm clock with DS3231, LCD 1602-I2C, buzzer with arduino uno.
Individually, we have make 2 part of code work. The first one is the lcd showing the real time clock. The second one is the buzzer beep every 1 minute. However, when we combine 2 codes together, there are error "'clock' was not declared in this scope" starting from
a2 = clock.getAlarm2();

We dont know whether we can combine the code like this.
Can anyone help or any suggestion?Thanks!


#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
#define alarm1Pin 2

RTC_DS3231 rtc; // Create an instance of the RTC_DS3231 class
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the I2C LCD with its address (0x27 for most displays) and dimensions (16x2)

void setup() {

  pinMode(alarm1Pin, OUTPUT);
  
  lcd.begin(); // Initialize the LCD
  lcd.backlight(); // Turn on the backlight (if available on your display)
  lcd.clear(); // Clear the LCD
  lcd.setCursor(0, 0); // Set the cursor to the top-left corner
  lcd.print("Initializing...");

  Serial.begin(115200);
  Wire.begin();
  
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  
  // Uncomment the following lines to set the initial date and time if needed
  rtc.adjust(DateTime(__DATE__, __TIME__));

  clock.armAlarm1(false);
  clock.armAlarm2(false);
  clock.clearAlarm1();
  clock.clearAlarm2();
  clock.setAlarm2(0, 0, 1, 0, DS3231_EVERY_MINUTE);
  clock.setAlarm2(0, 0, 1,     DS3231_MATCH_M);
  checkAlarms();
}
void checkAlarms()
{
  RTCAlarmTime a1;  
  RTCAlarmTime a2;

  if (clock.isArmed1())
  {
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
    switch (clock.getAlarmType1())
    {
      case DS3231_EVERY_SECOND:
        Serial.println("every second");
        break;
      case DS3231_MATCH_S:
        Serial.print("when seconds match: ");
        Serial.println(clock.dateFormat("__ __:__:s", a1));
        break;
      case DS3231_MATCH_M_S:
        Serial.print("when minutes and sencods match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a1));
        break;
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
      case DS3231_MATCH_DT_H_M_S:
        Serial.print("when date, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("d H:i:s", a1));
        break;
      case DS3231_MATCH_DY_H_M_S:
        Serial.print("when day of week, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("l H:i:s", a1));
        break;
      default: 
        Serial.println("UNKNOWN RULE");
        break;
    }
  } else
  {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2())
  {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    switch (clock.getAlarmType2())
    {
      case DS3231_EVERY_MINUTE:
        Serial.println("every minute");
        break;
      case DS3231_MATCH_M:
        Serial.print("when minutes match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a2));
        break;
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
      case DS3231_MATCH_DT_H_M:
        Serial.print("when date, hours and minutes match: ");
        Serial.println(clock.dateFormat("d H:i:s", a2));
        break;
      case DS3231_MATCH_DY_H_M:
        Serial.println("when day of week, hours and minutes match: ");
        Serial.print(clock.dateFormat("l H:i:s", a2));
        break;
      default: 
        Serial.println("UNKNOWN RULE"); 
        break;
    }
  } else
  {
    Serial.println("Alarm2 is disarmed.");
  }
}
void loop() {
  // Read the current date and time from the DS3231
  DateTime now = rtc.now();
  
  // Clear the LCD
  lcd.clear();
  
  // Print the date and time on the LCD
  lcd.setCursor(0, 0); // Set the cursor to the top-left corner
  lcd.print("Date: ");
  lcd.print(now.year(), DEC);
  lcd.print("/");
  printTwoDigits(now.month());
  lcd.print("/");
  printTwoDigits(now.day());

  lcd.setCursor(0, 1); // Set the cursor to the second row
  lcd.print("Time: ");
  printTwoDigits(now.hour());
  lcd.print(":");
  printTwoDigits(now.minute());
  lcd.print(":");
  printTwoDigits(now.second());
  lcd.print("  ");

  // Print the same information to Serial
  Serial.print("Current Date: ");
  Serial.print(now.year(), DEC);
  Serial.print("/");
  printTwoDigits(now.month());
  Serial.print("/");
  printTwoDigits(now.day());
  Serial.print("  Current Time: ");
  printTwoDigits(now.hour());
  Serial.print(":");
  printTwoDigits(now.minute());
  Serial.print(":");
  printTwoDigits(now.second());
  Serial.println();

  delay(1000); // Update every 1 second
}

void printTwoDigits(int number) {
  if (number < 10) {
    lcd.print("0"); // Print a leading zero for single-digit numbers
  }
  lcd.print(number);
}

I see where you instantiate an RTC_DS3231 object as rtc, but I do not see where you instantiate a clock object.

Where do you instantiate a clock object?

Thanks for your reply.

Now I add this to the code:
RTC_DS3231 clock;
DateTime dt;

like this?

"rtc" or "clock" not both.

I have tried to re-write the code with the reference of some tutorial video. However, the lcd display cannot show the time but garbled text. Can someone help if I have the code mistake?
thanks!


#include <Wire.h>
#include <DS3231.h>
#define alarm1Pin 2
DS3231 clock;
RTCDateTime dt;

char daysOfTheWeek[7][12] = {
       "Sunday", "Monday", "Tuesday",
       "Wednesday", "Thursday", "Friday", "Saturday"};
char monthName[12][12] = {
  "January", "February", "March",
  "April", "May", "June",
  "July", "August", "September",
  "October", "November", "December"
};

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  Serial.begin(9600);
  pinMode(alarm1Pin, OUTPUT);
  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

  // Disarm alarms and clear alarms for this example, because alarms is battery backed.
  // Under normal conditions, the settings should be reset after power and restart microcontroller.
  clock.armAlarm1(false);
  clock.armAlarm2(false);
  clock.clearAlarm1();
  clock.clearAlarm2();
 
  // Manual (Year, Month, Day, Hour, Minute, Second)
  clock.setDateTime(2024, 7, 4, 4, 48, 0);

  // Set Alarm - Every second.
  // DS3231_EVERY_SECOND is available only on Alarm1.
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 0, 0, 0, DS3231_EVERY_SECOND);

  // Set Alarm - Every full minute.
  // DS3231_EVERY_MINUTE is available only on Alarm2.
  // setAlarm2(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm2(0, 0, 0, 0, DS3231_EVERY_MINUTE);
  
  // Set Alarm1 - Every 20s in each minute
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  clock.setAlarm1(0, 0, 0, 20, DS3231_MATCH_S);

  // Set Alarm2 - Every 01m in each hour
  // setAlarm2(Date or Day, Hour, Minute, Mode, Armed = true)
  clock.setAlarm2(0, 0, 1,     DS3231_MATCH_M);

  // Set Alarm - Every 01m:25s in each hour
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 0, 1, 25, DS3231_MATCH_M_S);

  // Set Alarm - Every 01h:10m:30s in each day
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(0, 1, 10, 30, DS3231_MATCH_H_M_S);

  // Set Alarm - 07h:00m:00s in 25th day in month
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(25, 7, 0, 0, DS3231_MATCH_DT_H_M_S);

  // Set Alarm - 10h:45m:30s in every Friday (1 - Mon, 7 - Sun)
  // setAlarm1(Date or Day, Hour, Minute, Second, Mode, Armed = true)
  // clock.setAlarm1(5, 10, 40, 30, DS3231_MATCH_DY_H_M_S);
  
  // Check alarm settings
  checkAlarms();
}

void checkAlarms()
{
  RTCAlarmTime a1;  
  RTCAlarmTime a2;

  if (clock.isArmed1())
  {
    a1 = clock.getAlarm1();

    Serial.print("Alarm1 is triggered ");
    switch (clock.getAlarmType1())
    {
      case DS3231_EVERY_SECOND:
        Serial.println("every second");
        break;
      case DS3231_MATCH_S:
        Serial.print("when seconds match: ");
        Serial.println(clock.dateFormat("__ __:__:s", a1));
        break;
      case DS3231_MATCH_M_S:
        Serial.print("when minutes and sencods match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a1));
        break;
      case DS3231_MATCH_H_M_S:
        Serial.print("when hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("__ H:i:s", a1));
        break;
      case DS3231_MATCH_DT_H_M_S:
        Serial.print("when date, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("d H:i:s", a1));
        break;
      case DS3231_MATCH_DY_H_M_S:
        Serial.print("when day of week, hours, minutes and seconds match: ");
        Serial.println(clock.dateFormat("l H:i:s", a1));
        break;
      default: 
        Serial.println("UNKNOWN RULE");
        break;
    }
  } else
  {
    Serial.println("Alarm1 is disarmed.");
  }

  if (clock.isArmed2())
  {
    a2 = clock.getAlarm2();

    Serial.print("Alarm2 is triggered ");
    switch (clock.getAlarmType2())
    {
      case DS3231_EVERY_MINUTE:
        Serial.println("every minute");
        break;
      case DS3231_MATCH_M:
        Serial.print("when minutes match: ");
        Serial.println(clock.dateFormat("__ __:i:s", a2));
        break;
      case DS3231_MATCH_H_M:
        Serial.print("when hours and minutes match:");
        Serial.println(clock.dateFormat("__ H:i:s", a2));
        break;
      case DS3231_MATCH_DT_H_M:
        Serial.print("when date, hours and minutes match: ");
        Serial.println(clock.dateFormat("d H:i:s", a2));
        break;
      case DS3231_MATCH_DY_H_M:
        Serial.println("when day of week, hours and minutes match: ");
        Serial.print(clock.dateFormat("l H:i:s", a2));
        break;
      default: 
        Serial.println("UNKNOWN RULE"); 
        break;
    }
  } else
  {
    Serial.println("Alarm2 is disarmed.");
  }
}

void loop()
{
  dt = clock.getDateTime();
 lcd.clear();
 lcd.setCursor (0,0);
 lcd.print(dt.day);
 lcd.print(" ");
 lcd.print(monthName[dt.month-1]);
 lcd.print(" ");
 lcd.print(dt.year);

 lcd.setCursor (0,1);
 lcd.print(dt.hour);
 lcd.print(":");
 lcd.print(dt.minute);
 lcd.print(":");
 lcd.print(dt.second);
 lcd.print(" ");
 //lcd.print(daysOfTheWeek[dt.dayOfWeek]);

 delay(300);
  Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));

  // Call isAlarm1(false) if you want clear alarm1 flag manualy by clearAlarm1();
  if (clock.isAlarm1())
  {
    Serial.println("ALARM 1 TRIGGERED!");
    digitalWrite(alarm1Pin,HIGH);
    delay(3000);
    digitalWrite(alarm1Pin,LOW);
    delay(300);
    digitalWrite(alarm1Pin,HIGH);
    delay(3000);
    digitalWrite(alarm1Pin,LOW);
    delay(300);
    digitalWrite(alarm1Pin,HIGH);
    delay(3000);
    digitalWrite(alarm1Pin,LOW);
    delay(300);
    digitalWrite(alarm1Pin,HIGH);
    delay(3000);
    digitalWrite(alarm1Pin,LOW);
    delay(300);
    
  }

  // Call isAlarm2(false) if you want clear alarm1 flag manualy by clearAlarm2();
  if (clock.isAlarm2())
  {
    Serial.println("ALARM 2 TRIGGERED!");
  }
 
  delay(1000);
}


Try installing and using diagnostics in the HD44780 library.

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