Synchronization a button with a zerocrossing signal

Hi, Blynkers.

I’m trying to synchronize a button with a zerocrossing signal every 4 Hz, but I’m having difficulty. I’m using the ESP-01 and I do not have the equipment to measure the signals. The serial is also not available for debugging.

The button circuit and zerocrossing are the same: http://www.dextrel.net/diyzerocrosser.htm

The code I tried is this:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
//-------------------------------------------------------------------------------------------------------------------------------------------------------
char auth[] = "x6x2xdx4xex8x2xfxdx1x2xbx6x4xfxe"; //Autenticacao do App Blynk com o Android
//-------------------------------------------------------------------------------------------------------------------------------------------------------
// Redes Credenciadas
//char ssid[] = "MyASUS";           //Rede
//char pass[] = "DomoTest";         //Senha
char ssid[] = "MY3206";             //Rede
char pass[] = "xxxxxxxx";           //Senha
//-------------------------------------------------------------------------------------------------------------------------------------------------------
// Config para ESP-01
#define out_moc     0                  //Pino de saida/acionamento do MOC - D2 da placa
#define wifi        1                  //Pino do led ESP-01
#define zrc         2
#define sw          3
//-------------------------------------------------------------------------------------------------------------------------------------------------------
SimpleTimer application;
//-------------------------------------------------------------------------------------------------------------------------------------------------------
int dim; //Nível do atenuador - valor menor é mais brilhante
volatile boolean switch_button;  // Boolean to store a "switch" to tell us if we have crossed zero
volatile boolean wifi_sts = false; //atribuído quando conectado a uma rede Wi-Fi
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void check_application()            //Funcao para tratar os comandos listados acima (porta XNOR)
{
  if (switch_button == true)
    digitalWrite(wifi, LOW);
  else
    digitalWrite(wifi, HIGH);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()  {
  Serial.begin(9600);             //Set de velocidade da serial do PC
  delay(10);
  Blynk.begin(auth, ssid, pass);  //Autenticacao de App e rede a se conectar
  delay(10);

  pinMode(out_moc, OUTPUT);       //Seta pino como saida para acionemento do MOC
  pinMode(wifi, OUTPUT);
  pinMode(zrc, INPUT_PULLUP);
  pinMode(sw, INPUT_PULLUP);

  digitalWrite(wifi, HIGH);
  digitalWrite(out_moc, HIGH);

  while (WiFi.status() == WL_NO_SHIELD) { //ou WL_CONNECTED
    for (int j = 0; j <= 4; j++)  {
      digitalWrite(wifi, LOW);
      delay(250);
      digitalWrite(wifi, HIGH);
    }
    digitalWrite(out_moc, HIGH);
  }

  application.setInterval(250L, check_application); //Constante de tempo para chamar a funcao que trata dos pinos de entrada e saida
  attachInterrupt(digitalPinToInterrupt(zrc), zero_cross_detect, FALLING);
    
  Blynk.connect();
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void zero_cross_detect() {
  delayMicroseconds(50);
  switch_button = (digitalRead(sw));
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------
void loop() {                        
if (Blynk.connected())  {
    if (wifi_sts != true)  {
      for (int i = 0; i <= 4; i++)  {
        digitalWrite(wifi, LOW);
        delay (500);
        digitalWrite(wifi, HIGH);
        delay (500);
      }
      wifi_sts = true;
      digitalWrite(wifi, LOW);
    }
    else  {
      Blynk.run();  //Roda aplicativo
      application.run();  //Roda o tempo
    }
  }
  else  {
    for (int i = 0; i <= 9; i++)  {
      digitalWrite(wifi, LOW);
      delay (250);
      digitalWrite(wifi, HIGH);
      delay (250);
    }
    wifi_sts = false;
    Blynk.connect();
  }
}

What is wrong?

I had to edit your post to correctly format your code for proper forum viewing, as documented in the Welcome Topic.