How to read and display struct content rtc

Have been searching for a tutorial on how to retrieve a name from a struct on a person's birthday using a rtc DS1307 as a reference.
Have found many say the DS1307 is not a good choice but it is what I have at the moment to work with.
Seems to be plenty of "clock" and "single alarm" examples which are fine but does not include any struct or array reference.
Any pointers to a tutorial or examples are appreciated.
Found some reference to begin but quickly left me behind due to very little commenting or explanation, basically over my head.
The "button" is simply put in there to test code as it stands.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte button = 9;
byte buttonState;

//****************************** copied from other code on the inet here down

char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

char monthOfYear [13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEPT", "OCT", "NOV", "DEC"};


struct People {
  char name[10];
  byte day;
  byte month;
};

const People birthdays[] PROGMEM = {
  {"OPO", 21, 9},      // my people name,day, month
  {"MUM", 14, 4},
  {"NAD", 20, 1},
  {"TON", 1, 6},
  {"TOOSAFE", 12, 5},
  {"JACKPOT", 7, 3},
  {"SAWITA", 3, 6},
  {"JEFF", 25, 2},
  {"PHI", 1, 1}

};

People Person;
//******************************* copied from other code on the inet here up

void setup ()
{
  pinMode(button, INPUT_PULLUP);
  Serial.begin(9600);
  lcd.init ();  // initialize the lcd
  lcd.backlight();//To Power ON the back light
  if (! rtc.begin())
  {
    lcd.print("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    lcd.print("RTC is NOT running!");
  }
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// to set the time manually
}



void loop ()
{
  buttonState = digitalRead(button); //just to test
  if (buttonState == HIGH) {         // "    "   "
    Clock();
  }
  else {                             // "    "    "
    /*code here to detect if a birthday has arrived and jump to
      bdayDisplay(); ....... alarm buzz also every so often with a mute switch
      or maybe just flash the display....
    */
    bdayDisplay();

  }

}


void Clock() {


  // print bottom line on 16 x 2 display
  DateTime now = rtc.now();
  lcd.setCursor(1, 1);
  lcd.print("TIME");
  lcd.print(" ");
  int thaiHour = now.hour() - 3;
  if (now.hour() - 3 < 10) {
    lcd.print("0");
  }
  lcd.print(thaiHour);
  lcd.print(':');
  if (now.minute() < 10) {
    lcd.print("0");
  }
  lcd.print(now.minute());
  lcd.print(':');
  if (now.second() < 10) {
    lcd.print("0");
  }
  lcd.print(now.second());
  lcd.print("  ");

  //print top line on 16 x 2 display
  lcd.setCursor(1, 0);
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  lcd.print("  ");
  lcd.print(now.day());
  lcd.print('/');
  lcd.print(now.month());
  lcd.print('/');
  int thaiYear = now.year() + 543;
  lcd.print(thaiYear);
  lcd.print("  ");

}

void bdayDisplay() {
  lcd.setCursor(0, 0);
  lcd.print (" HAPPY BIRTHDAY ");
  lcd.setCursor(0, 1);
  lcd.print (" ---- OPO ----- ");

}

Hello,

Use a for loop to compare every birthdays day and month with the actual day and month

Here is an example PtcZhO - Online C++ Compiler & Debugging Tool - Ideone.com

Something along these lines...

struct People 
{
  char name[10];
  byte day;
  byte month;
};

const People birthdays[] = 
{
  {"OPO", 21, 9},      
  {"MUM", 14, 4},
  {"NAD", 20, 1},
  {"TON", 1, 6},
  {"TOOSAFE", 12, 5},
  {"JACKPOT", 7, 3},
  {"SAWITA", 3, 6},
  {"JEFF", 25, 2},
  {"PHI", 1, 1}

};

void setup()
{

  Serial.begin(115200);

  byte month = 5;  // Get this form the RTC
  byte day   = 12; // Get this from the RTC

  for (uint8_t x = 0; x < sizeof(birthdays) / sizeof(birthdays[0]); x++)
  {
    Serial.print("Checking ");
    Serial.println(birthdays[x].name);

    if (birthdays[x].month == month &&
        birthdays[x].day   == day)
    {
      Serial.print("Happy Birthday ");
      Serial.println(birthdays[x].name);
    }
  }
}

void loop()
{

}

Thankyou...sorted in a fashion, may not be all that good looking but it works.

You may want to take a peek and give some constructive criticism .

One thing that has me though, when program displays a certain birthday, it follows the 4 second delay which is intended, then when it switches to the clock, it remains for 8 seconds even though I put delay 1000 at the end of the clock function.
Doesn't bother me as it is just fine, I was just curious as to why.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte scanButton = 9;
byte buttonState;

//****************************** copied from other code on the inet here down

char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

char monthOfYear [13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEPT", "OCT", "NOV", "DEC"};


struct People {        // set up the struct
  char name[10];
  byte day;
  byte month;
};

const People birthdays[] = {
  {"OPO", 21, 9},      // my people name, day and month of birthday
  {"MUM", 14, 4},
  {"NAD", 20, 1},
  {"TON", 1, 6},
  {"TOOSAFE", 12, 5},
  {"JACKPOT", 7, 3},
  {"SAWITA", 3, 6},
  {"JEFF", 25, 2},
  {"PHI", 1,1}

};


//******************************* copied from other code on the inet here up

void setup ()
{
  Serial.begin(9600);             // for serial monitor
  pinMode(scanButton, INPUT_PULLUP);

  lcd.init ();                  // initialize the lcd
  lcd.backlight();              // to power ON the back light
  if (! rtc.begin())            // real time clock check
  {
    lcd.print("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    lcd.print("RTC is NOT running!");
  }
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  //rtc.adjust(DateTime(2022, 6, 6,));// to set the time manually




}




void loop() {
  buttonState = digitalRead(scanButton); // check if scan button changed
  if (buttonState == LOW) {
    scanBirthdays();
  }

  DateTime now = rtc.now();      // check rtc

  // Serial.print("The year now is  ");
  // Serial.println(now.year());
  // Serial.print("The month now is  ");
  // Serial.println(now.month());
  // Serial.print("The day now is "  );
  // Serial.println(now.day());

  for (uint8_t x = 0; x < sizeof (birthdays) / sizeof(birthdays[0]); x++) {  // loop through list
    //Serial.print ("Checking ");
    //Serial.println(birthdays[x].name);

    if ( birthdays[x].month == now.month() &&
         birthdays[x].day == now.day()) {         // see if any birthdays match todays day and month
      //Serial.print("Happy Birthday ");
      //Serial.println(birthdays[x].name);
      lcd.setCursor(0, 0);                        //if they do then show on the LCD display
      lcd.print(" HAPPY BIRTHDAY ");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(6, 1);
      lcd.print(birthdays[x].name);
      lcd.print("    ");
      delay(4000);                                 // hold the happy birthday message 4 seconds
    }



    else {
      Clock();                                     // jump to display clock
    }

  }

}


void Clock() {
  // print bottom line on 16 x 2 display
  DateTime now = rtc.now();                // check time on real time clock
  lcd.setCursor(1, 1);                     // display on LCD
  lcd.print("TIME");
  lcd.print(" ");
  int thaiHour = now.hour() - 3;           //Thai time 3 hours behind here
  if (now.hour() - 3 < 10) {
    lcd.print("0");
  }
  lcd.print(thaiHour);
  lcd.print(':');
  if (now.minute() < 10) {                 // "0" leading if less than 10
    lcd.print("0");
  }
  lcd.print(now.minute());
  lcd.print(':');
  if (now.second() < 10) {
    lcd.print("0");
  }
  lcd.print(now.second());
  lcd.print("  ");

  //print top line on 16 x 2 display
  lcd.setCursor(1, 0);
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  lcd.print("  ");
  lcd.print(now.day());
  lcd.print('/');
  lcd.print(now.month());
  lcd.print('/');
  int thaiYear = now.year() + 543;         // Thai year = +543 years
  lcd.print(thaiYear);
  lcd.print("  ");
  delay(1000);

}

void scanBirthdays(){
  
  }

You are calling the Clock() routine inside the for loop, so it gets called for every entry in birthdays[].

If you move the call outside of the loop, it will just get called once the birthdays (if any today) have been displayed.

Finally managed to get it all together and working, all-be-it in a raggedy fashion.
Realise there are improvements that can be made e.g. the LED flashing that blocks and the lines of "if's" in the scan (not sure how to fix that to display monthinWords ) will work on those at a future time.
Thanks so much for the guidance, much appreciated. Cheers Jeff

/* Birthday Clock uses DS1307 RTC ....maybe replace later with DS3231 for better time keeping but seems ok for now
 *  Arduino pro mini with LEDs on pin 8 and the scan switch on pin 9
 *  I2C to 16 x 2 LCD display and to the rtc
 *  Can add extra birthdays if required in the "People birthdays[]" ..order is their name then the day then month of their birthday
 *  In the "scan" there probably is a better way to look and the month number in People birthdays[] but I used simple "if"
 *  So the program runs the "clock" showing current date on the top line and time on the bottom line
 *  When a birthday comes up, it switches over to print "Happy Birthday" top line and their name on the second line
 *  The LEDs flash at the beginning and end of this section and then goes back to the clock display for approx 8 seconds.
 *  If the "scan" button " is pressed and held for a few seconds, the display will run through all the names and their day and month of their birthdate.
 *  commented out serial prints were used during testing
 *  rtc adjust can be un-commented for adjusting the time etc. either manually as I did for Thailand or automatically for your local time via PC
 *  So uncomment whichever, adjust and upload, then comment out and reload again (with a battery in the rtc naturally)
 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte scanButton = 9;
byte buttonState;
byte ledPin = 8;
//int ledState = 0;
const long interval (200);



char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

char monthOfYear [13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEPT", "OCT", "NOV", "DEC"};


struct People {        // set up the struct
  char name[10];
  byte day;
  byte month;
};

const People birthdays[] = {
  {"OPO", 21, 9},      // my people name, day and month of birthday
  {"MUM", 14, 4},
  {"NAD", 20, 1},
  {"TON", 1, 6},
  {"TOOSAFE", 12, 5},
  {"JACKPOT", 7, 3},
  {"SAWITA", 3, 6},
  {"JEFF", 25, 2}
  
};




void setup ()
{
  Serial.begin(9600);             // for serial monitor
  pinMode(scanButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  lcd.init ();                  // initialize the lcd
  lcd.backlight();              // to power ON the back light
  if (! rtc.begin())            // real time clock check
  {
    lcd.print("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    lcd.print("RTC is NOT running!");
  }
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
  //rtc.adjust(DateTime(2022, 6, 7, 13, 23, 0));// to set the time manually

  digitalWrite(ledPin, LOW);   // make sure led is off to begin with
}




void loop() {
  buttonState = digitalRead(scanButton); // check if scan button changed
  if (buttonState == LOW) {
    scanBirthdays();
  }

  DateTime now = rtc.now();      // check rtc

  // Serial.print("The year now is  ");
  // Serial.println(now.year());
  // Serial.print("The month now is  ");
  // Serial.println(now.month());
  // Serial.print("The day now is "  );
  // Serial.println(now.day());

  for (uint8_t x = 0; x < sizeof (birthdays) / sizeof(birthdays[0]); x++) {  // loop through list ....const People birthdays
    //Serial.print ("Checking ");
    //Serial.println(birthdays[x].name);


    if ( birthdays[x].month == now.month() &&
         birthdays[x].day == now.day()) {   // see if any birthdays match todays day and month
      for (int j = 0; j < 10; j++) {
        digitalWrite(ledPin, HIGH);        // flash LED 10 times
        delay(interval);
        digitalWrite(ledPin, LOW);
        delay(interval);
      }
      //Serial.print("Happy Birthday ");
      //Serial.println(birthdays[x].name);
      lcd.setCursor(0, 0);                        //if they do then show on the LCD display
      lcd.print(" HAPPY BIRTHDAY ");
      lcd.setCursor(0, 1);
      lcd.print("                ");
      lcd.setCursor(6, 1);
      lcd.print(birthdays[x].name);
      lcd.print("    ");
      for (int i = 0; i < 10; i++) {     //flash LED 10 times again
        digitalWrite(ledPin, HIGH);
        delay(interval);
        digitalWrite(ledPin, LOW);
        delay(interval);
      }

      

    }

    else {
      Clock();                                      // jump to display clock
    }                                             // placing clock here gives 8 second clock display
                                                  // if above for loop operates
  }
}

//*************************************end of loop**************************************************
void Clock() {
  // print bottom line on 16 x 2 display
  DateTime now = rtc.now();                // check time on real time clock
  lcd.setCursor(1, 1);                     // display on LCD
  lcd.print("TIME");
  lcd.print(" ");

  if (now.hour() < 10) {
    lcd.print("0");
  }
  lcd.print(now.hour());
  lcd.print(':');
  if (now.minute() < 10) {                 // "0" leading if less than 10
    lcd.print("0");
  }
  lcd.print(now.minute());
  lcd.print(':');
  if (now.second() < 10) {
    lcd.print("0");
  }
  lcd.print(now.second());
  lcd.print("  ");

  //print top line on 16 x 2 display
  lcd.setCursor(0, 0);
  lcd.print(" ");
  lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  lcd.print("  ");
  lcd.print(now.day());
  lcd.print('/');
  lcd.print(now.month());
  lcd.print('/');
  int thaiYear = now.year() + 543;         // Thai year = +543 years
  lcd.print(thaiYear);
  lcd.print("  ");
  delay(1000);
}

//*******************************end of "clock"******************************************************

void scanBirthdays() {
  for (uint8_t x = 0; x < sizeof (birthdays) / sizeof(birthdays[0]); x++) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(birthdays[x].name); //scan the struct and look for names
    lcd.print(" BIRTHDAY");
    lcd.setCursor(0, 1);
    lcd.print(" ");
    lcd.print(birthdays[x].day);  //select the one that co-incides with the number through the for loop
    lcd.print(" of ");
    if (birthdays[x].month == 1) { //if the month is "1" print JANUARY etc.
      lcd.print("JANUARY");
    }
    if (birthdays[x].month == 2) {
      lcd.print("FEBRUARY");
    }
    if (birthdays[x].month == 3) {
      lcd.print("MARCH   ");
    }
    if (birthdays[x].month == 4) {
      lcd.print("APRIL   ");
    }
    if (birthdays[x].month == 5) {
      lcd.print("MAY     ");
    }
    if (birthdays[x].month == 6) {
      lcd.print("JUNE    ");
    }
    if (birthdays[x].month == 9) {
      lcd.print("SEPTEMBER");
    }
    delay(4000);
  }

}



//************************************program end***********************************************

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