Advertisement
tiodocomputador

AC Light Dimmer - Inmojo

Jan 12th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. /*
  2.  AC Light Dimmer - Inmojo
  3.  AC Voltage dimmer with Zero cross detection
  4.  
  5.  Author: Charith Fernanado http://www.inmojo.com charith@inmojo.com
  6.  License: Released under the Creative Commons Attribution Share-Alike 3.0 License.
  7.  http://creativecommons.org/licenses/by-sa/3.0
  8.  Target:  Arduino
  9.  
  10.  Attach the Zero cross pin of the module to Arduino External Interrupt pin
  11.  Select the correct Interrupt # from the below table
  12.  Pin    |  Interrrupt # | Arduino Platform
  13.  ---------------------------------------
  14.  2      |  0            |  All
  15.  3      |  1            |  All
  16.  18     |  5            |  Arduino Mega Only
  17.  19     |  4            |  Arduino Mega Only
  18.  20     |  3            |  Arduino Mega Only
  19.  21     |  2            |  Arduino Mega Only
  20.  
  21.  */
  22.  
  23. int AC_LOAD = 3;    // Output to Opto Triac pin
  24. int dimming = 128;  // Dimming level (0-128)  0 = ON, 128 = OFF
  25. int buttonPin = 2;
  26. int buttonState;
  27. void setup()
  28. {
  29.   Serial.begin(9600);
  30.   pinMode(AC_LOAD, OUTPUT);       // Set the AC Load as output
  31.   attachInterrupt(0, zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above
  32. }
  33.  
  34. void zero_crosss_int()  // function to be fired at the zero crossing to dim the light
  35. {
  36.   // Firing angle calculation :: 60Hz-> 8.33ms (1/2 Cycle)
  37.   // (8333us - 8.33us) / 128 = 65 (Approx)
  38.   int dimtime = (65*dimming);      
  39.   delayMicroseconds(dimtime);    // Off cycle
  40.   digitalWrite(AC_LOAD, HIGH);   // triac firing
  41.   delayMicroseconds(8.33);         // triac On propogation delay
  42.   digitalWrite(AC_LOAD, LOW);    // triac Off
  43. }
  44.  
  45. void loop()
  46. {
  47.   buttonState = digitalRead(buttonPin);
  48.   Serial.print("Primeira leitura: ");
  49.   Serial.print(buttonState);
  50.   Serial.println("");
  51.  
  52.   dimming = 128;
  53.   delay(100);
  54.   dimming = 75;  
  55.   delay(100);
  56.   dimming = 25;  
  57.   delay(100);
  58.   dimming = 0;  
  59.   delay(100);
  60.  
  61.   buttonState = digitalRead(buttonPin);
  62.   Serial.print("Segunda leitura: ");
  63.   Serial.print(buttonState);
  64.   Serial.println("");
  65.  
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement