Buenas, estoy desarrollando un proyecto que trata sobre la medición de corriente AC, voltaje AC y potencia, estos valores se muestran en una pantala OLED y adicional tiene que hacer una lógica de control para activar y desactivar secuencialmente las salidas de un módulo de 8 reles, para esto utilice la función MILLIS(); el problema es que al igualar una variable llamada (encendidoMillis) a la (currentMillis) el código no funciona, pienso que el problema esta en la aparte de la lógica de control y el uso de millis, ya que al eliminar esta sección del código, llamada // SECUENCIA PARA ACTIVACION DE SALIDAS // , el muestreo de señales y la visualización en la pantalla OLED funcionan bien, e igual, con el sketch completo, pero colocando como comentario ( // encendidoMillis = currentMillis) tambien me corre el código, pero por supuesto no me hace bien la secuencia de encendido y apagado de los reles.
Agradezco cualquier aporte que puedan hacer para resolver la falla, a continuación está el código
//MEDICION DE CORRIENTE AC USANDO EL SENSOR SCT013-000
//MEDICION DE VOLTAJE AC USANDO EL SENSOR ZMPT101B
//I1 y I2 REAL , V1 y V2 REAL, T1 y T2 REAL
//CONTROL DE SALIDAS SEGUN RANGO DE CORRIENTE
//USO DE LIBRERIA EMONLIB PARA CALCULO DE CORRIENTE IRMS
//VISUALIZACION POR DISPLAY OLED 128X64
//AUTOR: LUIS HERNANDEZ - Iot and Automation - www.iotandautomation.com
//FECHA: 27/04/21
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// Libreria para calculo de corriente
#include "EmonLib.h"
//Librerias para pantallas
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <Wire.h>
// Creando una instancia EnergyMonitor
EnergyMonitor energyMonitor1;
EnergyMonitor energyMonitor2;
// Voltaje de nuestra red eléctrica
float voltajeRed = 120.0;
// Variables para calculo de voltaje
int adc_max=570; //Reemplazar por valor adc_max entregado por el sketch: volt_ac_cal
int adc_min=450; //Reemplazar por valor adc_min entregado por el sketch: volt_ac_cal
float volt_multi=120; //Reemplazar por el "voltaje ac rms" entregado por un multimetro
float volt_multi_p;
float volt_multi_n;
//Control de salidas
int APAGADO = 1;
int ENCENDIDO = 0;
int SALIDA1 = 2; // declaramos la variable SALIDA1 y le asignamos el pin 2
int SALIDA2 = 3; // declaramos la variable SALIDA2 y le asignamos el pin 3
int SALIDA3 = 4; // declaramos la variable SALIDA3 y le asignamos el pin 4
int SALIDA4 = 5; // declaramos la variable SALIDA4 y le asignamos el pin 5
int SALIDA5 = 6; // declaramos la variable SALIDA5 y le asignamos el pin 6
int SALIDA6 = 7; // declaramos la variable SALIDA6 y le asignamos el pin 7
int SALIDA7 = 8; // declaramos la variable SALIDA7 y le asignamos el pin 8
int SALIDA8 = 9; // declaramos la variable SALIDA8 y le asignamos el pin 9
// Definiendo datos apra pantalla Oled
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 12
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Variables para secuencia de activacion de salidas
//Global Variables
const byte BUTTON=10; // our button pin
const byte BUTTON2=11; // our button pin
unsigned long encendidoMillis; // when button was released
unsigned long apagadoMillis; // when led was turned on
unsigned long Encendido_SALIDA1 = 2000; // wait to turn on LED
unsigned long Encendido_SALIDA2 = 4000; // wait to turn on LED
unsigned long Encendido_SALIDA3 = 6000; // wait to turn on LED
unsigned long Encendido_SALIDA4 = 8000; // wait to turn on LED
unsigned long Encendido_SALIDA5 = 10000; // wait to turn on LED
unsigned long Encendido_SALIDA6 = 12000; // wait to turn on LED
unsigned long Encendido_SALIDA7 = 14000; // wait to turn on LED
unsigned long Encendido_SALIDA8 = 16000; // wait to turn on LED
unsigned long Apagado_SALIDA1 = 2000; // turn off LED after this time
unsigned long Apagado_SALIDA2 = 4000; // turn off LED after this time
unsigned long Apagado_SALIDA3 = 6000; // turn off LED after this time
unsigned long Apagado_SALIDA4 = 8000; // turn off LED after this time
unsigned long Apagado_SALIDA5 = 10000; // turn off LED after this time
unsigned long Apagado_SALIDA6 = 12000; // turn off LED after this time
unsigned long Apagado_SALIDA7 = 14000; // turn off LED after this time
unsigned long Apagado_SALIDA8 = 16000; // turn off LED after this time
bool SecuenciaEncendido = false; // flag for when button is let go
bool SecuenciaApagado = false; // flag for when button is let go
void setup()
{
Serial.begin(115200);
//Configuracion de entrada de botones
pinMode(BUTTON, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
// 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
energyMonitor1.current(0, 75.1);
energyMonitor2.current(1, 75.1);
//Configuracion de los pines
pinMode(SALIDA1, OUTPUT);
pinMode(SALIDA2, OUTPUT);
pinMode(SALIDA3, OUTPUT);
pinMode(SALIDA4, OUTPUT);
pinMode(SALIDA5, OUTPUT);
pinMode(SALIDA6, OUTPUT);
pinMode(SALIDA7, OUTPUT);
pinMode(SALIDA8, OUTPUT);
// Inicializando las salidas
digitalWrite(SALIDA1, APAGADO);
digitalWrite(SALIDA2, APAGADO);
digitalWrite(SALIDA3, APAGADO);
digitalWrite(SALIDA4, APAGADO);
digitalWrite(SALIDA5, APAGADO);
digitalWrite(SALIDA6, APAGADO);
digitalWrite(SALIDA7, APAGADO);
digitalWrite(SALIDA8, APAGADO);
// Inicializando la pantalla Oled
Wire.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); //0x3C
// Inicializacion para calculo de voltaje
volt_multi_p = volt_multi * 1.4142; //Voltaje pico= Voltaje RMS * 1.4142 (Corriente Monofasica)
volt_multi_n = volt_multi_p * -1;
// get the time at the start of this loop()
//unsigned long currentMillis = millis();
}
void loop()
{
// Obtenemos el valor de la corriente eficaz
// Pasamos el número de muestras que queremos tomar
double Irms1 = energyMonitor1.calcIrms(1484);
double Irms2 = energyMonitor2.calcIrms(1484);
// Calculamos la potencia aparente
double potencia1 = Irms1 * voltajeRed;
double potencia2 = Irms2 * voltajeRed;
// Obtenemos los valores de calculo de voltaje
float volt_rmsV1=get_voltageV1(); //Voltage eficaz Linea 1 (V-RMS)
float volt_rmsV2=get_voltageV2(); //Voltage eficaz Linea 2 (V-RMS)
// Mostramos la información por el monitor serie
Serial.print("Potencia1 = ");
Serial.print(potencia1);
Serial.print(" Irms1 = ");
Serial.println(Irms1);
Serial.print("Potencia2 = ");
Serial.print(potencia2);
Serial.print(" Irms2 = ");
Serial.println(Irms2);
// Mostrar valores en pantalla Oled
oled.clearDisplay();
oled.setTextColor(WHITE);
oled.setTextSize(0);
oled.setCursor(7,2); // (width=128, height=64)
oled.print("IoT and Automation");
oled.setTextSize(0);
oled.setCursor(0, 16); // (width=128, height=64)
oled.print("I1= ");
oled.print(Irms1,2);
oled.setTextSize(0);
oled.setCursor(66, 16); // (width=128, height=64)
oled.print("V1= ");
oled.print(volt_rmsV1,1);
oled.setTextSize(0);
oled.setCursor(0, 25); // (width=128, height=64)
oled.print("P1= ");
oled.print(potencia1,1);
oled.setTextSize(0);
oled.setCursor(0, 31); // (width=128, height=64)
oled.print("---------------------");
oled.setTextSize(0);
oled.setCursor(0, 36); // (width=128, height=64)
oled.print("I2= ");
oled.print(Irms2,2);
oled.setTextSize(0);
oled.setCursor(66, 36); // (width=128, height=64)
oled.print("V2= ");
oled.print(volt_rmsV2,1);
oled.setTextSize(0);
oled.setCursor(0, 45); // (width=128, height=64)
oled.print("P2= ");
oled.print(potencia2,1);
oled.setTextSize(0);
oled.setCursor(0, 51); // (width=128, height=64)
oled.print("---------------------");
oled.setTextSize(0);
oled.setCursor(0, 57); // (width=128, height=64)
oled.print("T1= 0.00");
//oled.print(temp1,2);
oled.setTextSize(0);
oled.setCursor(66, 57); // (width=128, height=64)
oled.print("T2= 0.00");
//oled.print(temp1,2);
oled.display();
// **** SECUENCIA PARA ACTIVACION DE SALIDAS *****////
if((Irms1 < 0.6) && (Irms2 < 0.6)){
// get the time at the start of this loop()
unsigned long currentMillis = millis();
// update the time when button was pushed
// int encendidoMillis = currentMillis;
// unsigned long encendidoMillis = currentMillis;
// encendidoMillis = currentMillis;
// SecuenciaEncendido = true;
//encendidoMillis = currentMillis;
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA1) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA1, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA2) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA2, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA3) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA3, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA4) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA4, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA5) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA5, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA6) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA6, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA7) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA7, ENCENDIDO);
}
if ((unsigned long)(currentMillis - encendidoMillis) >= Encendido_SALIDA8) {
// okay, enough time has passed since the button was let go.
digitalWrite(SALIDA8, ENCENDIDO);
//encendidoMillis = currentMillis;
}
}
else {
// get the time at the start of this loop()
unsigned long currentMillis = millis();
// update the time when button was pushed
//unsigned long apagadoMillis = currentMillis;
// apagadoMillis = currentMillis;
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA1) {
digitalWrite(SALIDA8, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA2) {
digitalWrite(SALIDA7, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA3) {
digitalWrite(SALIDA6, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA4) {
digitalWrite(SALIDA5, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA5) {
digitalWrite(SALIDA4, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA6) {
digitalWrite(SALIDA3, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA7) {
digitalWrite(SALIDA2, APAGADO);
}
if ((unsigned long)(currentMillis - apagadoMillis) >= Apagado_SALIDA8) {
digitalWrite(SALIDA1, APAGADO);
//apagadoMillis = currentMillis;
//SecuenciaApagado = false;
}
}
//***************************************//
///////////////////////////////////////////
}
float get_voltageV1(void)
{
//// ********** Variables Locales ***********////
float adc_sample;
float volt_inst=0;
float Sumatoria=0;
float volt;
long tiempo_init=millis();
int N=0;
//// ****************************************** ////
//// ********** Calculo de Voltaje en Linea 1 ***********////
while( (millis() - tiempo_init) < 500)//Duración 0.5 segundos(Aprox. 30 ciclos de 60Hz)
{
adc_sample = analogRead(A2); //voltaje del sensor
volt_inst = map(adc_sample,adc_min,adc_max,volt_multi_n,volt_multi_p);
Sumatoria = Sumatoria+sq(volt_inst); //Sumatoria de Cuadrados
N = N+1;
delay(1);
}
volt=sqrt((Sumatoria)/N); //ecuación del RMS
return(volt);
//// **************************************************** ////
}
float get_voltageV2(void)
{
//// ********** Variables Locales ***********////
float adc_sample;
float volt_inst=0;
float Sumatoria=0;
float volt;
long tiempo_init=millis();
int N=0;
//// ****************************************** ////
//// ********** Calculo de Voltaje en Linea 2 ***********////
while( (millis() - tiempo_init) < 500)//Duración 0.5 segundos(Aprox. 30 ciclos de 60Hz)
{
adc_sample = analogRead(A3); //voltaje del sensor
volt_inst = map(adc_sample,adc_min,adc_max,volt_multi_n,volt_multi_p);
Sumatoria = Sumatoria+sq(volt_inst); //Sumatoria de Cuadrados
N = N+1;
delay(1);
}
volt=sqrt((Sumatoria)/N); //ecuación del RMS
return(volt);
//// **************************************************** ////
}