This next code is for a power meter, still to be fine tuned but this will also be used in some for in my project.
Again you can see how I use the serial monitor to check how the code & math are going.
This one I believe will have to be made up of several custom nodes. This is to help you understand where I'm going and what I'm trying to build.
Please ask questions if you don't understand something, i'm still learning about this also as i go. It's fun most of the time.
As you can see once I get a handle on the voltage divider and were & what parts of the code to use, i will progress to the next part and then onto the following custom node build.
I code most of this in Note ++, before I load it into the Arduino just for info..
// Watt meter 24 Volt BI adding ampHR
int batMonPin = A0; // input pin for the voltage divider
int batVal = 0; // variable for the A/D value
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;
int analogInputPin = A1; // Analog input pin current output in mA
int sensorValue = 0;
int outputValue = 0;
int currentSensor = 0;
unsigned long msec = 0;
float time = 0.0;
int sample = 0;
float totalCharge = 0.0;
float averageAmps = 0.0;
float ampSeconds = 0.0;
float ampHours = 0.0;
float wattHours = 0.0;
float amps = 0.0;
float batteryState = 0;
int R1 = 29000; // Resistance of R1 in ohms
int R2 = 5000; // Resistance of R2 in ohms
float ratio = 0; // Calculated from R1 / R2
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps:
}
void loop() {
int sampleBVal = 0;
int avgBVal = 0;
int sampleAmpVal = 0;
int avgSAV = 0;
// ---------------------------------------------------------------------------------
// Volts, Amps & Power Value readings & calculations
for (int x = 0; x < 10; x++){ // run through loop 10x
{
double Current = (AnalogInputPin); // Read analog value
Serial.print(", V From Null: ");
printDouble(Current, 2); // display Current
Serial.print(" A");
Serial.println("");
delay(3000);
// --------------------------------------------------------------------------------------------------------
--------------------------- Current sensing not working BI ---------------
sensorValue = analogRead(analogInPin); // read A1 the analog in value from current sensor
sampleAmpVal = sampleAmpVal + sensorValue; // add samples together
avgSAV = sampleAmpVal / 10;
// Serial.print("Raw current= ");
// Serial.print(sampleAmpVal);
// Serial.print(" Raw volt= ");
// Serial.println(sampleBVal);
delay (1000); // let ADC settle before next sample
}
outputValue = ((sensorValue * 4.77) / 1024) / 0.06; // convert to milli amps - 0.5724
// Print decimal numbers
/*
void printDouble(double val, byte precision) {
Serial.print (int(val)); // Print int part
if( precision > 0) { // Print decimal part
Serial.print(".");
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
Serial.print(frac,DEC);
}
}
// Read 1.1V reference against AVcc
long readInternalVcc() {
long result;
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
// Calculate current with Allegro ACS714
double currentSensor(int RawADC) {
int Sensitivity = 66; // mV/A // orginal value is 66, for a 30 amp sensor
long InternalVcc = readInternalVcc();
double ZeroCurrentVcc = InternalVcc / 2;
double SensedVoltage = (RawADC * InternalVcc) / 1024;
double Difference = SensedVoltage - ZeroCurrentVcc;
double SensedCurrent = Difference / Sensitivity;
Serial.print("ADC: ");
Serial.print(RawADC);
Serial.print("/1024");
Serial.print(", Sensed Voltage: ");
printDouble(SensedVoltage, 1);
Serial.print("mV");
Serial.print(", 0A at: ");
printDouble(ZeroCurrentVcc, 1);
Serial.print("mV");
return SensedCurrent; // Return the Current
}
*/
// ----------------------- Voltage sensing --------------------------------------
batVal = analogRead(batMonPin); // read A0 the voltage on the divider
sampleBVal = sampleBVal + batVal; // add samples together
avgBVal = sampleBVal / 10; //divide by 10 (number of samples) to get a steady reading
pinVoltage = batVal * 0.00655; // Calculate the voltage on the A/D pin
ratio = (float)R1 / (float)R2; // Use the ratio calculated for the voltage divider
batteryVoltage = pinVoltage * ratio; // to calculate the battery voltage
amps = outputValue;
float watts = amps * batteryVoltage;
Serial.print("Volts = " );
Serial.print(batteryVoltage);
Serial.print("\t Current (amps) = ");
Serial.print(amps);
Serial.print("\t Power (Watts) = ");
Serial.print(watts);
// ------------------------------------------------------------------------------
// Time section
sample = sample + 1;
msec = millis();
time = (float) msec / 1000.0;
totalCharge = totalCharge + amps;
averageAmps = totalCharge / sample;
ampSeconds = averageAmps*time;
ampHours = ampSeconds/3600;
wattHours = batteryVoltage * ampHours;
Serial.print("\t Time (hours) = ");
Serial.print(time/3600);
Serial.print("\t Amp Hours (ah) = ");
Serial.print(ampHours);
Serial.print("\t Watt Hours (wh) = ");
Serial.print(wattHours);
batteryState = 300 - ampHours; // 300 Ah batt bank capacity
Serial.print("\t Batt Capacity Left (ah) = ");
Serial.println(batteryState);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(1000);
}
}