Advertisement
babyyoda_

AttinyDimmerGood

Jun 19th, 2021
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int pot = A3;
  2. int potvalue;
  3.  
  4. int AC_LOAD = 1;    // Output to Opto Triac pin
  5. int dimming = 128;  // Dimming level (0-128)  0 = ON, 128 = OFF
  6.  
  7. void setup()
  8. {
  9.   pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  10.   attachInterrupt(0, zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above
  11.   pinMode(A3, INPUT);
  12. }
  13.  
  14. //the interrupt function must take no parameters and return nothing
  15. void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
  16. {
  17.   // Firing angle calculation : 1 full 50Hz wave =1/50=20ms
  18.   // Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle)
  19.   // For 60Hz => 8.33ms (10.000/120)
  20.   // 10ms=10000us
  21.   // (10000us - 10us) / 128 = 75 (Approx) For 60Hz =>65
  22.  
  23.   int dimtime = (75*dimming);    // For 60Hz =>65    
  24.   delayMicroseconds(dimtime);    // Wait till firing the TRIAC    
  25.   digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  26.   delayMicroseconds(100);         // triac On propogation delay  // (for 60Hz use 8.33) Some Triacs need a longer period
  27.   digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
  28. }
  29.  
  30. void loop()  {
  31.   potvalue = analogRead(pot);
  32.   dimming = map(potvalue, 0, 1023, 0, 128);
  33. //
  34. ////    dimming = 128;
  35.  
  36. //  for (int i=5; i <= 128; i+=5){
  37. //    dimming=i;
  38. //    delay(5000);
  39. //   }
  40. }
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement