Rtc ds1302 clock

Hello! I made an alarm clock. i didnt realised i have this module and i made the clock work mechanical :)..
Here's the code:

#include <TM1637Display.h>
#include <DHT.h>
#include <toneAC.h>
#define DHTPIN 10  // Specificați pinul de conexiune al senzorului DHT11
#define DHTTYPE DHT11 // Specificați tipul senzorului
#define CLK_PIN 8 // Pinul conectat la pinul CLK al afișajului
#define DIO_PIN 9 // Pinul conectat la pinul DIO al afișajului
#define BUTTON_PIN 5  // Pinul conectat la butonul pentru selectarea modului
#define BUTTON_PIN2 6 // Pinul conectat la butonul pentru afișarea umidității
#define ALARM_SET_PIN 4 // Pinul conectat la butonul pentru setarea alarmei
#define BUZZER_PIN 7  // Pinul la care este conectat buzzer-ul

DHT dht(DHTPIN, DHTTYPE); // Inițializați obiectul DHT

float temperatura;  // Variabila pentru temperatura
float umiditate;  // Variabila pentru umiditate

const int DIN_PIN = 2;
const int DIN2_PIN = 3;
int afisare, alarma = 3000;

unsigned long previousTime = 0;
const unsigned long interval = 60000; // Intervalul de timp în milisecunde (60 de secunde)
int i = 0;
int previousHour = -1;  // Ora anterioară afișată
bool alarmSet = false;  // Indicator pentru alarmă setată
bool buzzerOn = false;  // Indicator pentru starea buzzer-ului

TM1637Display display(CLK_PIN, DIO_PIN);

const uint8_t celsius[] = { SEG_A | SEG_B | SEG_F | SEG_G,  // Symbolul de grad
  SEG_A | SEG_D | SEG_E | SEG_F // C
};

void setup()
{
  display.setBrightness(7); // Setarea luminozității afișajului (0-7)
  pinMode(DIN_PIN, INPUT_PULLUP);
  pinMode(DIN2_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(ALARM_SET_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);  // Setează pinul buzzer-ului ca ieșire
  Serial.begin(9600);
  previousTime = millis();  // Salvează timpul de pornire
  dht.begin();
}

void loop()
{
  int value = digitalRead(DIN_PIN);
  int value2 = digitalRead(DIN2_PIN);
  int buttonState = digitalRead(BUTTON_PIN);
  int buttonState2 = digitalRead(BUTTON_PIN2);
  int alarmSetState = digitalRead(ALARM_SET_PIN);
  if (alarmSetState == LOW) dmn();
  else if (buttonState == LOW)
  {
    // Butonul 1 este apăsat, afișează temperatura
    temperatura = dht.readTemperature();  // Citirea temperaturii
    delay(250);
    display.showNumberDec(temperatura, false, 2, 0);
    display.setSegments(celsius, 2, 2);
  }
  else if (buttonState2 == LOW)
  {
    // Butonul 2 este apăsat, afișează umiditatea
    umiditate = dht.readHumidity(); // Citirea umidității
    delay(250);
    display.showNumberDecEx(umiditate);
  }
  else
  {
    // Niciun buton nu este apăsat, afișează ora
    unsigned long currentTime = millis(); // Obține timpul curent
    int currentHour = i / 100;  // Obține ora din valoarea i

    if (((currentTime - previousTime) % 1000) / 500 == 1) display.showNumberDecEx(i, 0b01000000, true);
    else display.showNumberDecEx(i, 0, true);

    if (value == 0)
    {
      i++;
      delay(200);
    }

    if (value2 == 0)
    {
      i += 100;
      delay(200);
    }

    if (i % 100 / 10 == 6)
    {
      i = (i - i % 100) + 100;
    }

    if (currentTime - previousTime >= interval)
    {
      i++;
      previousTime = currentTime;
    }

    if (i >= 2359)
    {
      i = 0;
    }

    if (alarma == i)
    {
      digitalWrite(BUZZER_PIN, HIGH);
      if (alarmSetState == LOW)
      {
        digitalWrite(BUZZER_PIN, LOW);
        alarma = 3000;
      }
    }
  }
}

void dmn()
{
  int confirm;

  do {
    if (afisare >= 2400) afisare = 0;
    int value = digitalRead(DIN_PIN);
    int value2 = digitalRead(DIN2_PIN);
    display.showNumberDecEx(afisare, 0b01000000, true); // 0b01000000 = 64 sau 0x40, puteți utiliza oricare dintre acestea.

    if (afisare % 100 / 10 == 6)
    {
      afisare = (afisare - afisare % 100) + 100;
    }

    if (value == LOW)
    {
      afisare++;
      delay(200);
    }

    if (value2 == LOW)
    {
      afisare += 100;
      delay(200);
    }

    alarma = afisare;
    confirm = digitalRead(ALARM_SET_PIN);
  } while (confirm == HIGH);

  loop();
}

Idk how to use this module, how can i make it memorise the time (i) and the alarm time (alarm) ?Thanks!

have a look at Real-Time-Clock-DS1302

Yes, but idk how to make it work on my code.. i need this project to be done tommorow. i want it to memorize the time and increment it

@miracolpro

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

You mean this topic?

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