Advertisement
babyyoda_

i2c_OnRec()

Jun 27th, 2021
109
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.   TinyWireS.onReceive(receiveEvent);
  16.   pinMode(ZCD_Pin, INPUT_PULLUP);
  17.   attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  18. }
  19.  
  20.  
  21. void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
  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 receiveEvent(uint8_t howMany)
  31. {
  32.   byte byteRcvd = 0;
  33.   if (TinyWireS.available()) {
  34.     byteRcvd = TinyWireS.receive();     // get the byte from master
  35.  
  36.     if (byteRcvd == '0') {
  37.       fan = false;
  38.       detachPinChangeInterrupt(digitalPinToPCINT(ZCD_Pin));
  39.       digitalWrite(AC_LOAD, LOW);
  40.     }
  41.     if (byteRcvd == '1') {
  42.       fan = true;
  43.       speedValue = fanSpeed[0];
  44.     }
  45.     if (byteRcvd == '2') {
  46.       fan = true;
  47.       speedValue = fanSpeed[1];  
  48.     }
  49.     if (byteRcvd == '3') {
  50.       fan = true;
  51.       speedValue = fanSpeed[2];
  52.     }
  53.     dimming = speedValue;
  54.   }
  55. }
  56.  
  57. void loop() {
  58.   delay(100);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement