Hi all,
i managed to (re) find the wording for code i was after.
here is my code thus far.
/*
result smoothing: http://www.arduino.cc/en/Tutorial/Smoothing
Voltage calculation: http://arduinotronics.blogspot.com.au/2011/03/monitoring-voltage-of-dc-battery-supply.html
*/
int VoltageIn = A5;
const int numReadings = 40;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int latchpin = 8; // connect to pin 12 on the '595 GREEM
int clockpin = 12; // connect to pin 11 on the '595 BLUE
int datapin = 11; // connect to pin 14 on the '595 YELLOW
float b = 0;
int c = 0;
float d = 0;
int e = 0;
int speed = 500; // used to control speed of counting
int segdisp[10] = {
252,96,218,242,102,182,190,228,254,246};
void setup()
{
Serial.begin(9600); // initialize serial communication with computer:
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
pinMode(latchpin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(datapin, OUTPUT);
}
void loop()
{
total= total - readings[index]; // subtract the last reading:
readings[index] = analogRead(VoltageIn);// read from the sensor:
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
if (index >= numReadings) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning:
average = total / numReadings; // calculate the average:
Serial.println(average); // send it to the computer as ASCII digits
delay(1); // delay in between reads for stability
int val = analogRead(average); // read the value from the sensor
float volts = (average * 0.021965811965812 * 0.94104308390023); // calculate the ratio
Serial.println("Volts:");
Serial.println(volts); // print the value in volts
Serial.println("");
Serial.println("");
int ones = int(volts);
int tenths = int((volts - ones) * 10);
Serial.println("voltage _test:");
Serial.println(ones);
Serial.println(tenths);
Serial.println("average value");
Serial.println("");
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, LSBFIRST, 0); // clears the right display
shiftOut(datapin, clockpin, LSBFIRST, 0); // clears the left display
shiftOut(datapin, clockpin, LSBFIRST, 0); // clears the left display
digitalWrite(latchpin, LOW);
if (volts < 10)
{
int tens = 0;
int ones = int(volts);
int tenths = int((volts - ones) * 10);
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, LSBFIRST, segdisp[tenths]); // clears the right display
shiftOut(datapin, clockpin, LSBFIRST, segdisp[ones]); // clears the left display
shiftOut(datapin, clockpin, LSBFIRST, segdisp[tens]); // clears the left display
digitalWrite(latchpin, LOW);
}
else if (volts >= 10)
{
int tens = int(volts / 10);
int ones = int((volts - tens * 10));
int tenths = int((volts - tens * 10 - ones) * 10);
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, LSBFIRST, segdisp[tenths]); // clears the right display
shiftOut(datapin, clockpin, LSBFIRST, segdisp[ones]); // clears the left display
shiftOut(datapin, clockpin, LSBFIRST, segdisp[tens]); // clears the left display
digitalWrite(latchpin, LOW);
}
delay(5); // to allow for a reset time
}
and i two questions. when writing to the '595s, there is a very annoying flicker between each value and re-print onto the 7 segments.
is there any way that that could be reduced?
and second. the relationship between the calculated voltage and true voltage should be a linear relationship, yes? i only ask because mine is not, i can have 1.2V calc == 1.2V measured, however bumping it up to 7.2V true gives me a fluctuating 7.3 - 7.4V measured