Set time on alarm

Hello, I want the alarm to sound for 5 seconds but when im put delay on alarm function its not work. Where should i put delay func?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>          // library for LCD with I2C
#include <virtuabotixRTC.h>
#include <hd44780.h>
#include <DHT.h>
virtuabotixRTC myRTC(4, 3, 2);
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define DHTPIN 5    
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

#include <EEPROM.h>

int h1;
int t;
const int btnChange = 8;
const int btnOK = 9;
const int btnMode = 10;
const int buzzer = 12;

int alarmHour = 12;
int alarmMinutes = 0;
boolean hourMode = 0;
boolean alarmOn = 1;
int M = 12;
int D = 31;
int Y = 2020;
int day = 7;
int h = 23;
int m = 59;

int addrH[] = {0, 1, 2, 3, 4};
int addrM[] = {5, 6, 7, 8, 9};
int addrActive[] = {10, 11, 12, 13, 14};

int mode = 1;
int submode = 1;
int slot = 0;

byte clockChar[] = {B00000, B01110, B10101, B10101, B10111, B10001, B01110, B00000};
byte alarmChar[] = {B00100, B01110, B01110, B01110, B11111, B00000, B00100, B00000};
byte dateChar[] = {B11111, B00000, B01100, B01010, B01010, B01100, B00000, B11111};

void setup()  {

  pinMode(btnChange, INPUT_PULLUP);
  pinMode(btnOK, INPUT_PULLUP);
  pinMode(btnMode, INPUT_PULLUP);
  pinMode(buzzer, OUTPUT);
  
  dht.begin();
  lcd.init();
  lcd.begin(16,2);         // initialize the LCD
  lcd.backlight();    // Turn on the blacklight
  lcd.createChar(8, clockChar);
  lcd.createChar(9, alarmChar);
  lcd.createChar(10, dateChar);

  Serial.begin(9600);

}

///////////////////////////////////////////////////////////////////   void LOOP  ///////////////////////////////////////////

void loop()  {

    h1 = dht.readHumidity();
    t = dht.readTemperature();
  if (digitalRead(btnMode) == LOW) {
    mode++;
    Serial.println("btnMode press");
    if (mode == 4) {
      mode = 1;
    }
    submode = 0;
    lcd.clear();
    delay(200);
  }

  if (mode == 1) {
    clockDisplay();
    Serial.println(mode);
    //    delay(200);
  }

  if (mode == 2) {
    alarmMode();
    Serial.println(mode);
    //    delay(200);
  }

  if (mode == 3) {
    updateRTC();
    Serial.println(mode);
    //    delay(200);
  }
  if (mode == 0) {
    alarm();
    Serial.println(mode);
    EEPROM.write(addrActive[0], 0);
    //    delay(200);
  }
  for (int x = 0; x < 5; x++) {
    if (EEPROM.read(addrActive[x]) == 1) {
      Serial.print("alarm slot active: ");
      Serial.println(x);
      if ( myRTC.hours == EEPROM.read(addrH[x]) && myRTC.minutes == EEPROM.read(addrM[x])) {
        EEPROM.write(addrActive[x], 0);
        mode = 0;
      }
    }
  }
}

void alarm() {
  clockDisplay();
  lcd.noBacklight();
  digitalWrite(buzzer, HIGH);
  delay(300);
  lcd.backlight();
  digitalWrite(buzzer, LOW);
  delay(100);
  
}


///////////////////////////////////////////////////////////////////   CLOCK DISPLAY  ///////////////////////////////////////////

