This is a simple program to read and display the current voltage on analog pin 0 and print the current voltage and maximum voltage read.
The problem I'm having is that the maximum value is recalculated each time even if the current reading is less than the maximum reading so the maximum voltage is always equal to the current voltage. What am I doing wrong?
Here is my code:
const int analogPin = 0; // replace 0 with analog pin
int sensorReading, maxReading;
int adcVal, i, numSamples = 1;
float maxVoltage, voltage;
void setup()
{
analogReference(DEFAULT);
Serial.begin(9600);
maxVoltage == 0.00;
maxReading == 0;
}
void loop()
{
sensorReading =(analogRead(analogPin));
if (sensorReading > maxReading); //Do we have a new maximum reading?
{ // begining of If Then
maxReading = sensorReading; //if true then make the current sensor reading the new maximum reading
maxVoltage = maxReading * (5.00/1023.00); //calculate new maximum voltage
} //end of If Then
voltage = sensorReading * (5.00/1023.00); //calculate current voltage
Serial.print("current voltage is ");Serial.print(voltage); Serial.print(" Max Voltage is "); Serial.print(maxVoltage);
Serial.println();
delay(1000);
}