I am making use of the code below to control the speed of an AC fan and the intensity of an AC Lamp with an Uno.
The problem I am having is that, when I set the lamp intensity to low or medium, the fan turns on at a very low speed as well, and I cannot turn it off even when setting the fan to off.
Also if I turn the lamp on and then off, the fan (which is still set as off), will spin slowly for a second or two in random intervals.
For troubleshooting purposes, I did separate this program into two programs, controlling each device and its circuit separately, and it works perfectly, but when I combine them, I get the above problem.
Any assistance in troubleshooting or ideas to solve this would be appreciated. Haven't attempted AC control before.
int PWM = 3; int FAN = 4; int dimming; void setup() { Serial.begin(9600); Serial.println("Serial connection started, waiting for instructions…n0 = Offn1 = 25%n2 =50%n3 = 75%n4 = 100%"); pinMode(PWM, OUTPUT); pinMode(FAN, OUTPUT); attachInterrupt(0, zero_crosss_int, RISING); } void zero_crosss_int() // function to be fired at the zero crossing to dim the light { int dimtime = (75 * dimming); // For 60Hz =>65 delayMicroseconds(dimtime); // Off cycle digitalWrite(FAN, HIGH); // triac firing delayMicroseconds(10); // triac On propogation delay //(for 60Hz use 8.33) digitalWrite(FAN, LOW); // triac Off } void loop () { if (Serial.available()) { char ser = Serial.read(); //read serial as a character switch (ser) { /* Lamp Control */ case '0': analogWrite(PWM, 0); //lamp Off break; case '1': analogWrite(PWM, 50); //lamp low break; case '2': analogWrite(PWM, 130); //lamp medium break; case '3': analogWrite(PWM, 255); //lamp high break; /* Fan Control */ case '4': dimming = 128; //fan off break; case '5': dimming = 90; //fan low break; case '6': dimming = 65; //fan medium break; case '7': dimming = 35; //fan high break; default: Serial.println("Invalid entry"); } } } The circuits it controls is based on these :
Also, these circuits and code are based on these post, if it may interest you:
Lamp PWM AC Lamp
Fan Dimmer AC Fan


dimmingshould be volatile.