The use of the ESP8266 in the world of IoT
User avatar
By Josep112
#69864 I'm trying to make a dimmer with 2 channels but without success when I press the button 1 the two lamps dimmer together, any idea where I'm going?

#include <ESP8266WiFi.h>
int LAMP[2] = {13,12};
int dimming[2] = {125,125};

void setup()
{
Serial.begin(115200);
pinMode(LAMP[0], OUTPUT);
pinMode(LAMP[1], OUTPUT);
attachInterrupt(14, zero_crosss, RISING);
pinMode(5, INPUT);
pinMode(4, INPUT);
}


void zero_crosss()
{

for(int i=0; i < 2; i++)
{
int dimtime = (65*dimming[i]);
delayMicroseconds(dimtime);
digitalWrite(LAMP[i], HIGH);
delayMicroseconds(8.33);
digitalWrite(LAMP[i], LOW);
}

}





void loop()
{
if(digitalRead(5)==0)
{
yield;
if(dimming[0] == 0)
{
dimming[0] = 125;
}
int Milliseconds_1;

Milliseconds_1 = millis()+100;
while (Milliseconds_1 > millis()) ;
dimming[1]= dimming[0] -5;
Serial.println(dimming[0]);
}



if(digitalRead(4)==0)
{
yield;
if(dimming[1] == 0)
{
dimming[1] = 125;
}
int Milliseconds_2;
Milliseconds_2 = millis()+100;
while (Milliseconds_2 > millis()) ;
dimming[1]= dimming[1] -5;
Serial.println(dimming[1]);
}
}
User avatar
By schufti
#69886 sorry, but it is not worth spending time on this.
Boardsearch wil show you that others before you failed on having:

a) delay in interrup service routine
(will sooner or later hit you with watchdogtimer timeout)
b) get reliable zero cross detection on esp8266
(has too much jitter due to WiFi bringing undeterminable delay to isr response time)

your isr (with for loop) does not provide two independent delays ... (when will 2nd dealy start?)
think about the first delay being longer than the second (lamp_1 dimmer than lamp_2) ...
User avatar
By trackerj
#69890 As schufti said above, it's complicated to run reliable ZCD and ISR service routines on the ESP8266 together with the other functions that you might want like WIFI, http server, etc.
The only reliable solution that I found to work very well is a AC dimmer that has integrated phase detector, ZCD , etc, so you don't bring any kind of overhead on your ESP8266.

For more details take a look here: MPDMv4 AC Dimmer switch - general view and to the related articles and projects. Combined with a multichannel DAC or even PWM I have run from one ESP8266 upto 32 separate AC Dimmers simultaneously without any problems.

Happy breadboarding,
TJ.