Advertisement
babyyoda_

Slave_a

Jul 1st, 2021
214
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.  
  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.   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 = Speed[0];
  44.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  45.     }
  46.     if (byteRcvd == '2') {
  47.       fan = true;
  48.       speedValue = Speed[1];  
  49.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  50.     }
  51.     if (byteRcvd == '3') {
  52.       fan = true;
  53.       speedValue = Speed[2];
  54.       attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, RISING);
  55.     }
  56.     dimming = speedValue;
  57.   }
  58. }
  59.  
  60. void loop() {
  61.   delay(100);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement