Random codes received when an interrupt is enabled for zero-crossing detection #741
-
My project is to control AC appliances using the Triac dimmer circuit and infrared remote. For the prototype, I am using Arduino Mega 2560, and an LED that is being run on 6V AC. Circuit: Code: #include <IRremote.h>
/**
* Digital pin that will detect the interrupt via H11AA1.
*/
const int PIN_INTERRUPT = 2;
/**
* Digital pin that leads to MOC 3052 triac driver.
*/
const int PIN_TRIAC = 7;
/**
* Pin for IR receiving.
*/
const int PIN_IR = 35;
IRrecv irrecv(PIN_IR);
decode_results results;
int dimValue;
void setup() {
pinMode(PIN_INTERRUPT, INPUT);
pinMode(PIN_TRIAC, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(PIN_TRIAC, LOW);
digitalWrite(LED_BUILTIN, LOW);
dimValue = 50;
irrecv.enableIRIn();
Serial.begin(38400);
interrupts();
attachInterrupt(digitalPinToInterrupt(PIN_INTERRUPT), zeroCrossing, FALLING);
}
void loop() {
if (irrecv.decode(&results)) {// Returns 0 if no data ready, 1 if data ready.
if (results.value != 0xFFFFFFFF){
Serial.println(results.value, HEX);
if (results.value == 0x1FEA05F && dimValue < 100){ // For decreasing brightness of LED
dimValue += 5;
} else if (results.value == 0x1FE609F && dimValue > 50){ // For increasing brightness of LED
dimValue -= 5;
}
}
irrecv.resume(); // Restart the ISR state machine and Receive the next value
}
}
/**
* AC frequency = 50 Hz.
* So, time period one full wave = 20 ms.
* Therefore, time period of half the wave (time period from one zero crossing to another) = 10 ms = 10,000 μs.
*
* Number of steps (taken arbitraily) = 100.
* => Each step corresponds to 10,000 μs/100 = 100 μs.
* ∴ Time for dimming = (100 * dimValue) μs.
*/
void zeroCrossing(){
int delayMicros = 100 * dimValue;
delayMicroseconds(delayMicros);
digitalWrite(PIN_TRIAC, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIAC, LOW);
} Problem: The LED is lit up successfully and can be dimmed using this code, so that part is fine. If I disconnect the interrupt pin from the H11AA1 and collect the IR remote button codes, they are received fine. The codes for the two buttons of my interest are 1FE609F and 1FEA05F. As you can see, these codes are just fine; the last two HEX bytes are inverted, as expected. But if I try to detect zero crossing and receive IR remote codes, I just get random codes that have no meaning. Codes like EB88D0C4, 98FAAB77, etc. The moment I disconnect the interrupt wire coming from the H11AA1 to pin 2 of the Arduino, the codes are again received fine. I have also tried using a different pin for interrupt (like pin no. 3, 18, 19), but no success. I believe your library has something to do with interrupts. For the IR receive, I am not using an interrupt pin, and it still works fine, which means that receiving IR codes do not need interrupts. And I do not need IR send. Is it possible to prevent your library from using interrupts so that the IR codes are received in conjunction with the zero-crossing pulse from the H11AA1? I can edit the files locally with your help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Using delay in an ISR is always forbidden, except if you know what you do (blocking all other code) ! |
Beta Was this translation helpful? Give feedback.
Using delay in an ISR is always forbidden, except if you know what you do (blocking all other code) !