Advertisement
babyyoda_

i2c_Dimmer_issue

Jun 26th, 2021
69
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;    //default off
  8. bool fan;
  9. int fanSpeed[3] = {90, 50, 20};
  10. int speedValue;
  11.  
  12. void setup() {
  13.   pinMode(AC_LOAD, OUTPUT);            //
  14.   TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
  15.   pinMode(ZCD_Pin, INPUT_PULLUP);
  16.   attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  17. }
  18.  
  19.  
  20. void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
  21. {
  22.   int dimtime = (75 * dimming);  // For 60Hz =>65
  23.   delayMicroseconds(dimtime);    // Wait till firing the TRIAC
  24.   digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  25.   delayMicroseconds(100);         // triac On propogation delay  // (for 60Hz use 8.33) Some Triacs need a longer period
  26.   digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
  27. }
  28.  
  29. void loop() {
  30.   byte byteRcvd = 0;
  31.   if (TinyWireS.available()) {
  32.     byteRcvd = TinyWireS.receive();     // get the byte from master
  33.  
  34.     if (byteRcvd == '0') {
  35.       fan = false;
  36.       detachPinChangeInterrupt(digitalPinToPCINT(ZCD_Pin));
  37.       digitalWrite(AC_LOAD, LOW);
  38.     }
  39.     if (byteRcvd == '1') {
  40.       fan = true;
  41.       speedValue = fanSpeed[0];
  42.  
  43.     }
  44.     if (byteRcvd == '2') {
  45.       fan = true;
  46.       speedValue = fanSpeed[1];
  47.       //      if(fan) attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  48.     }
  49.     if (byteRcvd == '3') {
  50.       fan = true;
  51.       speedValue = fanSpeed[2];
  52.    
  53.     }
  54.    
  55.     dimming = speedValue;
  56.    
  57.   }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement