Troubles combining IRsend and IRrecv from IRremote library

Hello, this is my first post on this forum, I have an Arduino Uno rev.3 that I’ve bought some weeks ago and after having followed some tutorials I’m trying my first project to control my ceiling fan in my home with my tv remote: I’ve decoded the raw IR codes with arduino and created an array for each code, I’ve tried them individually and I can remotely control my fan from the board, now I’ve decoded some buttons on my Acer TV remote and I need to send an IR to the ceiling fan when a IR code is received from the tv remote. Below you can see my code. My problem is that the tv remote sends a lot of 0xFFFFFFFF if I keep a button pressed, but if I release the button before any of these codes is received from the arduino, the board keeps turning on and off the light very fast for a few seconds and then it stops. How can I avoid this? I’ve tried inserting a line like this after the irsend results.value = 0xFFFFFFFF; but if I use this the light gets triggered once and then my board seems to hang up. Someone could helpm me? This is driving me crazy!

#include <IRremote.h>

byte rcvPin = 2; // sets IR receiver Pin 
byte ldrPin = 0; // sets light dependent resistor Pin 
IRrecv irrecv(rcvPin); // enables IR receiving on Pin D2
decode_results results;
IRsend irsend;  // enables IR sending on Pin D3

/*Remote IR Codes:
Vortice ceiling fan:
  FanSpeed0:   0xD1D354D2
  FanSpeed1:   0xC6B51673
  FanSpeed2:   0x292CF9AD
  FanSpeed3:   0x260C914D
  FanSpeed4:   0x5D2ABE91
  FanSpeed5:   0x352AD5F8
  LightToggle: 0xB2A0D1ED
  TimerToggle: 0xA4647371
Acer TV:
  GreenE:      0x28A0F5F6
  BottomLeft:  0x36C5EBDA
  BottomRight: 0x7B782F3C
*/

// Arrays containing remote buttons IR raw codes for Vortice ceiling fan remote
unsigned int fanSpeed0[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int fanSpeed1[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed2[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed3[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed4[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed5[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int lightToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 300, 850, 250, 2500, 250, 2500, 250, 900, 250, 850, 300, 2500, 250, 2500, 250};
unsigned int timerToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250};

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
  if (irrecv.decode(&results)) {
      Serial.println(results.value, HEX);
      irrecv.resume();   // Receive the next value
  }
  switch(results.value) {
    case 0x28A0F5F6:
      Serial.println("GreenE");
      irsend.sendRaw(lightToggle, 25, 38);
      break;
    case 0x36C5EBDA:
      Serial.println("Bottom Left");
      break;
   }   
  delay(100);
}

Try this change:

#include <IRremote.h>

byte rcvPin = 2; // sets IR receiver Pin 
byte ldrPin = 0; // sets light dependent resistor Pin 
IRrecv irrecv(rcvPin); // enables IR receiving on Pin D2
decode_results results;
IRsend irsend;  // enables IR sending on Pin D3

long qqq; // store codes

/*Remote IR Codes:
Vortice ceiling fan:
  FanSpeed0:   0xD1D354D2
  FanSpeed1:   0xC6B51673
  FanSpeed2:   0x292CF9AD
  FanSpeed3:   0x260C914D
  FanSpeed4:   0x5D2ABE91
  FanSpeed5:   0x352AD5F8
  LightToggle: 0xB2A0D1ED
  TimerToggle: 0xA4647371
Acer TV:
  GreenE:      0x28A0F5F6
  BottomLeft:  0x36C5EBDA
  BottomRight: 0x7B782F3C
*/

// Arrays containing remote buttons IR raw codes for Vortice ceiling fan remote
unsigned int fanSpeed0[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int fanSpeed1[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed2[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed3[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed4[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed5[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int lightToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 300, 850, 250, 2500, 250, 2500, 250, 900, 250, 850, 300, 2500, 250, 2500, 250};
unsigned int timerToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250};

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
  if (irrecv.decode(&results)) {
      qqq=results.value;
      Serial.println(qqq, HEX);
      irrecv.resume();   // Receive the next value
  }
  switch(qqq) {
    case 0x28A0F5F6:
      Serial.println("GreenE");
      irsend.sendRaw(lightToggle, 25, 38);
      break;
    case 0x36C5EBDA:
      Serial.println("Bottom Left");
      break;
   }   
   qqq=0;
  delay(100);
}

Thank you for the answer but the problem is still the same, when qqq=0 arduino gets out of the loop but doesn't receive anymore any IR, while if I don't set qqq=0 the receiver keeps outputting the last pressed key, but this happens only when an irsend is inside a switch, I've tried creating a lot of cases without using irsend and they all work well, so I think this could be a problem with the IR library... any ideas?

try moving "qqq=0" to within the case-sentence..

I've tried putting it inside both cases of the switch, in the case that doesn't use irsend works like a charm, in the case using irsend it gets stucked, as soon as irsend is triggered the receiver doesn't work anymore, I've tried debugging the program using various Serial.println to see if it gets stucked inside the switch, but the loop always works, the problem is the receiver that doesn't works anymore if I use irsend after having received a code. There's a way to turn the receiver on and off? So I could receive the code and stop the receiver if is one of the codes that I need, use irsend and then restart the receiver... but I don't know if the library allows this

I am not very familiar with the library...
but I'd try to move (copy) the "irrecv.resume();" to additional locations

Problem solved! After using irsend the ir receiver must be reenabled! Adding irrecv.enableIRIn(); before the case break solves everything, now it works like a charm!

#include <IRremote.h>

byte rcvPin = 2; // sets IR receiver Pin 
byte ldrPin = 0; // sets light dependent resistor Pin 
IRrecv irrecv(rcvPin); // enables IR receiving on Pin D2
decode_results results;
IRsend irsend;  // enables IR sending on Pin D3

/*Remote IR Codes:
Vortice ceiling fan:
  FanSpeed0:   0xD1D354D2
  FanSpeed1:   0xC6B51673
  FanSpeed2:   0x292CF9AD
  FanSpeed3:   0x260C914D
  FanSpeed4:   0x5D2ABE91
  FanSpeed5:   0x352AD5F8
  LightToggle: 0xB2A0D1ED
  TimerToggle: 0xA4647371
Acer TV:
  GreenE:      0x28A0F5F6
  BottomLeft:  0x36C5EBDA
  BottomRight: 0x7B782F3C
*/

// Arrays containing remote buttons IR raw codes for Vortice ceiling fan remote
unsigned int fanSpeed0[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int fanSpeed1[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed2[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed3[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 2500, 250};
unsigned int fanSpeed4[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250};
unsigned int fanSpeed5[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250};
unsigned int lightToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 300, 850, 250, 2500, 250, 2500, 250, 900, 250, 850, 300, 2500, 250, 2500, 250};
unsigned int timerToggle[25] = {
  250, 900, 250, 900, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250, 2500, 250, 2500, 250, 900, 250, 900, 250, 2500, 250};

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
  if (irrecv.decode(&results)) {
      Serial.println(results.value, HEX);
      irrecv.resume();   // Receive the next value
  }
  switch(results.value) {
    case 0x28A0F5F6:
      Serial.println("GreenE");
      irsend.sendRaw(lightToggle, 25, 38);
      //results.value = 0xFFFFFFFF;
      irrecv.enableIRIn();
      break;
    case 0x36C5EBDA:
      Serial.println("Bottom Left");
      //results.value = 0xFFFFFFFF;
      break;
   }   
  results.value = 0xFFFFFFFF; 
  delay(100);
}