Alarm SET with one button and a buzzer

Hello!! These days i started a new project, a smart clock.. you have 4 buttons, one for setting the hour, one for setting the minutes, one for showing the temperature and one for showing the humidity. I want to make an alarm using a button and a buzzer but i cant figure it out..I want that when you press the button the clock to show 00:00 and you have to set the hour that you want and press the button and then you have to set the clock back to its hour.. Can you guys help me? Thanks

Here is my code ( i know its a litle messy):

#include <TM1637Display.h>
#include <DHT.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 1          // Pinul conectat la butonul pentru selectarea modului
#define BUTTON_PIN2 0         // 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;


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 (buttonState == LOW) {
    // Butonul 1 este apăsat, afișează temperatura
    temperatura = dht.readTemperature();  // Citirea temperaturii
    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
    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

    display.showNumberDecEx(i, 0b01000000, true); // 0b01000000 = 64 sau 0x40, puteți utiliza oricare dintre acestea.

    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;
    }

                   
       
  }
}

Thanks!

You may have discovered this already, but you need to include the .cpp in the library folder or in the sketch folder.

You should change the following two button pins because 0 and 1 will interfere with communications on the USB cable. (5 and 6 are available).

#define BUTTON_PIN 1          // Pinul conectat la butonul pentru selectarea modului
#define BUTTON_PIN2 0         // Pinul conectat la butonul pentru afișarea umidității

The code counts minutes.
The humidity button works.
The temperature button works.

I imagine the time is shown when no buttons are pressed, but how will the hour be set with one (the third) button and the minute be set with one (the fourth) button... and then how is an alarm set?

Here is a simulation to get you started. (code is almost same as original - pin 5, pin 6 in the place of pin 0, pin 1)

I don't know how good my idea is. I want when I press buttons 5 and 6 to take me to the alarm setting setup, then using buttons 2 and 3 (with which I set the time) to navigate to the desired time and then press button 4 to I confirm that the displayed time is the one at which I want the alarm to sound. After I have set the alarm, the real time will be shown back on the display.

i solved it. thanks!

Very good! : )

It is okay not to know... you will discover all you need. Every idea is helped when you draw it with paper and pencil. It lets you share your idea without needing to describe it... and sometimes you can see if you need more (or less) in your project without needing help. Next project, make a drawing. Best wishes.

It worked. initially i made that when temp and humidity buttons are pressed, you will be redirected to a void where you set the alarm time. Now i want to do the setup by pressing only a button...

#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 alarmSetState;

  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;
    alarmSetState = digitalRead(ALARM_SET_PIN);
  } while (alarmSetState == HIGH);

  loop();
}

It kinda works but its stuck in the do-while from the void dmn.. Could you help? :slight_smile:

You have ALARM_SET_PIN pin tied high when not pulled low... Seems like that never happens, so the next two lines keep the while going...

so what i have to change?

You need some code to read the button tthat sets ALARM_SET_PIN to LOW to leave the while

No.. now it only shows the alarm set-up when i keep pressing the button

You asked how to make your code leave the while() so "yes" that worked. What is the next step that you want?

In that do while you set the alarm. That button is like a submit button.

Okay. Save that information when the button is pushed.

The button itself will return to a high state after the button is release due to the INPUT_PULLUP, and should return you to the while loop.

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