Help with DateTime and TimeSpan in RTClib

Hello guys,

I am working on a complicated kitchen timer but the first leg is, well, a simple kitchen timer. To make it scalable I am creating functions for each task. I find creating a new Alarm time will be easier if I used TimeSpan(0,0,X,0) where X is the time after which the alarm should go off in mins.

I use the below statement in setAlarm(int x) and value of x is passed as an argument to the function.

 DateTime alarm1(now + TimeSpan(0,0,x,0));

Now I want to use alarm1 in another function checkAlarm() but obviously it is a local object and cannot be used outside setAlarm.

Question is how do I construct or declare alarm1 globally?
Assuming the answer is DateTime now = rtc.alarm1(); in the beginning of the code, how can I use this statement DateTime alarm1(now + TimeSpan(0,0,x,0)); in the setAlarm function to achieve the same goal of x mins future time.

Entire Code //Error 'alarm1' was not declared in this scope

#include "RTClib.h"
#include <LiquidCrystal.h>

//initializations
const int rs = 9, en = 8, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
RTC_DS3231 rtc;
DateTime now = rtc.now();
DateTime Alarm1;

//constants
const int upButtonPin = 7;
const int downButtonPin = 8;
const int setupButtonPin = 9;
const int buzzerPin = 10;

//variables
// int counter = 0; //used counter
int timer1 = 0;

int alarm1Hour = 0; //Variables for alarm 1 setting
int alarm1Minute = 0;
int alarm1Second = 0;
bool alarm1_ON = false;


void setup() 
{

//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // comment this line after setting the time once.
lcd.begin(16, 2);
// counter = 0; 
pinMode(upButtonPin, INPUT);
pinMode(13, OUTPUT);  //till we do the much needed soldering
pinMode(10, OUTPUT);  //till we do the much needed soldering
digitalWrite(13, HIGH); //till we do the much needed soldering
digitalWrite(10, LOW); //Buzzer Pin low to start with.

}


void loop() 
{

displayTime();
checkSetupState();
checkAlarm();

}


//modules
void displayTime() 
{

lcd.setCursor(0, 0);  
//  lcd.print("        "); //clear row
lcd.setCursor(0, 0);
lcd.print(now.hour());
lcd.print(":");
lcd.print(now.minute());
lcd.print(":");
lcd.print(now.second());
//delay(500); 
}

void checkSetupState()
{
if(digitalRead(setupButtonPin) == HIGH) 
    {
    lcd.print("Time Now: ");
    lcd.print(now.hour());
    lcd.print(":");
    lcd.print(now.minute()); 
    delay(300);
    timer1 = setupMode(); 
    setAlarm(timer1);
    } 
}

int checkButton(int xButtonPin)
{
  //bool xButtonState;
  int x = 0;
  if(digitalRead(xButtonPin) == HIGH)
    {
      delay(300);
      x = x + 1; //Replace 1 with an increment variable which can be passed in the function along with the pin. Stage II.
      // displayTime(); //Not required as of now might consider
    }
    return x;
}



void displayCounter(int y)
{
  lcd.setCursor(0, 1);  
  lcd.print("                "); //clear row
  lcd.setCursor(0, 1);  
  lcd.print(y);
  lcd.print(" Mins");

}



int setupMode()  //Enters setup mode and checks for up/down input pins activity and accordingly adjusts the value of a variable, 
                //when setupButton is pressed again the function exits and returns value of variable to the calling module.
{
  int x = 0;
  //lcd.clear();
  lcd.setCursor(0, 1); 
  lcd.print("                "); //clear row
  lcd.setCursor(0, 1); 
  lcd.print("Set Mins: ");
  //delay(300);
  while(digitalRead(setupButtonPin) == LOW)
    {
     x = x + checkButton(upButtonPin) - checkButton(downButtonPin);
     displayCounter(x);
    }
    delay(300); //allowance for debounce

  return x;
}

void setAlarm(int x)
  {
   
   DateTime alarm1(now + TimeSpan(0,0,x,0));
   alarm1Hour = alarm1.hour();
   alarm1Minute = alarm1.minute();
   alarm1Second = alarm1.second();

   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("TIMR1 SET FOR: ");
   lcd.setCursor(0,1);
   lcd.print(x);
   lcd.print(" mins | ");
   lcd.print(alarm1.hour());
   lcd.print(":");
   lcd.print(alarm1.minute()); 

   alarm1_ON = true;
   
  }

void checkAlarm()
  {
    if(alarm1_ON == true)
    {
      if(now.hour()==alarm1.hour() && alarm1.minute() == alarm1.minute() == true)
      {
        while(digitalRead(setupButtonPin) == LOW && digitalRead(upButtonPin) == LOW && digitalRead(downButtonPin) == LOW) 
        { 
         tone(10,1); // Buzz of the Buzzer https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/
        }
      
        alarm1_ON = false;
      
      }
    }
  }

Welcome to the forums. +1 Karma for using code tags to post your code. If you look at the complete error message, you will see a lot more information than simply 'not declared in this scope'. There is even a button at the bottom of the IDE to copy your error message so you can paste it here as well.

You have declared a global variable called 'Alarm1' but you are trying to use 'alarm1' Case matters.

Make your global declarations as follows

RTC_DS3231 rtc;
DateTime now;// = rtc.now(); //can't call the function here
DateTime alarm1; //lower case

Modify the loop to look like this

void loop()
{
now = rtc.now();
displayTime();
checkSetupState();
checkAlarm();
}

You need to make sure tht there are pinModes set for all these pins in setup(). You are missing some.

const int upButtonPin = 7;
const int downButtonPin = 8;
const int setupButtonPin = 9;
const int buzzerPin = 10;

Code compiles and prints time to the lcd.