Skip to content

Commit

Permalink
Merge branch 'bcelary-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lfchavier committed Oct 29, 2015
2 parents cfa4692 + 8116e60 commit d3f9331
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 81 deletions.
8 changes: 4 additions & 4 deletions examples/DisplayLamps/DisplayLamps.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "Dimmer.h"

// Set 3 dimerable lights in ramp mode (smooth transitions) with speed 100 at pins 3,5 and 6
Dimmer lamp1(3, RAMP_MODE, 100, 50, ON);
Dimmer lamp2(5, RAMP_MODE, 100, 50, OFF);
Dimmer lamp3(6, RAMP_MODE, 100, 50, OFF);
// Set 3 dimmable lights in ramp mode (smooth transitions) with speed 100 at pins 3,5 and 6
Dimmer lamp1(3, RAMP_MODE);
Dimmer lamp2(5, RAMP_MODE);
Dimmer lamp3(6, RAMP_MODE);

// Light powers animation
#define p1 20
Expand Down
22 changes: 12 additions & 10 deletions examples/RandomLamps/RandomLamps.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,30 @@

#include "Dimmer.h"

Dimmer lamp1(3, NORMAL_MODE, 100, 50, ON);
Dimmer lamp2(5, RAMP_MODE, 100, 50, OFF);
Dimmer lamp3(6, RAMP_MODE, 100, 50, OFF);
Dimmer lamp1(3);
Dimmer lamp2(5);
Dimmer lamp3(6);

int value;

void setup() {
lamp1.begin();
lamp2.begin();
lamp3.begin();

randomSeed(analogRead(0));
}

void loop(){
value=random(0,256);
void loop() {
value = random(0, 255);
lamp1.set(value);
delay(500);
value=random(0,256);

value = random(0, 255);
lamp2.set(value);
delay(500);
value=random(0,256);
lamp2.set(value);

value = random(0, 255);
lamp3.set(value);
delay(500);
};

}
34 changes: 18 additions & 16 deletions examples/WaveLamps/WaveLamps.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@

#include "Dimmer.h"

//store dimm lights in array
Dimmer dimm[] = { Dimmer(3, RAMP_MODE, 100, 50, ON),
Dimmer(5, RAMP_MODE, 100, 50, OFF),
Dimmer(6, RAMP_MODE, 100, 50, OFF)
};
Dimmer dimm[] = {
Dimmer(3, RAMP_MODE),
Dimmer(5, RAMP_MODE),
Dimmer(6, RAMP_MODE)
};

#define p1 20
#define p2 60

byte pot[6][3] = { {p2, 0, 0 },
{p1, p1, 0 },
{0 , p2, 0 },
{0 , p1, p1},
{0 , 0, p2},
{p1, 0, p1}
};
byte pot[6][3] = {
{p2, 0, 0 },
{p1, p1, 0 },
{0 , p2, 0 },
{0 , p1, p1},
{0 , 0, p2},
{p1, 0, p1}
};

void setup() {
dimm[0].begin();
dimm[1].begin();
dimm[2].begin();
dimm[0].begin();
dimm[1].begin();
dimm[2].begin();
}

void loop(){
Expand All @@ -41,4 +43,4 @@ void loop(){
}
delay(200);
}
}
}
71 changes: 36 additions & 35 deletions src/Dimmer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This is the Dimmer library to control AC loads, including dimmable lamps.
Version 2.0 - Multi-instance objects.
Copyright (c) 2015 Circuitar
Expand All @@ -12,16 +12,16 @@

// Time table to set triacs
static int triacTime[] ={
147, 142, 139, 136, 133, 131, 129, 127, 125, 124,
122, 121, 119, 118, 116, 115, 114, 113, 112, 111,
110, 108, 107, 106, 105, 104, 103, 102, 102, 101,
100, 99, 98, 97, 96, 95, 94, 93, 93, 92,
91, 90, 89, 88, 88, 87, 86, 85, 84, 83,
83, 82, 81, 80, 79, 78, 77, 77, 76, 75,
74, 73, 72, 71, 71, 70, 69, 68, 67, 66,
65, 64, 63, 62, 61, 60, 59, 58, 57, 56,
55, 54, 53, 51, 50, 49, 48, 46, 45, 43,
42, 40, 38, 36, 34, 31, 28, 24, 19, 0
147, 142, 139, 136, 133, 131, 129, 127, 125, 124,
122, 121, 119, 118, 116, 115, 114, 113, 112, 111,
110, 108, 107, 106, 105, 104, 103, 102, 102, 101,
100, 99, 98, 97, 96, 95, 94, 93, 93, 92,
91, 90, 89, 88, 88, 87, 86, 85, 84, 83,
83, 82, 81, 80, 79, 78, 77, 77, 76, 75,
74, 73, 72, 71, 71, 70, 69, 68, 67, 66,
65, 64, 63, 62, 61, 60, 59, 58, 57, 56,
55, 54, 53, 51, 50, 49, 48, 46, 45, 43,
42, 40, 38, 36, 34, 31, 28, 24, 19, 0
};

