maxValue problem, Solved quickly

Hi This is AC voltmeter which is using maxValue - minValue for calculating amplitude of AC signal, I want to add on LCD maxValue to be displayed,.
This is whot I did ( marked by +++++++++++ ) but M=0.00 all the time.
How to fix it ?

/*
  Measuring AC Current Using ACS712
  www.circuits4you.com
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
const int sensorIn = PA7;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

//+++++++++++++++++++++
double maxValue = 0;
//+++++++++++++++++++++

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {

  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707; //root 2 is 0.707
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");

  lcd.setCursor(0, 1);
  lcd.print("U=");
  lcd.print(VRMS);
  
//++++++++++++++++++++++++
  lcd.setCursor(0, 0);
  lcd.print("M=");
  lcd.print(maxValue);
//++++++++++++++++++++++++

}

float getVPP()
{
  float result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  //int minValue = 1024;          // store min value here
  int minValue = 4095.0;

  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {
    readValue = analogRead(sensorIn);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the minimum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  //result = ((maxValue - minValue) * 5.0)/1024.0;
  result = ((maxValue - minValue) * 5.0) / 4095.0;

  return result;


}

You have two different variables called maxValue, one with global scope, and one with local scope - it is visible only in getVPP

Lose the "float" in front of the second one.

 
float getVPP()
{
   result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  //int minValue = 1024;          // store min value here
  int minValue = 4095.0;

This way ?
Doesn't make a sens.

Sorry. "int" in front of maxValue.

Or change the global double into an int.

Thanks
it is working.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.