Trying to figure out voltage peak measurement that decreases in peak over time

I am trying to figure this out without an array, but not sure where to start if that’s even possible. I don’t need anyone to post the code for me, but maybe a general direction on where to begin would be helpful, or pointers to references I can use to learn.

I have a voltage that I want to measure under load in a certain way (say 5v), it stays stable under load with a bit of a voltage drop for each device added. This changes my maximum voltage that can be reported because I want my maximum reported voltage dependent on number of devices dropping it.

5v 1 device makes the range 2.5v to 4.8v
5v input 2 devices makes the range 2.5v to 4.6v
pulling off the 2nd device brings the voltage back up to 4.8v ( I then want to use this new max value for the range)

Essentially I want to measure the voltage first, then store a variable holding that value as current maximum.

Then I want to apply a load that will drop the voltage.

I want to measure that drop with an analog read, which I know how to do, and report that drop to the serial monitor, along with the new maximum.

Then I want to add a load and sample the voltage again and use the new stable voltage under load as a new maximum to drive a new min/max range from there.
Then remove a load and see if I can restore my range to the previous range dynamically.

That’s the simplification. I want to do this for multiple loads.

What are you connecting (and how), that pulls your supply voltage down 50%? That has to be fixed first.

That’s not the problem. I would have the same scenario measuring the amplitude variability in a sine wave from top to bottom.

I am just experimenting with resistance and measurements teaching myself more in-depth programming.

Oh, I see.

Compare subsequent voltage values. If the new value is lower than the preceding one then that preceding value is the current relative maximum.

For load changes keep a “long time maximum” obtained in multiple consecutive measurements. Then check for a number of consecutive new maximum values of about the same height. Once you got enough same readings you have found a new long time maximum.

Allow for some noise on all measured values. Same value does not mean absolutely equal but equal within a certain acceptable interval.