Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PinChangeInterrupt.h"
- #include "TinyWireS.h"
- #define I2C_SLAVE_ADDR 2
- int AC_LOAD = 3;
- int ZCD_Pin = 1;
- int dimming = 128;
- int Speed[3] = {90, 50, 20};
- int speedValue;
- bool fan;
- bool isr_;
- void setup() {
- pinMode(AC_LOAD, OUTPUT);
- TinyWireS.begin(I2C_SLAVE_ADDR);
- TinyWireS.onReceive(receiveEvent);
- pinMode(ZCD_Pin, INPUT_PULLUP);
- }
- void zero_crosss_int() //function to be fired at the zero crossing to dim the light
- {
- isr_ = true;
- }
- void receiveEvent(uint8_t howMany)
- {
- byte byteRcvd = 0;
- if (TinyWireS.available()) {
- byteRcvd = TinyWireS.receive(); // get the byte from master
- if (byteRcvd == '0') {
- fan = false;
- detachPinChangeInterrupt(digitalPinToPCINT(ZCD_Pin));
- digitalWrite(AC_LOAD, LOW);
- }
- if (byteRcvd == '1') {
- fan = true;
- speedValue = Speed[0]; //90
- attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
- }
- if (byteRcvd == '2') {
- fan = true;
- speedValue = Speed[1]; //50
- attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
- }
- if (byteRcvd == '3') {
- fan = true;
- speedValue = Speed[2]; //20
- attachPCINT(digitalPinToPCINT(ZCD_Pin), zero_crosss_int, FALLING);
- }
- dimming = speedValue;
- }
- }
- void loop() {
- if (isr_) {
- long int dimtime;
- dimtime = (75 * dimming); // For 60Hz =>65
- delayMicroseconds(dimtime); // Wait till firing the TRIAC
- digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC
- delayMicroseconds(100); // triac On propogation delay // (for 60Hz use 8.33) Some Triacs need a longer period
- digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement