Here is what I did recently in a small project.
A method to have an auto range voltmeter.
// M O D E # 4 V O L T M E T E R
case 4:
{
int temp = 0;
byte scaleFactor = 3;
digitalWrite(range,LOW); //Turn OFF the auto range Opto FET 0-15 volts
temp = analogRead(voltageIn);
delay (100);
temp = analogRead(voltageIn);
//are we below the threshold (0-5 volts)?
if (temp <= 350)
{
//we are in the range of 0-5 volts
//turn ON the auto range Opto FET
digitalWrite(range,HIGH);
scaleFactor = 1;
}
// subtract the last reading:
total = total - readings[index];
// read twice the voltage on Pin A0:
analogRead(voltageIn);
delay(100);
readings[index] = analogRead(voltageIn);
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
{
// ...wrap around to the beginning:
index = 0;
}
// calculate the average:
average = total / numReadings;
//use 5060 to compensate for the 1K resistor
i = map(average, 0, 1023, 0,5060 );
i = i * scaleFactor;
break;
}
