Hello, I kindly asking for some help to my AC dimming project.
I moved the hardware to ESP8266 from an Arduino MEGA and want to control an AC dimming.
It works fine with MEGA, but when moving to a WEMOS D1, code crash with Serial output as below.
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
I know the TimerOne.h can't work on ESP8266 and use Ticker.h instead.
// lines marked with * works with Arduino MEGA etc boards.
// #include <TimerOne.h> // Use with Arduino MEGA etc.
#include <Ticker.h> // Ticker Library for ESP8266
Ticker TimeMS;
volatile int i=0; // Variable to use as a counter
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
// * int AC_pin = 3; // Output to Opto Triac
int AC_pin = 13;
int dim = 130; // Dimming level (0-30) 0 = on, 130 = 0ff
int inc=1; // counting up or down, 1=up, -1=down
int freqStep = 75; // This is the delay-per-brightness step in microseconds.
void setup()
{
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
// * attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
attachInterrupt(digitalPinToInterrupt(12), zero_cross_detect, RISING); // Attach an Interupt to I/O 12
// * Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
// * Timer1.attachInterrupt(dim_check, freqStep);
TimeMS.attach_ms(freqStep, dim_check); // Check every 75ms (50Hz)
}
void zero_cross_detect()
{
zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check()
{
if(zero_cross == true)
{
if(i>=dim)
{
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
Serial.println("Zero Cross Detected");
}
else
{
i++; // increment time step counter
}
}
}
void loop()
{
dim+=inc;
if((dim>=135) || (dim<=0))
inc*=-1;
delay(50);
}