static Dimmer* dimmmers[MAX_TRIAC]; // Pointers to all registered dimmer objects
Expand All @@ -44,12 +44,22 @@ void callTriac(){

// Timer2 compare interruption
ISR(TIMER2_COMPA_vect) {
callTriac();
callTriac();
}


// Class Constructor
Dimmer::Dimmer(uint8_t triacPin, uint8_t mode, uint8_t value, bool state, uint16_t resolution){
Dimmer::Dimmer(uint8_t triacPin, uint8_t mode, uint8_t value, bool state, uint16_t resolution) :
triacPin(triacPin),
operationMode(mode),
lampValue(value),
lampState(state),
countResolution(resolution),
pulses(0),
pulseCount(0),
msCounter(0),
rampCounter(0)
{
if (dimmerCount < MAX_TRIAC){
if (mode == RAMP_MODE){
this->lampValue = value;
Expand All @@ -59,30 +69,23 @@ Dimmer::Dimmer(uint8_t triacPin, uint8_t mode, uint8_t value, bool state, uint16
this->lampValueRamp = value;
}

this->triacPin = triacPin;
this->operationMode = mode;
this->lampState = state;
this->countResolution = resolution;
this->msCounter = 0;
this->rampCounter = 0;

// Register dimmer objects
dimmmers[dimmerCount++] = this;
}
}

bool Dimmer::begin(){
pinMode(triacPin, OUTPUT);
void Dimmer::begin(){
pinMode(triacPin, OUTPUT);

if(!started){
pinMode(zeroCrossPin, INPUT);
attachInterrupt(zeroCrossInt, callZeroCross, RISING);
started = true;
started = true;
}

if(operationMode != COUNT_MODE){
// Setup Timer2 to fire every 50us
TCCR2A = 0x02; // CTC mode
TCCR2A = 0x02; // CTC mode
TCCR2B = 0x02; // prescaler=8
TIMSK2 = 0x02; // Timer2 Compare Match Interrupt Enable
OCR2A = 99; // Compare value
Expand Down Expand Up @@ -135,33 +138,31 @@ void Dimmer::set(uint8_t value, bool state){
void Dimmer::zeroCross(){
if (operationMode == COUNT_MODE) {
if (pulses >= MSB && pulseCount > 0){ // Check the MSB bit
pulseCount--;
pulseCount--;
}

pulses = pulses << 1;

if (lampValueRamp > pulseCount * SCALE){
digitalWrite(triacPin, HIGH);
pulses++;
pulseCount++;
}
else {
} else {
digitalWrite(triacPin, LOW);
}
}
// NORMAL OR RAMP MODE
else {
} else {
// NORMAL OR RAMP MODE
// Clear counter
msCounter=0;
// Turn-off triac
digitalWrite(triacPin, LOW);
digitalWrite(triacPin, LOW);
}
}

void Dimmer::triac(){
if (operationMode != COUNT_MODE) {
msCounter++;

//With ramp mode
if (operationMode == RAMP_MODE){
rampCounter++;
Expand All @@ -175,11 +176,11 @@ void Dimmer::triac(){
}
}
}

if(lampValueRamp > 0 && lampState){
if(triacTime[lampValueRamp-1] <= msCounter){
digitalWrite(triacPin, HIGH);
}
}
}
}
}
32 changes: 16 additions & 16 deletions src/Dimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* NanoShield_zeroCross pin settings.
*
* @param zeroCrossPin Change this parameter to the pin your zero cross is attached.
* @param zeroCrossInt Change this parameter to the interruption correspondent to the pin of the zero cross.
* @param zeroCrossInt Change this parameter to the interruption correspondent to the pin of the zero cross.
* @see https://www.arduino.cc/en/Reference/attachInterrupt for more information.
*/
#define zeroCrossPin 2
Expand All @@ -53,14 +53,14 @@
* A ZeroCross Nanoshield or similar module to dimm AC lights.
*/
class Dimmer
{
{
public:
/**
* Constructor.
*
* Creates an object to access one Triac Nanoshield.
* @param triacPin pin select matching the jumper on the board. (D3, D5, etc)
* @param mode operation mode to crontrol the light.
* @param mode operation mode to crontrol the light.
* Possible modes:
* NORMAL_MODE: Uses timer to apply only a percentage of the AC power to the light.
* RAMP_MODE: Same as in normal mode, but it applies a ramp effect to the light.
Expand All @@ -71,31 +71,31 @@ class Dimmer
* @param resolution Applied only in RAMP_MODE
* Controlls the speed of the ramp when changing values.
* If resolution is 200 the lamp goes from 0% to 100% in one second.
* Maximum value: 65535. Default value is 300.
*
* Maximum value: 65535. Default value is 300.
*
* @see begin()
*/
Dimmer(uint8_t triacPin, uint8_t mode = NORMAL_MODE, uint8_t value=50, bool state = ON, uint16_t resolution = 300);
*/
Dimmer(uint8_t triacPin, uint8_t mode = NORMAL_MODE, uint8_t value = 50, bool state = ON, uint16_t resolution = 300);

/**
* Initializes the module.
*
* Initializes ZeroCross and Timer 2 interrupts. Set the light according to initial settings.
*/
bool begin();
void begin();

/**
* Turns the light OFF.
* Turns the light OFF.
*/
void off();

/**
* Turns the light ON.
* Turns the light ON.
*/
void on();

/**
* Toggles the light state.
* Toggles the light state.
*/
void toggle();

Expand Down Expand Up @@ -127,10 +127,10 @@ class Dimmer
void set(uint8_t value, bool state);

private:
uint8_t triacPin;
uint8_t operationMode;
uint16_t countResolution;
uint8_t pulseCount = 0;
uint8_t triacPin;
uint8_t pulseCount;
uint8_t lampValue;
uint8_t lampValueRamp;
bool lampState;
Expand All @@ -140,11 +140,11 @@ class Dimmer
#if BUFFER == 64
#define SCALE (100/64)
#define MSB 0x8000000000000000ULL
uint64_t pulses = 0;
uint64_t pulses;
#else
#define SCALE (100/32)
#define MSB 0x80000000U
uint32_t pulses = 0;
uint32_t pulses;
#endif

void zeroCross();
Expand All @@ -154,4 +154,4 @@ class Dimmer
friend void callZeroCross();
};

#endif
#endif

0 comments on commit d3f9331

Please sign in to comment.