This code for a dimming circuit (Zero cross detection Triac)
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 dimming = 3; // dimming level (0-100)
unsigned char i;
unsigned char flag=0;
unsigned char half;void setup() {
// put your setup code here, to run once:
pinMode(channel_1, OUTPUT);// Set AC Load pin as output
pinMode(channel_2, OUTPUT);// Set AC Load pin as output
pinMode(10, INPUT);
attachInterrupt(1, light, RISING);}
void light() {
if (Serial.available()) {
dimming = Serial.read();
if (dimming < 1) { //Turn TRIAC completely OFF if dimming is 0
digitalWrite(channel_1, LOW);
}
if (dimming > 254) { //Turn TRIAC completely ON if dimming is 255
digitalWrite(channel_1, HIGH);
}
}
if (dimming > 0 && dimming < 255) { //dimming part, if dimming is not 0 and not 255
delayMicroseconds(27*(255-dimming));
digitalWrite(channel_1, HIGH);
delayMicroseconds(500);
digitalWrite(channel_1, LOW);
dimming = 250;}
}void loop() {
// put your main code here, to run repeatedly:int half = digitalRead(10);
if (half == HIGH); {
dimming = 100;
}}
At the end if statement where it is seeing if 10 is high, it is supposed to set dimming to 100 at that point, But if I change that value it changes the dimming, no matter if the pin is high or not? Not too sure why.