Ir remote lighting a led

hey! I'm having trouble getting this IR sensor and remote set working can anyone please help me? I have an IR sensor hooked up to an LED so when I press a button it'll light up the LED. but it isn't working.

`// SPDX-FileCopyrightText: Limor Fried for Adafruit Industries
//
// SPDX-License-Identifier: MIT

/* Raw IR decoder sketch!
 
 This sketch/program uses the Arduno and a PNA4602 to 
 decode IR received. This can be used to make a IR receiver
 (by looking for a particular code)
 or transmitter (by pulsing an IR LED at ~38KHz for the
 durations detected 
 
 Check out www.ladyada.net and adafruit.com for more tutorials! 
 */
 
// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN      PIND
#define IRpin          2
 
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
 
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20 
 
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing
 
void setup(void) {
  Serial.begin(9600);
  Serial.println("Ready to decode IR!");
}
 
void loop(void) {
  uint16_t highpulse, lowpulse;  // temporary storage timing
  highpulse = lowpulse = 0; // start out with no pulse length
  
  
//  while (digitalRead(IRpin)) { // this is too slow!
    while (IRpin_PIN & (1 << IRpin)) {
     // pin is still HIGH
 
     // count off another few microseconds
     highpulse++;
     delayMicroseconds(RESOLUTION);
 
     // If the pulse is too long, we 'timed out' - either nothing
     // was received or the code is finished, so print what
     // we've grabbed so far, and then reset
     if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  // we didn't time out so lets stash the reading
  pulses[currentpulse][0] = highpulse;
  
  // same as above
  while (! (IRpin_PIN & _BV(IRpin))) {
     // pin is still LOW
     lowpulse++;
     delayMicroseconds(RESOLUTION);
     if ((lowpulse >= MAXPULSE)  && (currentpulse != 0)) {
       printpulses();
       currentpulse=0;
       return;
     }
  }
  pulses[currentpulse][1] = lowpulse;
 
  // we read one high-low pulse successfully, continue!
  currentpulse++;
}
 
void printpulses(void) {
  Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
  for (uint8_t i = 0; i < currentpulse; i++) {
    Serial.print(pulses[i][0] * RESOLUTION, DEC);
    Serial.print(" usec, ");
    Serial.print(pulses[i][1] * RESOLUTION, DEC);
    Serial.println(" usec");
  }
  
  // print it in a 'array' format
  Serial.println("int IRsignal[] = {");
  Serial.println("// ON, OFF ");
  for (uint8_t i = 0; i < currentpulse-1; i++) {
    //Serial.print("\t"); // tab
    Serial.print("pulseIR(");
    Serial.print(pulses[i][1] * RESOLUTION , DEC);
    Serial.print(");");
    Serial.println("");
    //Serial.print("\t");
    Serial.print("delayMicroseconds(");
    Serial.print(pulses[i+1][0] * RESOLUTION , DEC);
    Serial.println(");");
 
  }
  //Serial.print("\t"); // tab
  Serial.print("pulseIR(");
  Serial.print(pulses[currentpulse-1][1] * RESOLUTION, DEC);
  Serial.print(");");
 
}`

Post an annotated schematic showing exactly how you wired this along with links to technical information on each of the hardware items you are using. Many times IR signals are modulated of which there are several methods.

It's very early and I am old, but does not this code reset the highpulse and lowpulse to 0 every pass through the loop? Should those first two lines be global?

And some further observations/questions:

  • I did not find any statements that control a Led in your code. How is the Led (visible Light) connected?
  • There is a function to print to the Serial port. Did you see anything printed while you used the sketch?
  • Do you want to light an Led everytime pulses arrive or just when a certain command comes in? Your sketch directly measures the changes of the pin state and prints the pulse and gap durations via Serial. Any reasons for not using one of the standard IRRemote libraries that handle standard IR protocols and make live (usually) easier?

Finally: The sketch does what the code has been written for, see here
([Edit:] Almost what it was written for: see my post #5 :wink: )

https://wokwi.com/projects/445690176906130433

Just a simple test setup ...

To see what you could do using a library you may watch this

https://wokwi.com/projects/408236279940409345

ec2021

Hi @sonofcy!

They are just variables to measure the duration of pulses and gaps based on delayMicroseconds(RESOLUTION ) units ....

Though it works the sketch seems to be in a "wrong order" regarding the Serial printout:

  • The first print loop should probably provide an array of ON/OFF values and the second loop prints single statements for pulses and gaps

Here an example:

Received: 

OFF 	ON
19552 usec, 9260 usec
4640 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 560 usec
1720 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
1720 usec, 560 usec
1720 usec, 540 usec
1740 usec, 540 usec
540 usec, 540 usec
540 usec, 560 usec
520 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
540 usec, 540 usec
1740 usec, 540 usec
1740 usec, 540 usec
int IRsignal[] = {
// ON, OFF 
pulseIR(9260);
delayMicroseconds(4640);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(560);
delayMicroseconds(1720);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1720);
pulseIR(560);
delayMicroseconds(1720);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(560);
delayMicroseconds(520);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(540);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);
delayMicroseconds(1740);
pulseIR(540);

Feel free to check out the Wokwi link I prepared ... It's sometimes easier to analyse the code in the Wokwi browser than here between code tags ... I know what I'm talking about :wink:

Regards
ec2021

I think you missed my point, placing the code at the top of the loop causes highpulse and lowpulse to be set to ZERO each pass of the loop. Make those two lines in loop global and see what happens.

The source for the code seems to be this page

https://learn.adafruit.com/ir-sensor/reading-ir-commands?view=all

No offense, but it's based on two while loops within loop() that increment either highpulse or lowpulse in units of (RESOLUTION microseconds). If you remove the resetting of the variables at the beginning of loop(), they'll never be reset to zero again, resulting in an overflow after a few iterations. You can easily test it on Wokwi ...

The code is only a "low level" decoder ...

It's still unclear when and how the TO wants to control a separate LED ...

@buds111
Which Arduino are you using?