Advertisement
emileswain

Arduino AC Dimmer ino

Sep 6th, 2014
2,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. // Original code kindly provided by::
  2. // http://www.youtube.com/watch?v=ztXRjyCQxlE
  3. // backyard amusement
  4. // What follows is a minor tweak.
  5.  
  6. const int PIN_ZERO_CROSS = 2;  // ZeroCross (ZCR)
  7. const int PIN_TRC = 1;  // Triac (TRC)
  8. const int PIN_LED = 14; // LED on Teensy.
  9.  
  10. unsigned long timer = 0;
  11. int dimStep = 5;
  12. int dimDir = -1;
  13. int minDimValue = 0; // Min dim value
  14. int maxDimValue = 128; // Max Dim value.
  15. int dimValue = maxDimValue;
  16.  
  17.  
  18. void setup()
  19. {
  20.  pinMode(PIN_ZERO_CROSS, INPUT);
  21.  pinMode(PIN_TRC,OUTPUT);
  22.  pinMode(PIN_LED,OUTPUT);
  23. }
  24.  
  25. void loop()
  26. {
  27.   if(digitalRead(PIN_ZERO_CROSS))
  28.   {
  29.     zeroCross();
  30.   }
  31.  
  32.   // Fading Test code. Sets dim value betwen 0 and 128 in steps of 5.
  33.   if(micros() - timer > 1500){
  34.       timer = micros();
  35.       dimValue += dimDir;
  36.  
  37.       if(dimValue >= maxDimValue){
  38.         dimDir = -1;
  39.         dimValue = maxDimValue;
  40.       }
  41.       if(dimValue <= minDimValue){
  42.         dimDir = 1;
  43.         dimValue = minDimValue;
  44.       }
  45.     }
  46. }
  47.  
  48.   // In the UK i'm on 50hz. That means 50 cycles per second.
  49.   // For each half cycle we need to turn the TRC off when the zeropoint has occurred
  50.   // and on after a delay. The longer the delay the dimmer the light.
  51.   //
  52.   //
  53.   // At the beginning of the cycle (the ZeroCross event) we want to switch the TRC OFF (LOW)
  54.   // After the required delay we want to turn the TRC ON (HIGH) for the remainder of the cycle.
  55.   //
  56.   // Different from the other code examples, which caused flicker, i've done two things.
  57.   // First! i'm not using the interrupt. Based on other peoples suggestions.
  58.   // Second! I've changed the order of setting the low and high pins. This stopped the flickering.
  59.   //
  60.   // Working out the delay.
  61.   // In 1000ms (1second) we have 50 cycles, so 1 cycle = 20ms
  62.   // Every half cycle we will get our ZeroCross event. So 1/2 a cycle = 10ms
  63.   // Every time we get the zeroCross event, we need to wait Nms before turning the TRC on.
  64.   //
  65.   // So how do we determine the amount of time to wait? We take the max wait time of 10ms and divide it
  66.   // by however many levels of light we need. I've used the 128 in the other examples, but you
  67.   // could use 255 for example, if you were converting an rgb value into a light level.
  68.   // delay time = 10000us/255 = 39.21us (microseconds)
  69.   //
  70.   // If your input range is quite arbitrary they you can use the following Arduino method to map it.
  71.   // int dimValue = constrain ( map(myValue, 200, 1024, 0, 128) , 0, 128);
  72.   //
  73.   // Once we have the delay time we then every time the ZeroCross() method is called we just wait that time
  74.   // before setting the TRC pin high.
  75.   //
  76.   // Firing angle calculation
  77.   // 50Hz-> 10ms (1/2 Cycle) → (10000us - 10us) / 128 = 78 (Approx)
  78.   // 60Hz-> 8.33ms (1/2 Cycle) → (8333us - 8.33us) / 128 = 65 (Approx)
  79. void zeroCross()
  80. {
  81.   int dimtime = 0;
  82.   float propTime = 0;
  83.   if(true){
  84.     dimtime = (77*dimValue);
  85.   }
  86.   else{
  87.     dimtime = (65*dimValue);
  88.   }
  89.   digitalWrite(PIN_TRC, LOW);    // triac Off
  90.   delayMicroseconds(dimtime);    // Off cycle
  91.   digitalWrite(PIN_TRC, HIGH);   // triac firing
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement