#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
volatile unsigned long vzeit[3];
volatile unsigned long vtime[3];
volatile int v0 = 0;
volatile int vv = 0;
volatile unsigned long czeit[3];
volatile unsigned long ctime[3];
volatile int c0 = 0;
volatile int cc = 0;
unsigned long vzeit0 = 0, vzeit1 = 0, vtime0 = 0, vtime1 = 0;
unsigned long czeit0 = 0, czeit1 = 0, ctime0 = 0, ctime1 = 0;
unsigned long vperiod = 0;
unsigned long cperiod = 0;
float vfrequency = 0.0;
float cfrequency = 0.0;
float U = 0.0, I = 0.0, S = 0.0, P = 0.0, Q = 0.0, E = 0.0, phi = 0.0, t = 0.0;
void buttons_interrupt_handler_2() {
unsigned long timer = micros();
if (v0 < 2) vzeit[v0++] = timer;
if (vv < 2) vtime[vv++] = timer;
}
void buttons_interrupt_handler_3() {
unsigned long timer = micros();
if (c0 < 2) czeit[c0++] = timer;
if (cc < 2) ctime[cc++] = timer;
}
void setup() {
Serial.begin(115200);
while (!Serial) { ; }
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLUE);
tft.setTextColor(ILI9341_GREENYELLOW, ILI9341_BLUE);
tft.setTextSize(3);
attachInterrupt(digitalPinToInterrupt(2), buttons_interrupt_handler_2, RISING);
attachInterrupt(digitalPinToInterrupt(3), buttons_interrupt_handler_3, RISING);
}
void loop() {
//Serial.print(analogRead(A0));
//Serial.print("\t");
//Serial.print(analogRead(A1));
//Serial.print("\n");
//return;
noInterrupts();
if (v0 == 2 && c0 == 2) {
vzeit0 = vzeit[0];
vzeit1 = vzeit[1];
czeit0 = czeit[0];
czeit1 = czeit[1];
v0 = 0;
c0 = 0;
}
if (vv == 2) {
vtime0 = vtime[0];
vtime1 = vtime[1];
vv = 0;
}
if (cc == 2) {
ctime0 = ctime[0];
ctime1 = ctime[1];
cc = 0;
}
interrupts();
//correction micros() overflow every 71.58 minutes
// if (t1 > t2) {
// t2 = 4294967295UL - t1 + t2 + 1;
// t1 = 0;
vperiod = vtime1 - vtime0;
if (vperiod == 0) vfrequency = 0.0;
else vfrequency = 1e6 / vperiod;
cperiod = ctime1 - ctime0;
if (cperiod == 0) cfrequency = 0.0;
else cfrequency = 1e6 / cperiod;
phi = 0.0;
if ((vzeit1 - vzeit0) > 0) {
if (czeit0 > vzeit0) phi = -360.0 * (czeit0 - vzeit0) / (vzeit1 - vzeit0); // vperiod;
if (vzeit0 > czeit0) phi = 360.0 * (vzeit0 - czeit0) / (vzeit1 - vzeit0); // vperiod;
}
phi = fmod(phi, 360);
if (phi > 180) phi -= 360;
if (phi < -180) phi += 360;
static int m = 0;
static unsigned long elapse = millis();
if (millis() - elapse > 500) {
elapse = millis();
//------------------------------------------------
U = getRmsVoltage(A0, vperiod, 500.0f);
I = getRmsVoltage(A1, cperiod, 500.0f);
if (U == 0.0) vfrequency = 0.0;
if (I == 0.0) cfrequency = 0.0;
if (U == 0.0) phi = 0.0f;
if (I == 0.0) phi = 0.0f;
//------------------------------------------------
S = U * I;
P = U * I * cos(radians(phi));
Q = U * I * sin(radians(phi));
static unsigned long zeit = millis();
static unsigned long ontm = millis();
E = E + P * (millis() - zeit) / 3.6e9;
zeit = millis();
t = (zeit - ontm) / 3.6e6;
//------------------------------------------------
Serial.println(m++);
Serial.print(U);
Serial.println(" V");
Serial.print(vfrequency);
Serial.println(" Hz");
Serial.print(I);
Serial.println(" A");
Serial.print(cfrequency);
Serial.println(" Hz");
Serial.print(phi);
Serial.println(" Grad");
Serial.print(S);
Serial.println(" VA");
Serial.print(P);
Serial.println(" W");
Serial.print(Q);
Serial.println(" var");
Serial.print(E, 4);
Serial.println(" kWh");
Serial.print(t, 4);
Serial.println(" h");
Serial.println();
display();
}
}
float getRmsVoltage(uint8_t pin, uint32_t period, float sensitivity) {
if (period == 0) return 0.0;
uint8_t loopCount = 1;
double readingVoltage = 0.0f;
for (uint8_t i = 0; i < loopCount; i++) {
// Calculate zero point (center Value)
uint32_t Vsum = 0;
uint32_t measurements_count = 0;
uint32_t t_start = micros();
while (micros() - t_start < period) {
Vsum += analogRead(pin);
measurements_count++;
}
int zeroPoint = Vsum / measurements_count;
// Calculate root mean square (RMS) of AC voltage
int Vnow;
Vsum = 0;
measurements_count = 0;
t_start = micros();
while (micros() - t_start < period) {
Vnow = analogRead(pin) - zeroPoint;
Vsum += (Vnow * Vnow);
measurements_count++;
}
readingVoltage += sqrt(Vsum / measurements_count) / 1023.0f * 5.0f * sensitivity;
}
return readingVoltage / loopCount;
}
float display() {
tft.setCursor(0, 0);
tft.setCursor(0, tft.getCursorY() + 13);
tft.print(F("U:"));
tft.print(FloatToChar(U, 7));
tft.println(F(" V"));
tft.print(F("f:"));
tft.print(FloatToChar(vfrequency, 7));
tft.println(F(" Hz"));
tft.setCursor(0, tft.getCursorY() + 13);
tft.print(F("I:"));
tft.print(FloatToChar(I, 7));
tft.println(F(" A"));
tft.print(F("f:"));
tft.print(FloatToChar(cfrequency, 7));
tft.println(F(" Hz"));
tft.setCursor(0, tft.getCursorY() + 13);
tft.print((char)231);
tft.print(F(":"));
tft.print(FloatToChar(phi, 7));
tft.print(F(" "));
tft.println((char)247);
tft.setCursor(0, tft.getCursorY() + 13);
tft.print(F("S:"));
tft.print(FloatToChar(S, 7));
tft.println(F(" VA"));
tft.print(F("P:"));
tft.print(FloatToChar(P, 7));
tft.println(F(" W"));
tft.print(F("Q:"));
tft.print(FloatToChar(Q, 7));
tft.println(F(" var"));
tft.setCursor(0, tft.getCursorY() + 13);
tft.print(F("E:"));
tft.print(FloatToChar(E, 7));
tft.println(F(" kWh"));
tft.print(F("t:"));
tft.print(FloatToChar(t, 7));
tft.println(F(" h"));
}
char* FloatToChar(float U, uint8_t R ) {
// R = sign + digits
if (R < 1) return 0;
static char strf[20];
static char unit[20];
R--;
char c;
// 32 = ' '
// 43 = '+'
// 45 = '-'
// 48 = '0'
if (U < 0) c = 45;
else if (U > 0) c = 32;
else c = 32;
U = fabs(U);
if (U < pow(10, R - 2)) {
dtostrf(U, R, R - 2, strf);
} else {
dtostrf(U, R, 0, strf);
if (strf[0] == 32) strf[0] = 48;
}
snprintf(unit, R + 2, "%c%s", c, strf);
return unit;
}voltage
current
phase shift