Inherent delay in zero-cross detector?

Hi all,

I'm using a Robotdyn AC Dimmer with a Zero-Cross Detector to control the brightness of an AC-powered lightbulb through the Serial Port.

It works fine, but there is a large delay (perhaps almost 1s) between my Serial input and the dimming effect. What's very confusing is that if I mess around with the Dimmer Object's settings (changing Normal mode to Toggle mode but not giving it a range of brightnesses) I can see my Serial input can have an immediate effect in the dimmer (but the effect is buggy and only works when increasing brightness).

I have looked through the library the manufacturer provides and have found no in-built delay they might have added for reliability or otherwise.

My question is, is there an inherent delay with this sort of device or is it to do with the library or my code? I understand if the phase-switch is detected 60 times a second I should have a much higher reaction time than what I'm getting now.

This is my very simple code:

#define outputPin  12 


dimmerLamp dimmer(outputPin); 

int outVal = 0;

void setup() {
  Serial.begin(115200); 
  dimmer.begin(NORMAL_MODE, ON); 

}


void loop() {


  if (Serial.available())
  {
    int buf = Serial.parseInt();
    if (buf != 0) outVal = buf;
    
  }
  dimmer.setPower(outVal); // setPower(0-100%);


}

Many thanks, and a happy new year.

Can we guess you are using incandescent light bulbs? Can we guess you have not actually checked the light with some kind of meter? Can we guess you are relying on human vision to detect the brightness?

What did you actually use to determine the brightness of the light?

This is not a personal criticism, but pointing out that human vision cannot detect small changes in light.

Paul

Hi Paul.

I am indeed using an incandescent bulb. Do forgive my lack of clarity but the light intensity changes I request through the Serial Monitor are so large (going from 10% intensity [dimmer.setPower(10)] to 90% intensity [dimmer.setPower(90)]) that I feel can trust the accuracy of the input coming from my eyes. They cannot measure the brightness of the light in lumens, but they are good enough to detect a light bulb essentially switching on and off, and for me to determine that there is a very noticeable and unwanted delay.

Thanks.

The link you posted shows they have customer support. Have you asked them about the dimming delay?

Paul

Serial.parseInt() has a default 1-second timeout. Add a line ending to let .parseInt() know that it has reached the end of the input. It is good to flush the input buffer after the .parseInt() so the next loop doesn’t see that line ending and try to read another int.

  if (Serial.available())
  {
    int buf = Serial.parseInt();
    if (buf != 0) outVal = buf;
    // Flush the input buffer
    while (Serial.available())
      Serial.read();
  }

Thanks John! I had just come across a previous thread (including your reply) and have implemented one of Robin2's method's so as to work with start/end characters and to stay away from ParseInt.

Have a great day.

Btw, just tried your code and it works perfectly, but the delay is only gone if I also set Serial.setTimeout(10);
If not, the delay is much reduced but not quite eliminated.

Did you set Serial Monitor line ending to Newline?
snapshot1.png

snapshot1.png

There are much better way of dealing with serial input: store the value in a buffer (char array), parse when the termination character is received. That completely does away with any timeouts which block your code (and may come to haunt you when you start to expand the project).

See this Serial input basics tutorial.