void clockDisplay() {
  myRTC.updateTime();
  //  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(t);
  lcd.print((char)223);
  lcd.print("C");
  
  lcd.setCursor(0, 1);
    lcd.print(h1);
    lcd.print("%");
  
  lcd.setCursor(3, 1);
  switch (myRTC.month) {
    case 1: lcd.print("Ock"); break;
    case 2: lcd.print("Sbt"); break;
    case 3: lcd.print("Mar"); break;
    case 4: lcd.print("Nis"); break;
    case 5: lcd.print("May"); break;
    case 6: lcd.print("Haz"); break;
    case 7: lcd.print("Tem"); break;
    case 8: lcd.print("Agu"); break;
    case 9: lcd.print("Eyl"); break;
    case 10: lcd.print("Eki"); break;
    case 11: lcd.print("Kas"); break;
    case 12: lcd.print("Ara"); break;
  }

  if (myRTC.dayofmonth < 10) {
    lcd.print(" ");
    lcd.print(myRTC.dayofmonth);
  }
  else{
    lcd.print(myRTC.dayofmonth);
  }
  lcd.print(",");
  lcd.print(myRTC.year);

  lcd.setCursor(13, 1);
  switch (myRTC.dayofweek) {
    case 1: lcd.print("Paz"); break;
    case 2: lcd.print("Pzt"); break;
    case 3: lcd.print("Sal"); break;
    case 4: lcd.print("Car"); break;
    case 5: lcd.print("Per"); break;
    case 6: lcd.print("Cum"); break;
    case 7: lcd.print("Cts"); break;
  }
  lcd.setCursor(5, 0);
  if ((myRTC.hours > 12 && myRTC.hours < 22) || (myRTC.hours > 0 && myRTC.hours < 10)) {
    lcd.print("0");
  }
  if (myRTC.hours == 0) {
    lcd.print("12");
  }
  else if (myRTC.hours <= 12) {
    lcd.print(myRTC.hours);
  }
  else {
    lcd.print(myRTC.hours - 12);
  }
  lcd.print(":");
  if (myRTC.minutes < 10) {
    lcd.print("0");
  }
  lcd.print(myRTC.minutes);
  lcd.print(":");
  if (myRTC.seconds < 10) {
    lcd.print("0");
  }
  lcd.print(myRTC.seconds);
  lcd.print(" ");
  if (myRTC.hours < 12) {
    lcd.print("OO");
  }
  else {
    lcd.print("OS");
  }
}

