Coding help on a project I am working on!

Dear users :),

I am currently working on a project that consists of sound recognition and a RTC (clock) that displays on an LCD screen. Now I have the codes for the sound recognition and RTC separately. The problem now lies on trying to combine both the codes to display on the LCD screen so that the LCD shows the time normally, and would display the sound recognitions when needed, going back to showing the time after displaying the sound recognition part.

This is my sound recognition code

#include <LiquidCrystal_I2C.h>
#include<Wire.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);
LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines

int voice_recogn=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // 통신 속도 9600bps로 PC와 시리얼 통신 시작
  mySerial.begin(9600); // 통신 속도 9600bps로 모듈과 시리얼 통신 시작
  lcd.init();
  lcd.backlight();
  mySerial.write(0xAA); // compact mode 사용
  mySerial.write(0x37);
  delay(1000);
  mySerial.write(0xAA); // 그룹1 음성 명령어 imported
  mySerial.write(0x21);
  Serial.println("The settings are complete");
}

void loop() {
  // put your main code here, to run repeatedly:
  while(mySerial.available())
  {
    Serial.println("voice input");//mySerial 이 사용가능하면 voice input 출력 ;이후부터 소리인식됨
    voice_recogn=mySerial.read();
    //이부분에 if 추가? (음성이 인식되면 글자 출력, 아니면 시계 기능 계속 / setup으로 돌아가게끔 스위치 추가?)
    switch(voice_recogn)
  {
    case 0x11:
    lcd.setCursor(0,0);  
    lcd.print("Announcement");
    Serial.println("안내방송 이미지 출력");
    break;
    case 0x12:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x13:
    lcd.setCursor(0,0);
    lcd.print("____Bell____"); 
    Serial.println("Image of Bell");
    break;
    case 0x14:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("Image of Siren");
    break;
    case 0x15:
    lcd.setCursor(0,0);
    lcd.print("Announcement"); 
    Serial.println("Image of announcement");
    break;
   } 
  }
}

and this is my RTC (clock) code

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68
/*
   function to update RTC time using user input
*/
void updateRTC()
{
  
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}
void setup()
{
  Serial.begin(9600); // initialize serial

  lcd.init();       // initialize lcd
  lcd.backlight();  // switch-on lcd backlight

  rtc.begin();       // initialize rtc
}
void loop()
{
  updateLCD();  // update LCD text

  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'u') updateRTC();  // update RTC time
  }
}

So I have tried to combine the two codes like this:

#include <LiquidCrystal_I2C.h>
#include<Wire.h>
#include <SoftwareSerial.h>
#include <RTClib.h>                 // for RTC
LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68
SoftwareSerial mySerial(2, 3);      // LCD announcement
int voice_recogn = 0;               // LCD announcement
void updateRTC()

// All LCD TIME coding from here
{

  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"
                          };
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
                                    "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
                                   };

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}


// LCD time and announcement

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // 통신 속도 9600bps로 PC와 시리얼 통신 시작
  mySerial.begin(9600); // 통신 속도 9600bps로 모듈과 시리얼 통신 시작
  lcd.init();
  lcd.backlight();
  mySerial.write(0xAA); // compact mode 사용
  mySerial.write(0x37);
  delay(1000);
  mySerial.write(0xAA); // 그룹1 음성 명령어 imported
  mySerial.write(0x21);
  Serial.println("The settings are complete");
  rtc.begin();       // initialize rtc
}



void loop(){
// put your main code here, to run repeatedly:
updateLCD();  // update LCD text


{
  if (Serial.available()) {
  char input = Serial.read();
  if (input == 'u') updateRTC();
}

  Serial.println("voice input");//mySerial 이 사용가능하면 voice input 출력 ;이후부터 소리인식됨
  voice_recogn = mySerial.read();
  //이부분에 if 추가? (음성이 인식되면 글자 출력, 아니면 시계 기능 계속 / setup으로 돌아가게끔 스위치 추가?)
  switch (voice_recogn)
  {
    case 0x11:
      lcd.setCursor(0, 0);
      lcd.print("Announcement");
      Serial.println("안내방송 이미지 출력");
      break;
    case 0x12:
      lcd.setCursor(0, 0);
      lcd.print("___Siren____");
      Serial.println("사이렌 이미지 출력");
      break;
    case 0x13:
      lcd.setCursor(0, 0);
      lcd.print("____Bell____");
      Serial.println("초인종 모양 이미지 출력");
      break;
    case 0x14:
      lcd.setCursor(0, 0);
      lcd.print("___Siren____");
      Serial.println("사이렌 이미지 출력");
      break;
    case 0x15:
      lcd.setCursor(0, 0);
      lcd.print("Announcement");
      Serial.println("안내방송 이미지 출력");
      break;
  }
}
}

I feel like i can just copy and paste all the codes just before void loop(). I just am not really sure how to integrate the two void loop() codes together.
Thank you in advance for your help!

Please post your best effort at combining the two sketches to do what you want

Hi thank you for your reply. I have just updated the post with the combinations of the sketches.

