I'm just learning how to use the Arduino Uno and I'm trying to control the speed of an AC fan. I made a sketch of this project on proteus but the triac seems not to be responding. I guess there's a problem with the attachInterrupt part of the code. Here's the code:
#define triacPulse 5
#define interruptPin 2
//button event
#define button1 6
void setup() {
// put your setup code here, to run once:
pinMode(button1, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(button1==HIGH){
attachInterrupt(0, triacControl, FALLING);
}
}
void triacControl()
{
delay(5); //delay for 5 milliseconds
digitalWrite(triacPulse, HIGH);
delay(5);
digitalWrite(triacPulse, LOW);
}
Thanks for the answer in advance
delay()
in an interrupt. Ever.delay()
in an interrupt. Ever. And if you ever find an application where you should use delays in an interrupt, you are wrong and you should not use it. As for the program, what do you want to do? In any case the code you wrote is not correct, since it is not the correct way to think interrupts. And finally, what do you expect by turning off and on a triac for 5ms? Usually you turn it on or off for a whole semi-period (but it should be synchronized)