Save Times to an array

Hello, I'm using a DS3231 as my RTC, and i'm trying to get it to save the times the sensor is activated to an array for it to be printed later.

However, when i go to print the array of times, all of the times are the same as the very last one. So i assume it's running rtc.getTimeStr() when i recall it.
How would i be able to make it print the time it went off and not the current time?

Thanks

(code attached)

PIR.ino (1.67 KB)

Code posted as advised in Read this before posting a programming question

#include <DS3231.h>
#include <LiquidCrystal.h>
#include <IRremote.h>

char *savedTimes[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
int remotenum;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
int senPin = 9;
int receiver = 7;
int senState = LOW;
int x = 0;
int addr = 0;
DS3231  rtc(SDA, SCL);
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
IRrecv irrecv(receiver);
decode_results results;

void setup()
{
  // put your setup code here, to run once:
  pinMode(senPin, INPUT);
  Serial.begin(9600);
  rtc.begin();
  lcd.begin(16, 2);
  irrecv.enableIRIn();
}

void loop()
{
  // put your main code here, to run repeatedly:
  senState = digitalRead(senPin);
  if (senState == HIGH)
  {
    lcd.setCursor(0, 0);
    lcd.print("Motion Detected");
    savedTimes[addr] = rtc.getTimeStr();
    addr = addr + 1;
    if (addr == 16)
    {
      addr = 0;
      Serial.println("Adress made 0");
    }
    //Serial.println(rtc.getTimeStr());
    lcd.setCursor(0, 1);
    lcd.print(rtc.getTimeStr());
    //Serial.println("Motion Detected");
    delay(2000);
  }
  else
  {
    x = 0;
  }
  if (irrecv.decode(&results))
  {
    remotenum = results.value;
    Serial.println(remotenum);
    if (remotenum == 9755 || -23971)
    {
      pinMode(senPin, OUTPUT);
      digitalWrite(senPin, LOW);
      lcd.setCursor(0, 0);
      lcd.write("                ");
      lcd.setCursor(0, 0);
      lcd.write("SENSOR STOPPED");
      for (int i = 0; i < 16; i++)
      {
        Serial.println(savedTimes[i]);
        delay(100);
      }
    }
    irrecv.resume();
  }
}

When you post code that uses a library that does not come with the IDE, please post a link to where to get the library or the library name to look for in the library manager. Like the LiquidCrystal library there are more than one DS3231 library.

if (remotenum == 9755 || -23971)Oops

I suspect that getTimeStr returns a pointer to a buffer where it puts the time as text. You copy this pointer into your array so every element points to the same buffer which is why you see the time repeated.

Your array doesn't have any memory set aside to store the time either, it's just an array of pointers. You need to make it a 2D array with sufficient space in each location to store the time string and when you read it, use strcpy to populate your array.

groundFungus:
When you post code that uses a library that does not come with the IDE, please post a link to where to get the library or the library name to look for in the library manager. Like the LiquidCrystal library there are more than one DS3231 library.

Everyone should do this. I usually use something like this:

#include <AD9850SPI.h> // GitHub - F4GOJ/AD9850SPI: AD9850 SPI library for arduino

which keeps the file name and its download URL at one place, usually in a header file.

wildbill:
I suspect that getTimeStr returns a pointer to a buffer where it puts the time as text. You copy this pointer into your array so every element points to the same buffer which is why you see the time repeated.

Your array doesn't have any memory set aside to store the time either, it's just an array of pointers. You need to make it a 2D array with sufficient space in each location to store the time string and when you read it, use strcpy to populate your array.

Would take much less memory for the array if you saved the time as unixtime and converted to text for printing.