Does not work is a vague description. How about lend a hand and do tell what you want the code to do and what the code is doing that makes it 'does not work.'
the constraint is not needed as far as i can tell since the 10bit analogread cant reply higher than 1023.
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
could be replaced with
signalMax = max(sample,signalMax); // save just the max levels
signalMin=min(sample,signalMin); // save just the min levels
and a delay of 1 in the while loop when doing analogRead.
the brightness thing you are doing in the start is throwing me off. why are you taking a sample in the begining (brightnes). then adding the peakTopeak voltage thing at the end.
what numbers are you ekspekting peakToPeak to be?
are you multiplying with 50 because of the 50 ms sampling window?
This you don't need to. PeakToPeak is correct.
and the dividing should be with 1024.0 not 1023
In the meantime there are some howlers in your code:-
if (sample < 1023) // toss out spurious readings
If you want to do that then make it 1024 or <= 1023. However it is just plain silly doing this. You will never get anything greater that 1023. I think it is one of those lines that was in a very early tutorial written by someone with not much of a handle on the software. Then people have copied it without really understanding how stupid it is.
double volts = ....
In the Arduino Double is the same as float so there is little point in using it. But why convert to volts anyway? All you need is a number to set your LED brightness, it doesn't have to be in any formal units.
if (volts > 15)
Really? You can get a reading of over 15V on a 5V system? Time for a rethink.
If you only sample for 20mS your chances of hitting the minimum and maximum peak of an audio signal are small indeed and you will get flickering even with a constant signal input. A correctly biased audio input ( you do have one of these don't you? ) will read 512 with zero volume input and given that audio signals are symmetrical to any degree of approximation that matters with an LED brightens you only need to consider positive signal excursions, not negative ones. So you can simply measure the maximum peak and subtract 512 from it to get a peak number.
However I do thing you probably need an envelope follow for your project.