Does it compile ?
If it compiles then does it run and what does it do ?

    if (Serial.available())
    {
      char input = Serial.read();
      if (input == 'u') updateRTC();
    }

This code is redundant as you update the LCD each time through loop() regardless

Hi yes it does verify the code, but when I upload it to the board, it just shows the time but doesn't do the sound recognition bit

    voice_recogn = mySerial.read();

voice_recogn is an int. What exactly are you entering that might match 0x11, 0x12, 0x13, 0x13 or 0x15 in the switch/case ?

Oh right so basically the 0x11, 0x12, 0x13 that you're seeing is the sound recognition bit. So we have pre recorded the sounds using a voice recognition module. for simple explanation, one group consists of 5 pre recorded sounds. Which is why we have those 5 hex codes.

Hi so I have tried changing the codes around so I got it to work but it shows a blank screen at the start, once it detects one of the recorded sounds, it then shows the date time and the sound recognition all together.

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68
SoftwareSerial mySerial(2, 3);

int voice_recogn=0;

void updateRTC()
{
  
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}

void setup() {
  Serial.begin(9600); // 통신 속도 9600bps로 PC와 시리얼 통신 시작
  mySerial.begin(9600); // 통신 속도 9600bps로 모듈과 시리얼 통신 시작
  lcd.init();
  lcd.backlight();
  rtc.begin();       // initialize rtc
  mySerial.write(0xAA); // compact mode 사용
  mySerial.write(0x37);
  delay(1000);
  mySerial.write(0xAA); // 그룹1 음성 명령어 imported
  mySerial.write(0x21);
  Serial.println("The settings are complete");
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  while(mySerial.available())
  { 
    updateLCD();
    Serial.println("voice input");//mySerial 이 사용가능하면 voice input 출력 ;이후부터 소리인식됨
    if(voice_recogn=mySerial.read()) {
    //이부분에 if 추가? (음성이 인식되면 글자 출력, 아니면 시계 기능 계속 / setup으로 돌아가게끔 스위치 추가?)
    switch(voice_recogn)
  {
    case 0x11:
    lcd.setCursor(0,0);  
    lcd.print("Announcement");
    Serial.println("안내방송 이미지 출력");
    break;
    case 0x12:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x13:
    lcd.setCursor(0,0);
    lcd.print("____Bell____"); 
    Serial.println("초인종 모양 이미지 출력");
    break;
    case 0x14:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x15:
    lcd.setCursor(0,0);
    lcd.print("Announcement"); 
    Serial.println("안내방송 이미지 출력");
    break;
   }
    }
    else(voice_recogn=!mySerial.read());{
        if (Serial.available()) {
        char input = Serial.read();
        if (input == 'u') updateRTC();  // update RTC time
      
    }
  }
}
}

So write something to the screen in setup() if you don't want it to be blank

Where is the serial input coming from ?

Thanks for all the replies :slight_smile: so I've added updateLCD to the setup and now it doesn't show a blank screen initially. But I have realised another problem where the time is just frozen and not 'ticking'. The time is going but it only shows the actual time whenever the screen is 'updated' so when I play one of the recorded songs it refreshes the screen and shows the time and it freezes again.

regarding your serial input question, I am not sure what that means but my serial monitor ideally is supposed to show both setting the time and date and the sound recognition results but the time and date function isn't appearing.

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68
SoftwareSerial mySerial(2, 3);

int voice_recogn=0;

void updateRTC()
{
  
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}

void setup() {
  Serial.begin(9600); // 통신 속도 9600bps로 PC와 시리얼 통신 시작
  mySerial.begin(9600); // 통신 속도 9600bps로 모듈과 시리얼 통신 시작
  lcd.init();
  lcd.backlight();
  rtc.begin();       // initialize rtc
  mySerial.write(0xAA); // compact mode 사용
  mySerial.write(0x37);
  delay(1000);
  mySerial.write(0xAA); // 그룹1 음성 명령어 imported
  mySerial.write(0x21);
  Serial.println("The settings are complete");
  // put your setup code here, to run once:
  updateLCD();
}

void loop() {
  // put your main code here, to run repeatedly:
  
  while(mySerial.available())
  { 
    
    updateLCD();
    Serial.println("voice input");//mySerial 이 사용가능하면 voice input 출력 ;이후부터 소리인식됨
    if(voice_recogn=mySerial.read()) {
    //이부분에 if 추가? (음성이 인식되면 글자 출력, 아니면 시계 기능 계속 / setup으로 돌아가게끔 스위치 추가?)
    switch(voice_recogn)
  {
    case 0x11:
    lcd.setCursor(0,0);  
    lcd.print("Announcement");
    Serial.println("안내방송 이미지 출력");
    break;
    case 0x12:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x13:
    lcd.setCursor(0,0);
    lcd.print("____Bell____"); 
    Serial.println("초인종 모양 이미지 출력");
    break;
    case 0x14:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x15:
    lcd.setCursor(0,0);
    lcd.print("Announcement"); 
    Serial.println("안내방송 이미지 출력");
    break;
   }
    }
    else(voice_recogn=!mySerial.read());{
        if (Serial.available()) {
        char input = Serial.read();
        if (input == 'u') updateRTC();  // update RTC time
      
    }
  }
}
}

