Ticker time with simulated 50Hz signal, FOR function

Hi all,

I have been trying to simulate the 50Hz signal to control a LED dimming Zero Cross Triac device. Still not have the Triac but was trying to simulate the signal and get into the grips of the programming. This will be implemented in a ESP8266.

First, tried to simulated the attachInterrupt() function, replacing the pin entry on the function by int variables ON/OFF, but did not work...

Tried the internal timer, but this runs too fast and not for my purposes. So tried the Ticker library...

The issue that I am experimenting with my program, see below code, is in the loop function when I call the FOR function to get the dimming effect, this blocks my previous function of calculating the times for each variable.

Debugging the code, when muting the FOR you can see that the times are calculated but when FOR is working, I cannot see any results of the times...

Any ideas? Thanks,

/*
  Passing paramters to Ticker callbacks
  Apart from void(void) functions, the Ticker library supports
  functions taking one argument. This argument's size has to be less or
  equal to 4 bytes (so char, short, int, float, void*, char* types will do).
  This sample runs two tickers that both call one callback function,
  but with different arguments.
  The built-in LED will be pulsing.
*/

#include <Ticker.h>

Ticker tickerSetHigh;
Ticker tickerSetLow;

const float t3 = 10;  // In a 50Hz signal, 50 times will cross the 0. Full wave will last 0.02sec (20millis) and will cross 2 the 0 cross. Half cycle of a wave of 50Hz is 10 millis.
float t1 = 0;         // OFF time, simulating a Triac this time will be off, millis to wait after zero cross.
float t2 = 0;         // ON trigger,simulating a Triac this time will be on till next zero cross, millis after ON trigger.  t3=t1+t2 
                      // IN THE TICKER FUNCTION, MILLIS ARE NOT TRIGGERED WHEN ZERO CROSS OCCURS, THEY ARE ABSOLUTE VALUES MEANING NOT WHEN TRIGGERS BUT HOW LONG IT LASTS!!!
float scale = 50;      // 0-100 power



void setPin(int state) {              //Zero cross function to call, simulates time (t1) SetLow (OFF) then it trigger the Triac with SetHigh (ON)(t2)
  digitalWrite(LED_BUILTIN, state);
}

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(1, HIGH);


 tickerSetLow.attach_ms(t2, setPin, 0);    // every t1 ms, call setPin(0). This will simulate OFF time on Triac

 tickerSetHigh.attach_ms(t1, setPin, 1);  // every t2 ms, call setPin(1). This will simulate ON time on Triac
}

void loop() {

t2 = (scale/100)*t3;                  // Period of ON time according to scale power.
t1 = t3-t2;                           // Period of OFF time according to scale power.
 
  Serial.println(scale);              //For debugging purposes
  Serial.println(t1);                 //For debugging purposes
  Serial.println(t2);                 //For debugging purposes
  
 for (int i=5; i<=100; i++){          // Dimming effect
    scale=i;
    delay(1000);                      // Time for the dimming effect
    Serial.println(scale);
  }
  }

It looks like you need to ditch the for loop and the delay() in favour of letting the loop() function do what its name implies and using millis() for timing.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

What do you mean by ditch for the loop and the delay?

Loop is calculating the ON/OFF (t1/t2) timing according of what the FOR is saying to them?

Trying to achieve, is keeping the cycle timing (t3) fixed (10 millis to emulate the zero cross 50Hz), the t1 and t2 will vary, giving a dimming effect.

Experimenting on the in-build LED before going to the mains...

What do you mean by ditch for the loop and the delay?

You don't need the for loop. The loop() function will loop for you
By using delay(1000) you cause the program to pause for 1 second, which causes your problem whereby your other code is not executed, hence my suggestion that you use millis() for timing soa as not to pause program execution.

Not sure how to go around it, there are 2 timers:

1 - control the zero cross for OFF/ON. This timer will say the amount of power (intensity of brightness)

2 - control the time of the dimming. Will say how long it will keep that power, that is why I entered the delay() on the FOR.

Getting rid of the delay() and entering the millis() did not improved at all...

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >=interval){
  previousMillis=millis();
  t2=interval;                   // Now the ON time is determined by the interval which the max will be the 10ms of the half cycle of 50Hz
  t1=t3-t2;
    Serial.println(t2);
      Serial.println(t1);
}
  for (int i=0; i<=10; i++){    // the dimming effect is controlled by the ON time = interval <=10ms
  interval=i;
  Serial.println(interval);
}
}