Issues with IRremote

Hello All,

First off I am a programming newbie so I am sure I have issues with programming convention within my code. I am having the following issues with my code.

IRreceive is not working properly. I have code that is watching for codes coming in on the IR receiver (TSOP58038). When a code is received the translateIR() function is supposed to be called. The problem is I am getting all kinds of random values within the decode results even when there is no signal from the IR receiver. I hooked a scope to the output of my IR receiver and it only changes when I press a remote button but the results.value is constantly changing even with no signal from the IR receiver. A remote button press works may 1 press out of 50 attempts. I am open to all suggestions on improvement to my code. My code exceeded the allowed characters so It is on the next two posts in order in the sketch.

#include <IRremote.h>
#include <IRremoteInt.h>
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#include <TMRpcm.h>           //  also need to include this library...

decode_results results;
const int RECV_PIN = 7; // Create object for IR Receiver pin
TMRpcm tmrpcm;   // create an object for use in this sketch
IRrecv irrecv(RECV_PIN); // create IR object
IRsend irsend;
int sStatus = 0; // Create object to store system status
int ledPin = 5; // Create object for IR Emitter
int sensorValue; // Create object to store Sensor Value 
int relayPin = 15; //create an object for relay pin


void setup(){
 irrecv.enableIRIn(); // Start the receiver 
 pinMode(relayPin, OUTPUT); // Make relay pin an output
 digitalWrite(ledPin, LOW); // Pull Emitter Pin Low
 digitalWrite(relayPin, LOW); // pull up relay pin for open relay state
 
 tmrpcm.speakerPin = 11; //11 on Mega, 9 on Uno, Nano, etc
 pinMode(ledPin, OUTPUT);  // sets the digital pin as output
 Serial.begin(9600);
 if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be 

initialized:
   Serial.println("SD fail"); 
    return;   // don't do anything more if not
  }
  Serial.println("SD INITIALIZED");
}

void loop()
{
  int sensorValue = analogRead(A0); // read the output from Proximity sensor
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage); // print out the value you read:
  Serial.print("The IR Results Value is:");
  Serial.println(results.value, HEX);
  delay(200);
  translateIR(); 
    if (sStatus == 0 && voltage >= 1.3){ //Check if system is off sensor on A0 is activated 
//and then turn everything on and play wav file
      digitalWrite(relayPin, HIGH); // Turn on Auxillary system
      sStatus = 1;
      digitalWrite(ledPin, HIGH);   // sets the LED on
      Serial.println(sStatus);
      Serial.println("Turn on");
irsend.sendNEC(0xF7C03F, 32); // TURN ON
       Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7C03F, 32); // TURN ON
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7C03F, 32); // TURN ON
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
    Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF700FF, 32); // DIM UP
    delay(500);
tmrpcm.play("Play.wav");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
 irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
    irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
    irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
     Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
irsend.sendNEC(0xF7C837, 32); 
irsend.sendNEC(0xF7C837, 32); 
     Serial.println("Finished Turning System On");
    } else if(sStatus == 1 && voltage >= 1.3){ //Check if system is on sensor is 
    // activated and led is on and then turn everything off
      sStatus = 0;
      digitalWrite(ledPin, LOW);   // sets the LED on
      Serial.println(sStatus);
      tmrpcm.play("EXIT.wav");
irsend.sendNEC(0xF7807F, 32); // DIM Down
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM Down
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM DOWN
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM DOWN
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7807F, 32); // DIM DOWN
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF7E01F, 32); // WHITE BUTTON
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF740BF, 32); // TURN OFF
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF740BF, 32); // TURN OFF
    delay(100); Serial.println("Sent NEC repeat");
irsend.sendNEC(0xF740BF, 32); // TURN OFF
    delay(100); Serial.println("Sent NEC repeat");
    digitalWrite(relayPin, LOW);
      Serial.println("Turn off");
      delay(4000);
    }
    Serial.println("NO VALUES MET");
    irrecv.resume();
  }
