0

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

3
  • 2
    Do not use delay() in an interrupt. Ever.
    – Majenko
    Commented Sep 28, 2017 at 13:23
  • First of all, please use the code tag to put code here, otherwise it is not readable from the others. Then, as Majenko said, do not use 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)
    – frarugi87
    Commented Sep 28, 2017 at 14:17
  • I don't think you should be putting attachInterupt in your loop. You button may be bouncing so you could be attaching it many times each press. -- OH BTW Don't take any of these comments as criticism, don't get put off -- Commented Sep 28, 2017 at 15:21

1 Answer 1

2

First of all your if statement will always be false. You ask if 6 equals 1 (from arduino.h). It schould be digitalRead(button1) == HIGH. Also it looks like your button should start an impulse of 5ms. Am I right? In this case you will attach the Interrupt to the Button. Also don't put delays in the Interrupt function. Another thing, that isn't important in small Projects only for you, is to increase the readability by typing "define constants" uppercase, so you know where to find them and that they aren't changeable variables (For this reason HIGH, LOW, INPUT, RISING, ... are all uppercase).

All in all the code should be look like this:

#define BUTTON1PIN          6
#define TRIACPIN            5
#define TRIACPULSE          5                       // [ms]

bool doPulse = false;
long triacStartTime = 0;

void setup(){
  pinMode(BUTTON1PIN, INPUT);
  pinMode(TRIACPIN, OUTPUT);
  attachInterrupt(BUTTON1PIN, pulseISR, RISING);    // attach a Interrupt once!
                                                    // pulseISR is called everytime the button is pressed
                                                    // ISR: Interrupt service Routine (only for Readability)
}

void loop(){
  if(doPulse){
    digitalWrite(TRIACPIN, HIGH);
    triacStartTime = millis();
    doPulse = false;
  }
  if(millis() > triacStartTime + TRIACPULSE){        // Shut down after 5 ms
    digitalWrite(TRIACPIN, LOW);
  }
}

void pulseISR(){
  doPulse = true;                                    // NO delays in ISR! And as short as possible
}
3
  • Personally I find it a bit overkill to use an ISR to detect a button. Also, without any debouncing, it will give a lot of troubles. Finally, if he is using a zero-crossing optocoupler (or a solid state relay with a zero crossing optocoupler) he will have problems with 5ms times (remember that a 50Hz wave crosses the zero once per 10ms, while a 60Hz one every 8.3ms). In my opinion, the OP has not clear what he needs to do yet, so better have him figure out what to do rather than providing him solutions that will make things more complicated..
    – frarugi87
    Commented Sep 29, 2017 at 8:48
  • @frarugi87 Of course it is an overkill. But he ask for a solution for his interrupt problem and after seeing his approach i had to show the correct way of using an interrupt. I still think he might want to use some kind of PWM for his speed control, but as you stated, the timing is kind of strange for this purpose.
    – H. Puc
    Commented Sep 29, 2017 at 8:51
  • I'm actually doing a college project on controlling the speed of an AC fan, so the code I uploaded is a form of sketch I wrote to see a prototype simulation on PROTEUS before implementing it. I not going to use a button, but rather a temperature sensor. The button I used is just to force an event. I'm considering a frquency of 50Hz. And of course, the wave crosses the zero once every 10ms. so, I'm tring to achieve a 5ms delay after the crossing to obtain a reduced average power in the end. Commented Sep 29, 2017 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.