Advertisement
hwthinker

AC Light Control With potensio

Nov 18th, 2018
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*AC Light Control
  2.  
  3.  Update by HwThinker
  4.  * added Serial
  5.  *
  6.  Updated by Robert Twomey
  7.  
  8.  Changed zero-crossing detection to look for RISING edge rather
  9.  than falling.  (originally it was only chopping the negative half
  10.  of the AC wave form).
  11.  
  12.  Also changed the dim_check() to turn on the Triac, leaving it on
  13.  until the zero_cross_detect() turn's it off.
  14.  
  15.  Adapted from sketch by Ryan McLaughlin
  16.  http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1230333861/30
  17.  
  18.  */
  19.  
  20. #include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
  21. int sensorValue = 0;        // value read from the pot
  22. const int analogInPin = A0;
  23. volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
  24. volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
  25. int AC_pin = 3;                // Output to Opto Triac
  26. int dim = 0;                    // Dimming level (0-128)  0 = on, 128 = 0ff
  27. int inc=1;                      // counting up or down, 1=up, -1=down
  28.  
  29. int freqStep = 75;    // This is the delay-per-brightness step in microseconds.
  30.                       // For 60 Hz it should be 65
  31. // It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
  32. // and the number of brightness steps you want.
  33. //
  34. // Realize that there are 2 zerocrossing per cycle. This means
  35. // zero crossing happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply.
  36.  
  37. // To calculate freqStep divide the length of one full half-wave of the power
  38. // cycle (in microseconds) by the number of brightness steps.
  39. //
  40. // (120 Hz=8333uS) / 128 brightness steps = 65 uS / brightness step
  41. // (100Hz=10000uS) / 128 steps = 75uS/step
  42.  
  43. void setup() {                                      // Begin setup
  44.    Serial.begin(9600);
  45.   pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  46.   attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  47.   Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  48.   Timer1.attachInterrupt(dim_check, freqStep);      
  49.   // Use the TimerOne Library to attach an interrupt
  50.   // to the function we use to check to see if it is
  51.   // the right time to fire the triac.  This function
  52.   // will now run every freqStep in microseconds.                                            
  53. }
  54.  
  55. void zero_cross_detect() {    
  56.   zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  57.   i=0;
  58.   digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
  59. }                                
  60.  
  61. // Turn on the TRIAC at the appropriate time
  62. void dim_check() {                  
  63.   if(zero_cross == true) {              
  64.     if(i>=dim) {                    
  65.       digitalWrite(AC_pin, HIGH); // turn on light      
  66.       i=0;  // reset time step counter                        
  67.       zero_cross = false; //reset zero cross detection
  68.     }
  69.     else {
  70.       i++; // increment time step counter                    
  71.     }                                
  72.   }                                  
  73. }                                  
  74.  
  75. void loop() {                        
  76. //  dim+=inc;
  77. //  if((dim>=128) || (dim<=0))
  78. //    inc*=-1;
  79.   sensorValue = analogRead(analogInPin);
  80.   dim = map(sensorValue, 0, 1023, 0, 128);
  81.   // print the results to the Serial Monitor:
  82.   Serial.print("sensor = ");
  83.   Serial.print(sensorValue);
  84.   Serial.print("\t output = ");
  85.   Serial.println(dim);
  86.   delay(18);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement