What is a proper way to store sensor values from n>1000 readings ago

Hello everyone!
I am trying to figure out a way to print a value of a sensor alongside a value from some time in the past.
Here's my code that works - it gets a reading every second and stores values for past 5 readings:

#include <DHT.h>;

#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

float hum;    //Stores humidity value
float oldhum; //Stores hum from 5 readings ago

// here my ugly workaround begins...
float hum4;   // humidity 1 reading ago
float hum3;   // humidity 2 readings ago
float hum2;   // humidity 3 readings ago
float hum1;   // humidity 4 readings ago
// ...................................................................

unsigned long previousMillis = 0;
const long interval = 1000;


void setup() {
  Serial.begin(9600);
  delay(500);
  
  dht.begin();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    
    hum = dht.readHumidity();

// this is the part that I want to rewrite properly
    oldhum = hum4;
    hum4 = hum3;
    hum3 = hum2;
    hum2 = hum1;
    hum1 = hum;
// -------------------------------------------------
    Serial.print("current humidity: ");
    Serial.println(hum);
    Serial.print("old humidity 5 readings ago: ");
    Serial.println(oldhum);
  }
}

What I'm trying to achieve is to have an option to change the reading interval and also the time between current reading and past reading to be shown - so for example:

const long interval = 500;                  // half a second between sensor readings
const long someTimeAgo = 900000             // approx. 15 mins between two sensor values shown

(someTimeAgo / interval) would be the number of sensor readings between now and a past reading that i want to show.
Any help would be greatly appreciated, I was trying to imagine such a thing with my very limited programming skills and got stuck.

Firstly you'd use an array for the data - that's already a win over the current code.

Secondly a circular buffer would mean not having to move any data at all, just the
insert and extract pointers/indices would change. Circular buffer - Wikipedia

However for 1000 readings you might have issues with the amount of RAM on some
Arduino boards.

Thank you for pointing me towards circular buffers! I have tried something like this: (please scroll to the bottom of the sketch)

#include <DHT.h>;

// Constants for DHT22
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//Variables
float hum;    //Stores humidity value
float oldhum; //Stores hum from 5 readings ago
float myCircularBuffer[6];               // array with five slots
int readPointer = 0;                    // and reading from the first one
int writePointer = readPointer + 1;     // distance between readings that interest me
unsigned long previousMillis = 0;
const long interval = 5000;


void setup() {
  Serial.begin(9600);
  delay(500);
  
  dht.begin();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    
    hum = dht.readHumidity();
    myCircularBuffer[arrayCounter(true)] = hum;            // to store hum in the array as a n-th element (writing)
    oldhum = myCircularBuffer[(arrayCounter(false))];    // to assign value to oldhum reading from the array (reading)

    Serial.print("current humidity: ");
    Serial.println(hum);
    Serial.print("old humidity 1 reading ago: ");
    Serial.println(oldhum);
    Serial.print("hum was stored in the slot nr: ");
    Serial.println(writePointer);
    Serial.print("oldhum was read from the slot nr: ");
    Serial.println(readPointer);
    Serial.println();
  }
}

int arrayCounter (bool writeTrue) {
  if (writeTrue == true) {
    writePointer++;
    if (writePointer > 5) {
      writePointer = 0;
    }
    return writePointer;
  }
  else {
    readPointer++;
    if (readPointer > 5) {
      readPointer = 0;
    }
    return readPointer;
  }
}

Is that a circular buffer? I have two variables - readPointer and another one writePointer pointing to five array elements ahead. I have also found a library CircularArray GitHub - rlogiacco/CircularBuffer: Arduino circular buffer library .
I will try to estimate which one is more memory-consuming.

What board/processor are you using? If its an UNO, like many beginners, you get 2,000 Bytes of RAM. If your data sample is an int, your 1,000 samples just wiped out the entire thing.

-jim lee

It's a pro mini (so also 2kb). I will have to decrease the resolution of stored values, so "value from exactly 15min ago" will become a "one of the values from 15 to 16min ago", which is fine for this project.

Or you can use an extremal memory module like this - Adafruit SPI Non-Volatile FRAM Breakout - 64Kbit / 8KByte : ID 1897 : $5.95 : Adafruit Industries, Unique & fun DIY electronics and kits

You are wasting space with float values. IIRC DHT readings will fit completely inside an int. That will cut your memory footprint in half.