diff --git a/README.rst b/README.rst index 954065f..f062071 100644 --- a/README.rst +++ b/README.rst @@ -1,14 +1,12 @@ Dimmer Library for Arduino ============== -This is an Arduino software library to control AC loads using triacs and a zero cross detector circuit. The library methods can be used to control the AC load power for multiple triacs independently, using a single shared zero-cross circuit. +This is an Arduino software library to control AC loads using triacs and a zero and non zero cross detector circuit. The library methods can be used to control the AC load power for multiple triacs independently, using a single shared zero-cross circuit. * Source code: https://github.com/circuitar/Dimmer * Documentation: http://dimmer.readthedocs.org/ * Reference Board: `Triac Nanoshield`_ and `Zero Cross Nanoshield`_ from Circuitar_ -There are different ways to implement zero cross detector circuits. This library is based on the implementation above, but it can be easily adapted to use any type of zero cross detector circuit. - To install, just click **Download ZIP** and install it using **Sketch > Include Library... > Add .ZIP Library** in the Arduino IDE. The following examples_ are provided: diff --git a/src/Dimmer.cpp b/src/Dimmer.cpp index c2ca3ce..b6a5aab 100644 --- a/src/Dimmer.cpp +++ b/src/Dimmer.cpp @@ -110,10 +110,17 @@ ISR(TIMER_COMPA_VECTOR(DIMMER_TIMER)) { *triacPinPorts[i] |= triacPinMasks[i]; } } -} + if (zeroCrossedTriac) { + delayMicroseconds(10); + // Turn off all triacs + for (uint8_t i = 0; i < dimmerCount; i++) { + *triacPinPorts[i] &= ~triacPinMasks[i]; + } + } + } // Constructor -Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq) : +Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq,bool zero_crossed_triac) : triacPin(pin), operatingMode(mode), lampState(false), @@ -125,7 +132,8 @@ Dimmer::Dimmer(uint8_t pin, uint8_t mode, double rampTime, uint8_t freq) : pulsesLow(0), pulseCount(0), pulsesUsed(0), - acFreq(freq) { + acFreq(freq), + zeroCrossedTriac(zero_crossed_triac) { if (dimmerCount < DIMMER_MAX_TRIAC) { // Register dimmer object being created dimmerIndex = dimmerCount; @@ -157,7 +165,7 @@ void Dimmer::begin(uint8_t value, bool on) { TCCRxA(DIMMER_TIMER) = TCCRxA_VALUE; // Timer config byte A TCCRxB(DIMMER_TIMER) = TCCRxB_VALUE; // Timer config byte B TIMSKx(DIMMER_TIMER) = 0x02; // Timer Compare Match Interrupt Enable - OCRxA(DIMMER_TIMER) = 100 * 60 / acFreq - 1; // Compare value (frequency adjusted) + OCRxA(DIMMER_TIMER) = 100 * 50 / acFreq - 1; // Compare value (frequency adjusted) started = true; } diff --git a/src/Dimmer.h b/src/Dimmer.h index 1e7f9c1..3527392 100644 --- a/src/Dimmer.h +++ b/src/Dimmer.h @@ -62,10 +62,11 @@ class Dimmer { * COUNT_MODE: Counts AC waves and applies full half cycles from time to time. * @param rampTime time it takes for the value to rise from 0% to 100% in RAMP_MODE, in seconds. Default 1.5. @see setRampTime(). * @param freq AC frequency, in Hz. Supported values are 60Hz and 50Hz, use others at your own risk. + * @param zero_crossed_triac Zero crossing triac (true), non zero crossing track (false) * * @see begin() */ - Dimmer(uint8_t pin, uint8_t mode = DIMMER_NORMAL, double rampTime = 1.5, uint8_t freq = 60); + Dimmer(uint8_t pin, uint8_t mode = DIMMER_NORMAL, double rampTime = 1.5, uint8_t freq = 60, bool zero_crossed_triac = false); /** * Initializes the module. @@ -152,6 +153,7 @@ class Dimmer { uint8_t pulsesUsed; uint64_t pulsesHigh; uint64_t pulsesLow; + bool zeroCrossedTriac; void zeroCross();