// This “Setup” part runs just once
void setup() {
Serial.begin(9600);
}

// This “Loop” part will repeat forver…
void loop() {
long startMillis= millis(); // get time in milliseconds
int peakToPeak = 0; // a variable to save peak-to-peak level

int signalMax = 0; // a minimum value for a max value
int signalMin = 1024; // a max. value for a min value

// These codes will find the max and the min value from a sensor during the first 50 milliseconds
while (millis() – startMillis < 50) // run only the first 50 milliseconds…
{
int sample = analogRead(0); // get a value from a sensor
if (sample > signalMax) // if a value is higher than a max value,
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin) // if a value is lower than a min value,
{
signalMin = sample; // save just the min levels
}
}

peakToPeak = signalMax – signalMin; // max – min = peak amplitude
// check the range of values
// then change the 100, and 400 values
// 0, 255 is decided for LED brightiness values.
int displayPeak = map(peakToPeak, 100, 400, 0, 255);
analogWrite(11, displayPeak); // lit LED using the peak value
Serial.println(peakToPeak);
}