#include <TimerOne.h>
volatile int i=0; // Variable to use as a counter
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_LOAD = 3; // Output to Opto Triac
int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
int inc=1; // counting up or down, 1=up, -1=down
const int upPin = A1; //button for brightnes up
const int downPin = A2;
int up; //brightnes up button reading
int down;
int lastCounter = 1;
int counter;
int freqStep = 65; // This is the delay-per-brightness step in microseconds.
// It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
// and the number of brightness steps you want.
//
// The only tricky part is that the chopper circuit chops the AC wave twice per
// cycle, once on the positive half and once at the negative half. This meeans
// the chopping happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply.
// To calculate freqStep you divide the length of one full half-wave of the power
// cycle (in microseconds) by the number of brightness steps.
//
// (1000000 uS / 120 Hz) / 128 brightness steps = 65 uS / brightness step
//
// 1000000 us / 120 Hz = 8333 uS, length of one half-wave.
void setup() {
pinMode(up, INPUT);
pinMode(down, INPUT); // Begin setup
pinMode(AC_LOAD, OUTPUT); // Set the Triac pin as output
attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep);
}
void zero_cross_detect() {
zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
i=0;
digitalWrite(AC_LOAD, LOW); // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
if(zero_cross == true) {
if(i>=dim) {
digitalWrite(AC_LOAD, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}
void loop() {
counter = lastCounter;
up = digitalRead(upPin); //button pins reading
down = digitalRead(downPin);
if (up == HIGH) { //if the up button is pressed
counter = counter++; //counter increases by one
}
if (down == HIGH) { //if down button is pressed
counter = counter--; //counter decreases by one
}
if (counter > 5){ //check that counter won't get further than 5
counter = 5;
}
if (counter < 2){ //check that counter won't fall bellow 1
counter = 1;
}
switch (counter){ //depending on counter value the propper PWM value is sent to the leds
case 1:
dim = 128;
break;
case 2:
dim = 100;
break;
case 3:
dim = 75;
break;
case 4:
dim = 50;
break;
case 5:
dim = 0;
break;
}
analogWrite(AC_LOAD, dim);
lastCounter = counter; //counter recylculation
delay(500);
}
Andrew, I suspect you did not write that code because if you knew enough to write those comments you would know enough to fix it.
So to find out what you have done wrong we need to know how you have wired things up.