Advertisement
babyyoda_

Dimmer_isr_loop

Jul 1st, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "PinChangeInterrupt.h"
  2. #include "TinyWireS.h"
  3.  
  4. #define I2C_SLAVE_ADDR  2
  5. int AC_LOAD = 3;
  6. int ZCD_Pin = 1;
  7. int dimming = 128;
  8. int Speed[3] = {90, 50, 20};
  9. int speedValue;
  10. bool fan;
  11. bool isr_;
  12.  
  13. void setup() {
  14.   pinMode(AC_LOAD, OUTPUT);
  15.   TinyWireS.begin(I2C_SLAVE_ADDR);
  16.   TinyWireS.onReceive(receiveEvent);
  17.   pinMode(ZCD_Pin, INPUT_PULLUP);
  18. }
  19.  
  20.  
  21. void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
  22. {
  23.   isr_ = true;
  24. }
  25.  
  26. void receiveEvent(uint8_t howMany)
  27. {
  28.   byte byteRcvd = 0;
  29.   if (TinyWireS.available()) {
  30.     byteRcvd = TinyWireS.receive();     // get the byte from master
  31.  
  32.     if (byteRcvd == '0') {
  33.       fan = false;
  34.       detachPinChangeInterrupt(digitalPinToPCINT(ZCD_Pin));
  35.       digitalWrite(AC_LOAD, LOW);
  36.     }
  37.     if (byteRcvd == '1') {
  38.       fan = true;
  39.       speedValue = Speed[0];    //90
  40.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
  41.     }
  42.     if (byteRcvd == '2') {
  43.       fan = true;
  44.       speedValue = Speed[1];   //50
  45.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
  46.     }
  47.     if (byteRcvd == '3') {
  48.       fan = true;
  49.       speedValue = Speed[2];  //20
  50.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
  51.     }
  52.     dimming = speedValue;
  53.   }
  54. }
  55.  
  56. void loop() {
  57.   if (isr_) {
  58.     long int dimtime;
  59.     dimtime = (75 * dimming);  // For 60Hz =>65
  60.     delayMicroseconds(dimtime);    // Wait till firing the TRIAC
  61.     digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  62.     delayMicroseconds(100);         // triac On propogation delay  // (for 60Hz use 8.33) Some Triacs need a longer period
  63.     digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement