- Sat Nov 28, 2015 12:05 am
#35170
Here is a light dimmer sketch for a 4 channel AC dimmer board... The sketch is for the arduino ... But it is for the same type of board I will be using with the exception of it being a 1 ch board.
Code: Select all
// Testing sketch for 50Hz !!!
#include <TimerOne.h>
unsigned char channel_1 = 7; // Output to Opto Triac pin, channel 1
unsigned char channel_2 = 6; // Output to Opto Triac pin, channel 2
unsigned char channel_3 = 5; // Output to Opto Triac pin, channel 3
unsigned char channel_4 = 4; // Output to Opto Triac pin, channel 4
unsigned char CH1, CH2, CH3, CH4;
unsigned char i=0;
unsigned int delay_time=2500; // delay ms or SPEED
unsigned char clock_tick; // variable for Timer1
void setup() {
pinMode(channel_1, OUTPUT);// Set AC Load pin as output
pinMode(channel_2, OUTPUT);// Set AC Load pin as output
pinMode(channel_3, OUTPUT);// Set AC Load pin as output
pinMode(channel_4, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
Timer1.initialize(100); // set a timer of length 100 microseconds for 50Hz or 83 microseconds for 60Hz;
Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}
void timerIsr()
{
clock_tick++;
if (CH1==clock_tick)
{
digitalWrite(channel_1, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_1, LOW); // triac Off
}
if (CH2==clock_tick)
{
digitalWrite(channel_2, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_2, LOW); // triac Off
}
if (CH3==clock_tick)
{
digitalWrite(channel_3, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_3, LOW); // triac Off
}
if (CH4==clock_tick)
{
digitalWrite(channel_4, HIGH); // triac firing
delayMicroseconds(10); // triac On propogation delay (for 60Hz use 8.33)
digitalWrite(channel_4, LOW); // triac Off
}
}
void zero_crosss_int() // function to be fired at the zero crossing to dim the light
{
// Every zerocrossing interrupt: For 50Hz (1/2 Cycle) => 10ms ; For 60Hz (1/2 Cycle) => 8.33ms
// 10ms=10000us , 8.33ms=8330us
clock_tick=0;
}
void loop() {
CH1=5; CH2=45; CH3=65; CH4=80;
delay(delay_time);
CH1=80; CH2=65; CH3=45; CH4=5;
delay(delay_time);
CH1=65; CH2=5; CH3=65; CH4=5;
delay(delay_time);
CH1=5; CH2=65; CH3=5; CH4=65;
delay(delay_time);
CH1=5; CH2=65; CH3=65; CH4=5;
delay(delay_time);
CH1=65; CH2=5; CH3=5; CH4=65;
delay(delay_time);
CH1=5; CH2=5; CH3=5; CH4=5;
delay(delay_time);
CH1=95; CH2=95; CH3=95; CH4=95;
delay(delay_time);
}
This code just dims the bulbs for a lighting show bummer I cannot find anything for say a slider but I would assume maybe somehow I could adapt the slider example to work with it and so on. As time goes on.
The board I have will be one channel curious to know if basic will be able to handle things the same etc etc... Or will basic code have to be modified to handle ZCD (Zero Cross Detection) and such anyhow just thought I would share just started doing a little research for about 30mins or so tonight so maybe by the time I get my board by the end of next month I'll be able to hook it up and try it out...
We will see I am sort of a blockhead at times I have been stuck still on figuring out a way to read the status of outputs with basic it seems as if they are static or my msg handler is not working to turn the pins on/off one or the two I am going to have to try with a buttons page to turn things on and off instead of the msg handler to see if the problem isn't just the message handler example given for basic that I based the code off of.
Anyhow slow and steady wins the race they say so I'll keep at it.