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_pin = 10; // Output to Opto Triac
int Dimmer_pin = 0; // Pot for testing the dimming
int LED = 3; // LED for testing
int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
int freqStep = 78; // Set the delay for the frequency of power (65 for 60Hz, 78 for 50Hz) per step (using 128 steps)
// freqStep may need some adjustment depending on your power the formula
// you need to us is (500000/AC_freq)/NumSteps = freqStep
// You could also write a seperate function to determine the freq
#include
NewSoftSerial mySerial(5,6);
int val;
int val2;
int value;
int dm=0;
int bt=0;
int of=0;
int onn=0;
int dmof=0;
int bton=0;
byte some=0;
void setup()
{
onn=1;
mySerial.begin(19200);
Serial.println("Beginning ... ");
//pinMode(pin6,INPUT);
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
pinMode(LED, OUTPUT); // Set the LED pin as output
attachInterrupt(0, zero_cross_detect, CHANGE); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection , this is where is did some modification adn used change instead of falling.
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep); // Use the TimerOne Library to attach an interrupt
}
void zero_cross_detect() { // function to be fired at the zero crossing
zero_cross = 1; // set the boolean to true to tell our dimming function that a zero cross has occured
}
void dim_check() // Function will fire the triac at the proper time
{
digitalWrite(AC_pin, LOW); // Turn off the Triac gate (Triac will not turn off until next zero cross)
if(zero_cross == 1) // First check to make sure the zero-cross has happened else do nothing
{
if(i>=dim) // Check and see if i has accumilated to the dimming value we want
{
digitalWrite(AC_pin, HIGH);// Fire the Triac mid-phase
i = 0; // Reset the accumilator
zero_cross = 0; // Reset the zero_cross so it may be turned on again at the next zero_cross_detect
} else {
i++; // If the dimming value has not been reached, incriment our counter
} // End dim check
} // End zero_cross check
}
void loop()
{
recieve();
}
void recieve()
{
if (mySerial.available())
{
val2=(int)mySerial.read();
val2=val2-48;
if(val2==1)
{
bton=0;
dimmer();
}
else if(val2==2)
{
bton=0;
brigth();
}
else if(val2==3)
{
//sendAck();
bton=0;
on();
}
else if(val2==4)
{
bton=0;
off();
}
else if(val2==5)
{
on();
bton=1;
}
else if(val2==6)
{
bton=1;
off();
}
}
}
void dimmer()
{
bt=0;
if(dm!=1)
{
dm=1;
for(int i=5;i<=115;i++)
{
dim=i;
//delay(50);
if(dim>30)
{
delay(500);
}
}
}
else
{
dim=115;
//delay(50);
}
}
void brigth()
{
dm=0;
if(bt!=1)
{
bt=1;
for(int i=115;i>=5;i--)
{
dim=i;
if(dim<90)
{
delay(400);
}
}
}
else
{
dim=5;
delay(50);
}
}
void off()
{
dm=0;bt=0;
dim=115;
delay(50);
}
void on()
{
dm=0;bt=0;
dim=5;
delay(50);
}
Inga kommentarer:
Skicka en kommentar