///////////////////////////////////////////////////////////////////   ALARM FUNCTION  ///////////////////////////////////////////
void alarmMode() {
  if (submode == 0) {
    lcd.setCursor(0, 0);
    lcd.write(1);
    lcd.setCursor(2, 0);
    lcd.print("Sec:");

    if (digitalRead(btnChange) == LOW) {
      slot++;
      if (slot > 4 ) {
        slot = 0;
      }
      delay(200);
    }
    lcd.setCursor(10, 0);
    lcd.print("Slot#");
    lcd.print(slot + 1);
    lcd.setCursor(0, 1);
    if (EEPROM.read(addrH[slot]) > 23) {
      EEPROM.write(addrH[slot], 0);
    }
    if (EEPROM.read(addrM[slot]) > 59) {
      EEPROM.write(addrM[slot], 0);
    }
    if (EEPROM.read(addrActive[slot]) > 1) {
      EEPROM.write(addrActive[slot], 0);
    }
    if ((EEPROM.read(addrH[slot]) > 12 && EEPROM.read(addrH[slot]) < 22) || (EEPROM.read(addrH[slot]) > 0 && EEPROM.read(addrH[slot]) < 10)) {
      lcd.print("0");
    }
    if (EEPROM.read(addrH[slot]) == 0) {
      lcd.print("12");
      alarmHour = 12;
    }
    else if (EEPROM.read(addrH[slot]) <= 12) {
      lcd.print(EEPROM.read(addrH[slot]));
      alarmHour = EEPROM.read(addrH[slot]);
    }
    else {
      lcd.print(EEPROM.read(addrH[slot]) - 12);
      alarmHour = EEPROM.read(addrH[slot]) - 12;
    }
    lcd.print(":");
    if (EEPROM.read(addrM[slot]) < 10) {
      lcd.print("0");
    }
    lcd.print(EEPROM.read(addrM[slot]));
    alarmMinutes = EEPROM.read(addrM[slot]);
    lcd.print(" ");
    if (EEPROM.read(addrH[slot]) < 12) {
      lcd.print("OO");
      hourMode = 0;
    }
    else {
      lcd.print("OS");
      hourMode = 1;
    }
    lcd.print(" ");
    if (EEPROM.read(addrActive[slot]) == 1) {
      lcd.print("Acik ");
    }
    else {
      lcd.print("Kapali");
    }
  }
  else {
    lcd.setCursor(0, 0);
    lcd.write(1);
    lcd.setCursor(2, 0);
    lcd.print("Ayarla|");
    lcd.setCursor(0, 1);
    lcd.print("Alarm ||");
    lcd.setCursor(9, 1);
    if (alarmHour < 10) {
      lcd.print("0");
      lcd.setCursor(10, 1);
    }
    lcd.print(alarmHour);
    lcd.setCursor(11, 1);
    lcd.print(":");
    lcd.setCursor(12, 1);
    if (alarmMinutes < 10) {
      lcd.print("0");
      lcd.setCursor(13, 1);
    }
    lcd.print(alarmMinutes);
    if (hourMode == 0) {
      lcd.print("OO");
    }
    else {
      lcd.print("OS");
    }
  }
  //  lcd.print(alarmMinutes);
  if (digitalRead(btnOK) == LOW) {
    submode++;
    if (submode > 4) {
      submode = 0;
    }
    delay(200);
    lcd.clear();
  }
  if (submode == 1) {
    lcd.setCursor(9, 0);
    lcd.write(1);
    lcd.write(1);
    if (digitalRead(btnChange) == LOW) {
      alarmHour++;
      if (alarmHour > 12) {
        alarmHour = 1;
      }
      delay(500);
    }
  }
  if (submode == 2) {
    lcd.setCursor(12, 0);
    lcd.write(1);
    lcd.write(1);
    if (digitalRead(btnChange) == LOW) {
      alarmMinutes++;
      if (alarmMinutes >= 60) {
        alarmMinutes = 0;
      }
      delay(500);
    }
  }
  if (submode == 3) {
    lcd.setCursor(14, 0);
    lcd.write(1);
    lcd.write(1);
    if (digitalRead(btnChange) == LOW) {
      hourMode = !hourMode;
      delay(500);
    }
  }
  while (submode == 4) {
    lcd.setCursor(0, 0);
    lcd.print("Alarm Aktif?");
    lcd.setCursor(0, 1);
    lcd.print("Slot #");
    lcd.print(slot + 1);
    lcd.print("  ");
    if(alarmOn==1){
      lcd.print("Acik ");
    }
    else{
      lcd.print("Kapali");
    }
    if (digitalRead(btnChange) == LOW) {
      alarmOn = !alarmOn;
      delay(200);
    }

    if (digitalRead(btnOK) == LOW && alarmOn == 1) {
      lcd.clear();
      lcd.setCursor(0, 1);
      lcd.print("Ayarlaniyor.");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      delay(200);
      lcd.print(".");
      if (hourMode == 1 && alarmHour != 12) {
        EEPROM.write(addrH[slot], alarmHour + 12);
      }
      else if (hourMode == 0 && alarmHour == 12) {
        EEPROM.write(addrH[slot], 0);
      }
      else {
        EEPROM.write(addrH[slot], alarmHour);
      }

      EEPROM.write(addrM[slot], alarmMinutes);
      EEPROM.write(addrActive[slot], 1);
      submode = 0;
      mode = 1;
      lcd.clear();
    }
    if (digitalRead(btnOK) == LOW && alarmOn == 0)  {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Alarm Slot#");
      lcd.print(slot + 1);
      lcd.setCursor(0, 1);
      lcd.print(" OFF      ");
      
      EEPROM.write(addrActive[slot], 0);
      submode = 0;
      mode = 1;
      delay(600);

      delay(2000);
      lcd.clear();
    }
  }
}

///////////////////////////////////////////////////////////////////   SETTING DATE & TIME FUNCTION  ///////////////////////////////////////////

