I have schematic:
and code:
// AC dimmer with zero-crossing detection
// First edition by Niels Oestergaard
// !!!!!!!! not verified !!!!!!!!!!!!!
// based on design Andrew Kilpatricks tutorial: Andrew Kilpatrick//strategy for dimming part of the sketch:
//
// set counting interval to reach 255 in one half-period (50 hz = 10 millis pr. half-period / 60 Hz 8.3333333333333 pr. halft periode )
// set output to 0 - 255
// detect zero crossing (on interrupt pin)
// in interrupt rutine calculate time for turning on
// when millis reaching time, turn on for a short while
// turn output off to be sure not to trigger to early in next halfperiod// interrupt 0 - (pin 2)
int dimmer1Pin = 10; //output to opto triac (MOC 3010/3020)
int ledPin = 13; //reserved for sanity check and visual feedback
volatile int freqAdjTime = 20; //set to 10 for 50 Hz, and 8 for 60 Hz
volatile long nextOnTime = 0; // variable for storing a time (in millis) for when to fire the output next
volatile int outputValue; // Varialble for storing the output value 0-255
//unsigned int inputValue; // Variable for storing input value (ie. a potentiometer)
long lastMillis; // for timing purposesvoid setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
// sanity check
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
// end of sanity check
attachInterrupt(0, zero, RISING); //Attachment of zero crossing detection, which means the interrupt rutine "zero" is called each 20 millis
}void loop()
{
//
if (nextOnTime < millis()) { //is it time to set pin on?
digitalWrite(dimmer1Pin, HIGH); // then set it
delay(2); // wait a bit to be sure the triac is on
digitalWrite(dimmer1Pin, LOW); // turn of the trigger to be sure it is not trigger in the very beginning of next halfperiod
}// A loop that slowly increases outputvalue
if (millis()-lastMillis > 250 ) { //increase output each fourth of a second
outputValue++; //increase outputvalue by one//Check if max is reached
if (outputValue > 255) { // output = maximum
outputValue = 0; // set output to zero
}
}
}void zero()
{
// set a volatile variable with the millis value + (the value of the desired output (0-255) multiplied by the freq adj time)
nextOnTime = millis() + (freqAdjTime * outputValue); //how long will this calculation take? too long to enable low output??
}
But it's not working
Please help...