void translateIR() {
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
  Serial.println((results.value, HEX)); // Print out value of results of IR read decode
  Serial.println("TranslateIR has initiated");  
  
    if ((results.value) == 0xF7C03F) {   //ON BUtton
      Serial.write("ON\n");  
      irsend.sendNEC(0xF7C03F, 32); // TURN ON
       irsend.sendNEC(0xF7C03F, 32); // TURN ON
        irsend.sendNEC(0xF7C03F, 32); // TURN ON
         irsend.sendNEC(0xF7C03F, 32); // TURN ON
       irrecv.resume();
       results.value = 0;
      }
            
    else if ((results.value) == 0xF740BF) { //OFF BUTTON
      Serial.write("off\n");
      irsend.sendNEC(0xF740BF, 32); 
      irsend.sendNEC(0xF740BF, 32);
      irsend.sendNEC(0xF740BF, 32);
      irsend.sendNEC(0xF740BF, 32);
      irrecv.resume();
      results.value = 0;
       
    }
 else if ((results.value) == 0xF7807F) { // DIM DOWN BUTTON 
      Serial.write("DIM DOWN\n");
        irsend.sendNEC(0xF7807F, 32);
        irsend.sendNEC(0xF7807F, 32);
        irsend.sendNEC(0xF7807F, 32);
        irsend.sendNEC(0xF7807F, 32);
        irrecv.resume();
        results.value = 0;
    }
 else if ((results.value) == 0xF700FF) { // DIM UP BUTTON
      Serial.write("DIM UP\n");
       irsend.sendNEC(0xF700FF, 32);
       irsend.sendNEC(0xF700FF, 32);
       irsend.sendNEC(0xF700FF, 32);
       irsend.sendNEC(0xF700FF, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF720DF) { // RED BUTTON 
      Serial.write("RED BUTTON\n");
       irsend.sendNEC(0xF720DF, 32);
       irsend.sendNEC(0xF720DF, 32);
       irsend.sendNEC(0xF720DF, 32);
       irsend.sendNEC(0xF720DF, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7A05F) { //GREEN BUTTON
      Serial.write("GREEN BUTTON\n");
       irsend.sendNEC(0xF7A05F, 32);
       irsend.sendNEC(0xF7A05F, 32);
       irsend.sendNEC(0xF7A05F, 32);
       irsend.sendNEC(0xF7A05F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7609F) { //BLUE BUTTON
      Serial.write("BLUE BUTTON\n");
       irsend.sendNEC(0xF7609F, 32);
       irsend.sendNEC(0xF7609F, 32);
       irsend.sendNEC(0xF7609F, 32);
       irsend.sendNEC(0xF7609F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7E01F) { //WHITE BUTTON
      Serial.write("WHITE BUTTON\n");
       irsend.sendNEC(0xF7E01F, 32);
       irsend.sendNEC(0xF7E01F, 32);
       irsend.sendNEC(0xF7E01F, 32);
       irsend.sendNEC(0xF7E01F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF710EF) { //SECOND RED BUTTON
      Serial.write("SECOND RED BUTTON\n");
       irsend.sendNEC(0xF710EF, 32);
       irsend.sendNEC(0xF710EF, 32);
       irsend.sendNEC(0xF710EF, 32);
       irsend.sendNEC(0xF710EF, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF730CF) { //THIRD RED BUTTON
      Serial.write("THIRD RED BUTTON\n");
       irsend.sendNEC(0xF730CF, 32);
       irsend.sendNEC(0xF730CF, 32);
       irsend.sendNEC(0xF730CF, 32);
       irsend.sendNEC(0xF730CF, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF708F7) { //FOURTH RED BUTTON
      Serial.write("FOURTH RED BUTTON\n");
       irsend.sendNEC(0xF708F7, 32);
       irsend.sendNEC(0xF708F7, 32);
       irsend.sendNEC(0xF708F7, 32);
       irsend.sendNEC(0xF708F7, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF728D7) { //Fifth RED BUTTON
      Serial.write("Fifth RED BUTTON\n");
       irsend.sendNEC(0xF728D7, 32);
       irsend.sendNEC(0xF728D7, 32);
       irsend.sendNEC(0xF728D7, 32);
       irsend.sendNEC(0xF728D7, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7906F) { //SECOND GREEN BUTTON
      Serial.write("SECOND GREEN BUTTON\n");
       irsend.sendNEC(0xF7906F, 32);
        irsend.sendNEC(0xF7906F, 32);
         irsend.sendNEC(0xF7906F, 32);
          irsend.sendNEC(0xF7906F, 32);
           irsend.sendNEC(0xF7906F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7B04F) { // THIRD GREEN BUTTON
      Serial.write("THIRD GREEN BUTTON\n");
       irsend.sendNEC(0xF7B04F, 32);
       irsend.sendNEC(0xF7B04F, 32);
       irsend.sendNEC(0xF7B04F, 32);
       irsend.sendNEC(0xF7B04F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF78877) { //FOURTH GREEN BUTTON
      Serial.write("FOURTH GREEN BUTTON\n");
       irsend.sendNEC(0xF78877, 32);
       irsend.sendNEC(0xF78877, 32);
       irsend.sendNEC(0xF78877, 32);
       irsend.sendNEC(0xF78877, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7A857) { //FIFTH GREEN BUTTON
      Serial.write("FIFTH GREEN BUTTON\n");
       irsend.sendNEC(0xF7A857, 32);
       irsend.sendNEC(0xF7A857, 32);
       irsend.sendNEC(0xF7A857, 32);
       irsend.sendNEC(0xF7A857, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF750AF) { //Second Blue BUTTON
      Serial.write("Second Blue BUTTON\n");
       irsend.sendNEC(0xF750AF, 32);
       irsend.sendNEC(0xF750AF, 32);
       irsend.sendNEC(0xF750AF, 32);
       irsend.sendNEC(0xF750AF, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7708F) { //Third Blue BUTTON
      Serial.write("Third Blue BUTTON\n");
       irsend.sendNEC(0xF7708F, 32);
       irsend.sendNEC(0xF7708F, 32);
       irsend.sendNEC(0xF7708F, 32);
       irsend.sendNEC(0xF7708F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF748B7) { //Fourth Blue BUTTON
      Serial.write("Fourth Blue BUTTON\n");
       irsend.sendNEC(0xF748B7, 32);
       irsend.sendNEC(0xF748B7, 32);
       irsend.sendNEC(0xF748B7, 32);
       irsend.sendNEC(0xF748B7, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF76897) { //Fifth Blue BUTTON
      Serial.write("Fifth Blue BUTTON\n");
       irsend.sendNEC(0xF76897, 32);
       irsend.sendNEC(0xF76897, 32);
       irsend.sendNEC(0xF76897, 32);
       irsend.sendNEC(0xF76897, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7D02F) { //Flash BUTTON
      Serial.write("Flash BUTTON\n");
       irsend.sendNEC(0xF7D02F, 32);
       irsend.sendNEC(0xF7D02F, 32);
       irsend.sendNEC(0xF7D02F, 32);
       irsend.sendNEC(0xF7D02F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7F00F) { //Strobe BUTTON
      Serial.write("Strobe BUTTON\n");
       irsend.sendNEC(0xF7F00F, 32);
       irsend.sendNEC(0xF7F00F, 32);
       irsend.sendNEC(0xF7F00F, 32);
       irsend.sendNEC(0xF7F00F, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7C837) { //Music1 BUTTON
      Serial.write("Music1 BUTTON\n");
       irsend.sendNEC(0xF7C837, 32);
       irsend.sendNEC(0xF7C837, 32);
       irsend.sendNEC(0xF7C837, 32);
       irsend.sendNEC(0xF7C837, 32);
       irrecv.resume();
       results.value = 0;
    }
 else if ((results.value) == 0xF7E817) { //Music2 BUTTON
      Serial.write("Music2 BUTTON\n");
       irsend.sendNEC(0xF7E817, 32);
       irsend.sendNEC(0xF7E817, 32);
       irsend.sendNEC(0xF7E817, 32);
       irsend.sendNEC(0xF7E817, 32);
       irrecv.resume();
       results.value = 0;
    }
  }
  
  Serial.println("END");
  irrecv.resume();  
  
  }

Hi,
I would step back and use something simple to see if I could receive the IR OK.

Look at the ArduinoInfo.Info WIKI HERE:

Do you get reliable reception with IRrecvDemo ?? If not, you have other connection problems or hardware problems...

Let us know what you see...

I assume you are testing on a Standard Arduino, with ATMega328x.

First off, you need to issue a 'irrecv.enableIRIn()' after sending to be able to receive again. (Sending IR disables receiving IR).

Second, you should put a delay of say at least 40ms between each signal sent (otherwise the signal may be ignored).

Third, are you sure your code compiled, I think you may have to remove the send line (#include <IRremoteInt.h>)

Last, you have a lot of stuff bundled together, so why not remove all the SD, WAV and sensor stuff and just deal with the IR until you get that working then add in the others one by one so you can see if there are any conflicts with the other libraries.

An update on this issue. I don't know how I missed it earlier but I was getting noise on ground coming from the SHARP IR proximity sensor. After doing some investigation I found out this is a common issue. All I did was add a 6300uF cap between V+ and Ground at the proximity sensor and performance improved dramatically. I was still getting a slow response so I added another cap between V+ and Ground at the IR receiver and that cleared everything up. The sketch is now working perfectly. Thanks to all for your comments.

pleslie76:
All I did was add a 6300uF cap between V+ and Ground at the proximity sensor

That's a big cap!!

I wouldn't have used such a big cap but I read a couple other posts on the internet where someone was having the same issue and they tried smaller caps and the noise wasn't reduced enough until he put a large cap in.

Well, I'm no EE but generally, noise can be removed without resorting to such a large cap. Pullup or pulldown resistors, ferrite beads, chokes, etc in combination with two small caps (one 100nF for high-frequency noise and one 1uF to 47uF for low frequency ripple) should do the trick in most cases.

One concern with such a big cap is the inrush current when the circuit is powered on. The cap appears as a dead short until it's charged, so you may be exceeding the current specifications of your voltage regulator (if you're lucky, it's a current-limiting supply so it's not damaging itself every time you power up the circuit). Often a current-limiting resistor is added to large caps to reduce the inrush current to manageable levels.

Thanks for the advice. I will play around with different configurations and see if your suggestion works just as well (the two caps).