Displaying AVG voltage on LCD display trouble

Hello,

Any help here is much appreciated!

I’m using an Arduino Uno to read an amplified analog signal and print the values to an LCD screen. I’m trying to read the average voltage from an LED square wave.

My main goal is to

  1. only display values when the LED is on…so anything above 1V.
  2. Display the AVERAGE of the voltage spike, or square wave. Because there is some noise in my setup, the voltage at the top of the square wave bounces up and down. So instead of displaying the MAX voltage I’d like to read the AVERAGE of all of those data points.

I think my problem is that I’m a little off on my averaging logic. It is the same as my direct voltage reading. I’d expect it to be a little different.

Also I can’t seem to keep values below 1V from displaying. The numbers are correct but I really don’t need to see anything below 1V.

And finally, when the LED is on and a voltage spike is detected, the reading is not instantaneous and takes a couple of seconds for the numbers to reach their final destination. I’m wondering how to make it much faster.

Thanks for looking!

#include <LiquidCrystal.h>       
#include <stdio.h>
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);    

float OFFSET = 1.0;               // Above 1.0 = spike
float TEMP = 0;                    // Holds temp numbers
float SPIKE_AVG = 1.0;  // Average of all values in spike
bool SPIKE_DETECTED = false;


void setup() {
  
  Serial.begin(9600);
  while (!Serial);      
 lcd.begin(16, 2); 
lcd.print("Begin");
delay(1000);

 }

void loop() {
TEMP = analogRead(A0); 
 Serial.println((5.0 * TEMP)/1024);   //Displays A0 in Volts in Serial Monitor
  
 if( TEMP > OFFSET )
 {
    
  SPIKE_DETECTED = true;                      //voltage spike detected
    SPIKE_AVG = ( SPIKE_AVG  + TEMP)/2 ;    // Calculates the new average
    lcd.clear();
    lcd.print((5.0 * SPIKE_AVG)/1024);                     // Prints Spike average to LCD
 }

 else
 {
 
   
  if( TEMP < OFFSET )
   {
    SPIKE_DETECTED = false;  
 lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Below Offset"); 
   }

   SPIKE_DETECTED = false;  
 lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Below Offset");                       
 }

 delay(200);
}