Who or what is sending the data to the serial port ?

You can display the current time continuously by calling updateLCD() in loop() but it would be better to call it only when the second changes as doing so more often is a waste of time

im using a usb cable if that answers your question?
Could you help me with the loop? so I really dont know where to place updateLCD(); code because if I place updateLCD(); right after my void loop() then sound recognition doesn't work, but if I put updateLCD(); after while( serial.available()) time freezes again :frowning:

#include <Wire.h>                   // for I2C communication
#include <LiquidCrystal_I2C.h>      // for LCD
#include <RTClib.h>                 // for RTC
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
RTC_DS3231 rtc;                     // create rtc for the DS3231 RTC module, address is fixed at 0x68
SoftwareSerial mySerial(2, 3);

int voice_recogn=0;

void updateRTC()
{
  
  lcd.clear();  // clear LCD display
  lcd.setCursor(0, 0);
  lcd.print("Edit Mode...");

  // ask user to enter new date and time
  const char txt[6][15] = { "year [4-digit]", "month [1~12]", "day [1~31]",
                            "hours [0~23]", "minutes [0~59]", "seconds [0~59]"};
  String str = "";
  long newDate[6];

  while (Serial.available()) {
    Serial.read();  // clear serial buffer
  }

  for (int i = 0; i < 6; i++) {

    Serial.print("Enter ");
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
      ; // wait for user input
    }

    str = Serial.readString();  // read user input
    newDate[i] = str.toInt();   // convert user input to number and save to array

    Serial.println(newDate[i]); // show user input
  }

  // update RTC
  rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
  Serial.println("RTC Updated!");
}
/*
   function to update LCD text
*/
void updateLCD()
{

  /*
     create array to convert digit days to words:

     0 = Sunday    |   4 = Thursday
     1 = Monday    |   5 = Friday
     2 = Tuesday   |   6 = Saturday
     3 = Wednesday |
  */
  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

  /*
     create array to convert digit months to words:

     0 = [no use]  |
     1 = January   |   6 = June
     2 = February  |   7 = July
     3 = March     |   8 = August
     4 = April     |   9 = September
     5 = May       |   10 = October
     6 = June      |   11 = November
     7 = July      |   12 = December
  */
  const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  // get time and date from RTC and save in variables
  DateTime rtcTime = rtc.now();

  int ss = rtcTime.second();
  int mm = rtcTime.minute();
  int hh = rtcTime.twelveHour();
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();

  // move LCD cursor to upper-left position
  lcd.setCursor(0, 0);

  // print date in dd-MMM-yyyy format and day of week
  if (dd < 10) lcd.print("0");  // add preceeding '0' if number is less than 10
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[MM]);
  lcd.print("-");
  lcd.print(yyyy);

  lcd.print("  ");
  lcd.print(dayInWords[DD]);

  // move LCD cursor to lower-left position
  lcd.setCursor(0, 1);

  // print time in 12H format
  if (hh < 10) lcd.print("0");
  lcd.print(hh);
  lcd.print(':');

  if (mm < 10) lcd.print("0");
  lcd.print(mm);
  lcd.print(':');

  if (ss < 10) lcd.print("0");
  lcd.print(ss);

  if (rtcTime.isPM()) lcd.print(" PM"); // print AM/PM indication
  else lcd.print(" AM");
}

void setup() {
  Serial.begin(9600); // 통신 속도 9600bps로 PC와 시리얼 통신 시작
  mySerial.begin(9600); // 통신 속도 9600bps로 모듈과 시리얼 통신 시작
  lcd.init();
  lcd.backlight();
  rtc.begin();       // initialize rtc
  mySerial.write(0xAA); // compact mode 사용
  mySerial.write(0x37);
  delay(1000);
  mySerial.write(0xAA); // 그룹1 음성 명령어 imported
  mySerial.write(0x21);
  Serial.println("The settings are complete");
  // put your setup code here, to run once:
  updateLCD();
}

void loop() {
  while(mySerial.available())
  {
    Serial.println("voice input");//mySerial 이 사용가능하면 voice input 출력 ;이후부터 소리인식됨
    voice_recogn=mySerial.read();
    switch(voice_recogn)
  {
    updateLCD();
    case 0x11:
    lcd.setCursor(0,0);  
    lcd.print("Announcement");
    Serial.println("안내방송 이미지 출력");
    break;
    case 0x12:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x13:
    lcd.setCursor(0,0);
    lcd.print("____Bell____"); 
    Serial.println("초인종 모양 이미지 출력");
    break;
    case 0x14:
    lcd.setCursor(0,0);
    lcd.print("___Siren____");
    Serial.println("사이렌 이미지 출력");
    break;
    case 0x15:
    lcd.setCursor(0,0);
    lcd.print("Announcement"); 
    Serial.println("안내방송 이미지 출력");
    break;
   } 
  }
}

No, I meant what device is sending the data to Serial. Is it being typed in by a human or is it being sent by some electronic device

Note that this

    if (voice_recogn = mySerial.read())

probably does not do what you think, although it may work. What do you think it does ?

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