void updateRTC() {
  lcd.setCursor(0, 0);
  lcd.write(2);
  if (digitalRead(btnOK) == LOW) {
    submode++;
    if (submode > 7) {
      submode = 0;
    }
    delay(200);
    lcd.clear();
  }

  if (submode == 0) {
    lcd.setCursor(2, 0);
    lcd.print(" Tarih/Zaman");
    lcd.setCursor(0, 1);
    lcd.print("icin kirmizi tus");
  }
  if (submode == 1) {
    lcd.setCursor(2, 0);
    lcd.print("Ay Ayarla");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      M++;
      if (M == 13) {
        M = 1;
      }
      delay(200);
    }
    switch (M) {
      case 1: lcd.print("Ocak     ");
        break;
      case 2: lcd.print("Subat    ");
        break;
      case 3: lcd.print("Mart       ");
        break;
      case 4: lcd.print("Nisan       ");
        break;
      case 5: lcd.print("Mayis         ");
        break;
      case 6: lcd.print("Haziran        ");
        break;
      case 7: lcd.print("Temmuz        ");
        break;
      case 8: lcd.print("Agustos      ");
        break;
      case 9: lcd.print("Eylul   ");
        break;
      case 10: lcd.print("Ekim     ");
        break;
      case 11: lcd.print("Kasim    ");
        break;
      case 12: lcd.print("Aralik    ");
        break;
    }
  }
  if (submode == 2) {
    lcd.setCursor(2, 0);
    lcd.print("Gun Ayarla");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      D++;
      if (D == 32) {
        D = 1;
      }
      delay(200);
    }
    lcd.print(D);
    lcd.print(" ");
  }

  if (submode == 3) {
    lcd.setCursor(2, 0);
    lcd.print("Yil Ayarla");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      Y++;
      if (Y == 2099) {
        Y = 2000;
      }
      delay(200);
    }
    lcd.print(Y);
  }
  if (submode == 4) {
    lcd.setCursor(2, 0);
    lcd.print("Haftanin hangi gunu?");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      day++;
      if (day == 8) {
        day = 1;
      }
      delay(200);
    }
    switch (day) {
      case 1: lcd.print("Pazar      ");
        break;
      case 2: lcd.print("Pazartesi      ");
        break;
      case 3: lcd.print("Sali     ");
        break;
      case 4: lcd.print("Carsamba   ");
        break;
      case 5: lcd.print("Persembe    ");
        break;
      case 6: lcd.print("Cuma      ");
        break;
      case 7: lcd.print("Cumartesi    ");
        break;
    }
  }
  if (submode == 5) {
    lcd.setCursor(2, 0);
    lcd.print("Saat ayarla");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      h++;
      if (h == 24) {
        h = 0;
      }
      delay(200);
    }
    if (h > 12) {
      lcd.print(h - 12);
      lcd.print(" ");
    }
    else if (h == 0) {
      lcd.print("12");
      lcd.print(" ");
    }
    else {
      lcd.print(h);
      lcd.print(" ");

    }
    if (h >= 12) {
      lcd.setCursor(9, 1);
      lcd.print("OS");
    }
    else {
      lcd.setCursor(9, 1);
      lcd.print("OO");
    }

  }
  if (submode == 6) {
    lcd.setCursor(2, 0);
    lcd.print("Dakika Ayarla");
    lcd.setCursor(0, 1);
    lcd.print("--> ");
    if (digitalRead(btnChange) == LOW) {
      m++;
      if (m == 60) {
        m = 0;
      }
      delay(200);
    }
    lcd.print(m);
    lcd.print(" ");
  }
  if (submode == 7) {
    lcd.setCursor(2, 0);
    lcd.print("Tarih & Zaman");
    lcd.setCursor(0, 1);
    lcd.print("Guncelleniyor");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    delay(200);
    lcd.print(".");
    myRTC.setDS1302Time(00, m, h, day, D, M, Y);
    delay(200);
    lcd.clear();
    mode = 1;
  }
}

Can I ask why you think this is a bootloader issue?
Have I missed something?

changed

Does anyone know ?

Ideally, you shouldn't put a delay anywhere - it will stop the processor doing anything useful for the duration of the delay.

But its not working. Alarm not shutting off.

Demonstration code for several things at the same time

Using millis() for timing. A beginners guide

For example, change your alarm() function to this one:

void alarm() {
  clockDisplay();
  unsigned long timer = millis();
  while (millis() - timer < 5000) {
    lcd.noBacklight();
    digitalWrite(buzzer, HIGH);
    delay(250);
    lcd.backlight();
    digitalWrite(buzzer, LOW);
    delay(250);
  }
}

Or this other one:

void alarm() {
  clockDisplay();
  for (int n = 0; n < 9; n++) {
    lcd.noBacklight();
    digitalWrite(buzzer, HIGH);
    delay(300);
    lcd.backlight();
    digitalWrite(buzzer, LOW);
    delay(300);
  }
}

And if you think a little you will get some more
Greetings.

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