Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original code kindly provided by::
- // http://www.youtube.com/watch?v=ztXRjyCQxlE
- // backyard amusement
- // What follows is a minor tweak.
- const int PIN_ZERO_CROSS = 2; // ZeroCross (ZCR)
- const int PIN_TRC = 1; // Triac (TRC)
- const int PIN_LED = 14; // LED on Teensy.
- unsigned long timer = 0;
- int dimStep = 5;
- int dimDir = -1;
- int minDimValue = 0; // Min dim value
- int maxDimValue = 128; // Max Dim value.
- int dimValue = maxDimValue;
- void setup()
- {
- pinMode(PIN_ZERO_CROSS, INPUT);
- pinMode(PIN_TRC,OUTPUT);
- pinMode(PIN_LED,OUTPUT);
- }
- void loop()
- {
- if(digitalRead(PIN_ZERO_CROSS))
- {
- zeroCross();
- }
- // Fading Test code. Sets dim value betwen 0 and 128 in steps of 5.
- if(micros() - timer > 1500){
- timer = micros();
- dimValue += dimDir;
- if(dimValue >= maxDimValue){
- dimDir = -1;
- dimValue = maxDimValue;
- }
- if(dimValue <= minDimValue){
- dimDir = 1;
- dimValue = minDimValue;
- }
- }
- }
- // In the UK i'm on 50hz. That means 50 cycles per second.
- // For each half cycle we need to turn the TRC off when the zeropoint has occurred
- // and on after a delay. The longer the delay the dimmer the light.
- //
- //
- // At the beginning of the cycle (the ZeroCross event) we want to switch the TRC OFF (LOW)
- // After the required delay we want to turn the TRC ON (HIGH) for the remainder of the cycle.
- //
- // Different from the other code examples, which caused flicker, i've done two things.
- // First! i'm not using the interrupt. Based on other peoples suggestions.
- // Second! I've changed the order of setting the low and high pins. This stopped the flickering.
- //
- // Working out the delay.
- // In 1000ms (1second) we have 50 cycles, so 1 cycle = 20ms
- // Every half cycle we will get our ZeroCross event. So 1/2 a cycle = 10ms
- // Every time we get the zeroCross event, we need to wait Nms before turning the TRC on.
- //
- // So how do we determine the amount of time to wait? We take the max wait time of 10ms and divide it
- // by however many levels of light we need. I've used the 128 in the other examples, but you
- // could use 255 for example, if you were converting an rgb value into a light level.
- // delay time = 10000us/255 = 39.21us (microseconds)
- //
- // If your input range is quite arbitrary they you can use the following Arduino method to map it.
- // int dimValue = constrain ( map(myValue, 200, 1024, 0, 128) , 0, 128);
- //
- // Once we have the delay time we then every time the ZeroCross() method is called we just wait that time
- // before setting the TRC pin high.
- //
- // Firing angle calculation
- // 50Hz-> 10ms (1/2 Cycle) → (10000us - 10us) / 128 = 78 (Approx)
- // 60Hz-> 8.33ms (1/2 Cycle) → (8333us - 8.33us) / 128 = 65 (Approx)
- void zeroCross()
- {
- int dimtime = 0;
- float propTime = 0;
- if(true){
- dimtime = (77*dimValue);
- }
- else{
- dimtime = (65*dimValue);
- }
- digitalWrite(PIN_TRC, LOW); // triac Off
- delayMicroseconds(dimtime); // Off cycle
- digitalWrite(PIN_TRC, HIGH); // triac firing
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement