Con Arduino uno estoy implementando el sensor SCT-013-00 y ZMPT101B, para medir corriente, voltaje y obtener la potencia de cada ciclo. He conseguido un funcionamiento muy preciso de ambos sensores por separado. El tema, que no soy capaz de implementar ambos códigos en uno solo. Siempre me acaba saliendo un error o no me mide adecuadamente. Dejo los códigos por aquí por si podéis ayudarme. Gracias.
#include <Filters.h>
float testFrequency = 50; // Frecuencia (Hz)
float windowLength = 40.0/testFrequency; // promedio de la señal
int Sensor = 0; //A0
float intercept = -0.04; // to be adjusted based on calibration testing
float slope = 0.0405; // to be adjusted based on calibration testing
float volts; // Voltage
unsigned long periodo = 1000;
unsigned long tiempoAnterior = 0;
#include "EmonLib.h";
EnergyMonitor energyMonitor;
void setup() {
Serial.begin(9600);
delay(5000);
energyMonitor.current(1, 1.6);
RunningStatistics inputStats;
inputStats.setWindowSecs(windowLength);
}
void loop() {
Sensor = analogRead(A0); //Leer pin Analógico
inputStats.input(Sensor);
if((unsigned long)(millis() - tiempoAnterior) >= periodo) {
volts = intercept + slope * inputStats.sigma(); //offset y amplitud
volts = volts*(40.3231); //calibración
Serial.print("\tVoltage: ");
Serial.println(volts);
tiempoAnterior = millis();
// Obtenemos el valor de la corriente eficaz
// Pasamos el número de muestras que queremos tomar
double Irms = energyMonitor.calcIrms(1484);
// Calculamos la potencia aparente
double potencia = Irms * volts;
// Mostramos la información por el monitor serie
Serial.print("Potencia = ");
Serial.print(potencia);
Serial.print(" Irms = ");
Serial.println(Irms);
}
}
El error que me sale tras implementarlos es In function 'void loop()':
error: 'inputStats' was not declared in this scope
Adjunto los códigos por separado
Codigo SCT-013-00
#include "EmonLib.h"
// Crear una instancia EnergyMonitor
EnergyMonitor energyMonitor;
// Voltaje de nuestra red eléctrica
float voltajeRed = 230.0;
void setup()
{
Serial.begin(9600);
// Iniciamos la clase indicando
// Número de pin: donde tenemos conectado el SCT-013
// Valor de calibración: valor obtenido de la calibración teórica
energyMonitor.current(0, 1.6);
}
void loop()
{
// Obtenemos el valor de la corriente eficaz
// Pasamos el número de muestras que queremos tomar
double Irms = energyMonitor.calcIrms(1484);
// Calculamos la potencia aparente
double potencia = Irms * voltajeRed;
// Mostramos la información por el monitor serie
Serial.print("Potencia = ");
Serial.print(potencia);
Serial.print(" Irms = ");
Serial.println(Irms);
}
Código ZMPT101B
<
#include <Filters.h>
float testFrequency = 50; // Frecuencia (Hz)
float windowLength = 40.0/testFrequency; // promedio de la señal
int Sensor = 0; //A0
float intercept = -0.04; // to be adjusted based on calibration testing
float slope = 0.0405; // to be adjusted based on calibration testing
float volts; // Voltage
unsigned long periodo = 1000;
unsigned long tiempoAnterior = 0;
void setup() {
Serial.begin(9600);
delay(5000);
}
void loop() {
RunningStatistics inputStats;
inputStats.setWindowSecs(windowLength);
while(true) {
Sensor = analogRead(A1); //Leer pin Analógico
inputStats.input(Sensor);
if((unsigned long)(millis() - tiempoAnterior) >= periodo) {
volts = intercept + slope * inputStats.sigma(); //offset y amplitud
volts = volts*(40.3231); //calibración
Serial.print("\tVoltage: ");
Serial.println(volts);
tiempoAnterior = millis();
}
}
}