This is the code that I'm using:
int AC_pin = 13;
byte dim = 0;
int state;
void setup() {
Serial.begin(9600);
pinMode(AC_pin, OUTPUT);
attachInterrupt(0, light, FALLING);
}
void light() {
if (Serial.available()) {
dim = Serial.read();
if (dim == 48) {
digitalWrite(AC_pin, LOW);
}
if (dim == 49) {
state = 45;
}
if (dim == 50) {
state = 80;
}
if (dim == 51) {
state = 120;
}
if (dim == 52) {
state = 180;
}
if (dim == 53) {
digitalWrite(AC_pin, HIGH);
}
}
if (dim > 48 && dim < 53) {
delayMicroseconds(34*(255-state));
digitalWrite(AC_pin, HIGH);
delayMicroseconds(500);
digitalWrite(AC_pin, LOW);
}
}
void loop() {
}