-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCMVoltSense.h
83 lines (54 loc) · 2.14 KB
/
MCMVoltSense.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
MCMVoltSense.cpp - Library for Grove AC Voltage Sensor
Author: Christopher Mendez, November 3 2022
*/
#ifndef MCMVoltSense_h
#define MCMVoltSense_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// define theoretical vref calibration constant for use in boardVcc()
// 1100mV*1024 ADC steps
// override in your code with value for your specific AVR chip
#ifndef READVCC_CALIBRATION_CONST
#define READVCC_CALIBRATION_CONST 1126400L
#endif
// to enable 12-bit ADC resolution on Arduino Due,
// include the following line in main sketch inside setup() function:
// analogReadResolution(ADC_BITS);
// otherwise will default to 10 bits, as in regular Arduino-based boards.
#if defined(__arm__)
#define ADC_BITS 12
#else
#define ADC_BITS 10
#endif
#define ADC_COUNTS (1<<ADC_BITS)
class MCMmeter
{
public:
void VoltageStp(unsigned int _analogVin, double _VoltCal, double _PhaseCal);
void analogVoltage(unsigned int cycles, unsigned int timeout);
long boardVcc();
//Useful value variables
double Vrms;
private:
//Set Voltage and current input pins
unsigned int analogVin;
//Calibration coefficients
//These need to be set in order to obtain accurate results
double VoltCal;
double PhaseCal;
//--------------------------------------------------------------------------------------
// Variable declaration
//--------------------------------------------------------------------------------------
int sampleV; //sample holds the raw analog read value
double lastFilteredV,filteredV; //Filtered is the raw analog value minus the DC offset
double offsetV; //Low-pass filter output
double phaseShiftedV; //Holds the calibrated phase shifted voltage.
double sqV,sumV; //sq = squared, sum = Sum
int startV; //Instantaneous voltage at start of sample window.
boolean lastVCross, checkVCross; //Used to measure number of times threshold is crossed.
};
#endif