Average analog input with detecting high anomaly

Hello,
I need to read analog value, average it and detect high value peaks. It will detect light with detection flash.
For example I need read 50 samples in time. These 50 samples will shift in time ( 60th sample will average between 10th and 60th sample ). Meantime I must detect high peak value.
I can read this 50 values and then find high peak value, but I need to do this in time and shift calculating average value.

You have a challenge…
Typically, flashes of light are very short duration, and the sampling rate of an ADC is relatively slow.

You may benefit from a peak-hold circuit before the ADC input.

Not knowing how the pulses are generated, you’re the only person that can identify which is the n’th event, but with that knowledge, this is a great project for a microprocessor.'

The light flash will be about 10-20ms. I mean it´s enough time to detect.

…assuming your code is sampling every 5ms….
Not stalled or doing anything else (delay etc).

Have a look at the runningAverage library

by comparing the new value with the average you can detect outliers.
You could base an outlier on e.g. 2x stddev if your board is fast enough.

you could consider sampling at only 8 bit using an external SPI based ADC

OK these are 10/12 bit but quite fast (imho)

you can use leaky integration to compute running averages

avg += (samp - avg) / N

separate averages can be computed for the peak and background level whree N is differeent if samp > avg

we used peak and noise averages for speech detection in speakerphones

How long will the flashes be? To the eye, a led pulsed at the same brightness will appear dimmer or brighter with shorter or longer pulse.. it's how we control led brightness.

I suggest catching the flashes with a phototransistor that switches resistor-limited VCC current into a wire connected to a digital pin (no pulldown!) that is being read at high frequency. Each AVR digital read samples 1 microamp out of that wire. Count the HIGH reads and at the next LOW, make the pin OUTPUT LOW to drain the wire then start reading the pin again. The count will be an analog measure of the entire flash. If 10 ms apart, you can make sure that each event is done and cleared before the next.

I have done this to measure force applied to piezo disk and it worked very well. I had a BJT open to let 5V through 2.2K resistor fill the wire and even a slight touch got > 20 HIGH reads.

Jumpers have capacity. A capacitor and resistor can time but then you have to catch the start where the wire in this case is a cheap buffer to be read when able.