Hello, this may be a dumb question, but I have written a code to run an ISR anytime there is a state change using attachinterrupt. My problem is that when my ISR is executing, any incoming IR transmission is ignored, which is non ideal for this project. Any insight? My code is a mess so far but I'll post it anyways. Thanks guys
#include <IRremote.h>
int pin = (6,4,5);//changed to 6 because of interference
volatile int state = LOW;
long RAndNumber;
long randNumber;
long RandNumber;
int bright =255;
int timingOne = 2000;
int timingTwo = 1000;
int happy;
int RECV_PIN = 11;
int BUTTON_PIN = 12;
int STATUS_PIN = 13;
IRrecv irrecv(RECV_PIN);
IRsend irsend;
decode_results results;
void setup() {
// initialize serial communication:
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(BUTTON_PIN, INPUT);
pinMode(STATUS_PIN, OUTPUT);
}
// Storage for the recorded code
int codeType = -1; // The type of code
unsigned long codeValue; // The code value if not raw
unsigned int rawCodes[RAWBUF]; // The durations if raw
int codeLen; // The length of the code
int toggle = 0; // The RC5/6 toggle state
// Stores the code for later playback
// Most of this code is just logging
void storeCode(decode_results *results) {
codeType = results->decode_type;
int count = results->rawlen;
if (codeType == UNKNOWN) {
Serial.println("Received unknown code, saving as raw");
}
else {
if (codeType == NEC) {
Serial.print("Received NEC: ");
if (results->value == REPEAT) {
// Don't record a NEC repeat value as that's useless.
Serial.println("repeat; ignoring.");
return;
}
Serial.println(results->value, HEX);
codeValue = results->value;
codeLen = results->bits;
if(results->value == 16732335)
happy = 1;
else if(results->value == 16720605)
happy = 0;
}}
if(happy == 1)
attachInterrupt(0, blink, CHANGE);//pin2UNO
attachInterrupt(0, test, CHANGE);//pin2UNO
if(happy == 0)
detachInterrupt(0);
if(results->value == 16762935)
timingOne = timingOne +=2000;
timingTwo = timingTwo +=2000;
if(results->value == 16771095)
timingOne = timingOne -=2000;
timingTwo = timingTwo -=2000;
if(results->value == 16726725)
bright = bright +=25.5;
if(results->value == 16759365)
bright = bright -=25.5;
bright = constrain(bright,150,255);
}
void sendCode(int repeat) {
if (codeType == NEC) {
if (repeat) {
irsend.sendNEC(REPEAT, codeLen);
Serial.println("Sent NEC repeat");
}}}
int lastButtonState;
void loop() {
digitalWrite(pin, state);
if(happy == 0)
detachInterrupt(0);
// If button pressed, send the code.
int buttonState = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && buttonState == LOW) {
Serial.println("Released");
irrecv.enableIRIn(); // Re-enable receiver
}
else if (irrecv.decode(&results)) {
digitalWrite(STATUS_PIN, HIGH);
storeCode(&results);
irrecv.resume(); // resume receiver
digitalWrite(STATUS_PIN, LOW);
}
lastButtonState = buttonState;
Serial.println(happy);
}
void blink()
{
randNumber = random(4,7);
RandNumber = random(130,255);
RAndNumber = random(100,5500);
analogWrite(randNumber, RandNumber);
delay(timingOne);
(digitalWrite(randNumber, LOW));
}
void test(){
randNumber = random(4,7);
RandNumber = random(130,255);
RAndNumber = random(100,5500);
analogWrite(randNumber, RandNumber);
delay(timingTwo);
(digitalWrite(randNumber, LOW));
}