Power Measurement

```
Code fragment
/*
AC Power Calculations
Average Power, Apparent Power, Reactive Power, Irms, Vrms, Power Factor, Power Factor Angle
Date: Dec. 11, 2009

Line Current is measured using a current transformer from CR Magnetics.

CR8410-1000 @ http://www.crmagnetics.com/8400.pdf
Line voltage measurement via a standard AC step down transformer

Author: peter c
*/

#include
#include
#include
#include

#include
#include

#define LCD_DISPLAY_WIDTH  15
#define LCD_DEGREE_CHAR 0xF2

#define TEMPERATURE_PIN   5
#define CURRENT_BLACK_PIN 2               // current 1st 100 AMP input pin (black )
#define CURRENT_RED_PIN   3               // current 1st 100 AMP input pin (red )
#define VOLTAGEPIN 1                      // voltage signal input pin

#define DC_OFFSET 2.5                     // signal shifted 2.5 above ground

#define Te            2011.0               // current transformer effective turns ratio from speck sheet
#define CT_VMAX       2.5                 // maximum level shifted voltage from CT to input e.g. 7.2 v rms to 2.5 v-pp offset

#define CT_VRATED     10.61891             // Max rated RMS voltage * sqrt(2) of CT from spec sheed
//#define CT_VRATED     7.03239            // Max rated RMS voltage * sqrt(2) of CT from spec sheed
#define CT_LOAD_RES   151.0               // current transformer load resistor to create a voltage drop
//#define CT_LOAD_RES   100.0               // current transformer load resistor to create a voltage drop
#define MAX_SAMPLES   2000                // Samples per Calculation

#define PEAK_LINE_VOLTAGE   170           //120 * sqrt(2)
#define PEAK_VOLTAGE_TRANSFORMER   2.5    // input to A2D max AC value

#define TEMP_CONV  150./(1.5*1023/5)

#define BLACK_INDEX 0                     // offsets into array for holding power values
#define RED_INDEX   1
#define MAX_PRECISION 10

#define DISPLAY_RMS               1
#define DISPLAY_REAL_POWER        2                 // Real and Apparent
#define DISPLAY_APPARENT__POWER   3
#define DISPLAY_POWER_FACTORS     4                 // Power Factor
#define DISPLAY_PFA               5                 // Power Factor Angle
#define DISPLAY_CREST_FACTOR      6                 //
#define DISPLAY_LINE_SPECS        7                 // line voltage and crest factor
#define DISPLAY_C02               8                 // Killowatt hour and C02
#define DISPLAY_TEMPERATURE       9
#define DISPLAY_TIME              10
#define MAX_DISPLAY_MODE          10

#define SAMPLING_INTERVAL         50              // milli-seconds
#define DISPLAY_MODE_PIN        4               // display input switch mode

#define MB_MAX_REGISTERS 35                             // maximum Modbus registers

// Reset totalizers at 12:00 midnight every day
#define HOUR_RESET   0
#define MINUTE_RESET 0
#define SECOND_RESET 4

//create object to control an LCD.
//number of lines in display=1

LCD4Bit lcd = LCD4Bit(2);

//ACSignal voltageSignal( VOLTAGEPIN, 1, DC_OFFSET );
//ACSignal currentSignalBlack( CURRENT_BLACK_PIN, 1, DC_OFFSET );

ACSignal voltageSignal( VOLTAGEPIN, PEAK_LINE_VOLTAGE/PEAK_VOLTAGE_TRANSFORMER, DC_OFFSET );
ACSignal currentSignalBlack( CURRENT_BLACK_PIN, Te / CT_LOAD_RES * CT_VRATED / CT_VMAX, DC_OFFSET );
ACSignal currentSignalRed( CURRENT_RED_PIN, Te / CT_LOAD_RES * CT_VRATED / CT_VMAX, DC_OFFSET );

PowerSignal panelBlack( voltageSignal, currentSignalBlack );
PowerSignal panelRed( voltageSignal, currentSignalRed );

//PowerSignal panelBlack(voltageSignal, voltageSignal );

int g_displayMode =  MAX_DISPLAY_MODE ;
int modbus_regs[ MB_MAX_REGISTERS ];
int gDebug =0;
float gTemperature = 0.;
time_t gPCtime;

union
{
int regsf[2];
float val;
}
bfconvert;

union
{
int regsl[2];
long val;
}
blconvert;

void setup()
{
lcd.init();

init_mb_slave(19200, 'n', 0);
pinMode( DISPLAY_MODE_PIN,INPUT);
gTemperature = analogRead( TEMPERATURE_PIN ) * TEMP_CONV;

Serial.begin(19200); // start serial communication

//Serial.println("Start");

}

void loop()
{

char  l_timestampBuffer[ 20 ];

voltageSignal.beginSampling();
currentSignalBlack.beginSampling();
currentSignalRed.beginSampling();

panelBlack.beginSampling();
panelRed.beginSampling();

for( int i=0; i
```