Noob Q about Infrared and LED

Hello good people,

I'm a beginner at Arduino, we have to start somewhere.
I found Nick Gammons code very helpful to understand how to work with push buttons and blinking leds.
Gammon Forum : Electronics : Microprocessors : Switches tutorial in particular any of the turn indicator samples.
My next quest would be to turn the same code in to InfraRed + Remote.

I've been messing with it as far as my peanut allows me to at the moment.
But I haven't succeded and have reached my patience limit. Better ask than stare.

The existing code works fine with push buttons.
But when adding the IRremote.h, etc.. I run into issues.

I get no errors when compiling.
But when using the IR. All buttons do the same.
They light up 2 leds constantly untill random remote button is pushed again.
I have defined back pause and next buttons.

I've made another code and I can turn on each of the leds independetly, via digitalWrite, so I presume the IR functions.

Can someone lift me over my current limitation so I can understand it?

So I didn't like the transition from left <> right
from Motorcycle turn indicator with warning function
and
Replaced part with part from
Motorcycle turn indicator with brake light.
Separated each function for each push button.

Then I wanted to add Infrared remote and got stuck.
The code has junk in it from trying whatever came to mind.
So exuse me :slight_smile:

This is what I have so far:

Thank you, enjoy the lovely summer sunday.

FAILED IR code (turns only channel 9 and 10 on with any button on the remote:

[DELETED CODE]

IR Serial Monitor Codes:
Hi-Fi Remote button codes

[DELETED]

Thank you in advanced.

Working code for 3 push buttons (noIR)

[DELETED CODE]

In the failed IR sketch I see where you read the IR and print (value variable) it but I don't see where you do anything with the value variable.

Thanks for the quick reply.

Perhaps an example of something similar?
I've been trying out different things from other codes but no success.
It's not clicking in my mind.

Are you seeing the button codes in serial monitor when you run the failed IR sketch and press buttons on the remote? Are they the same as the codes listed in the original post?

Make the data type of the value variable unsigned long and try again. The unsigned int data type can hold from 0 to 65535, the code numbers are much larger.

Once you have the key pressed code (in value variable) use a switch statement or if, else if to apply an action according to the key pressed.

groundFungus:
Are you seeing the button codes in serial monitor when you run the failed IR sketch and press buttons on the remote? Are they the same as the codes listed in the original post?

Make the data type of the value variable unsigned long and try again. The unsigned int data type can hold from 0 to 65537, the code numbers are much larger.

Once you have the key pressed code (in value variable) use a switch statement or if, else if to apply an action according to the key pressed.

Yes the data from serial monitor is posted in 1st post.
I only use:
--->16753245
4294967295

Number 4294967295 comes after any press and is not used.

Yet 16753245 is longer than your 5 'digits'.

I've followed a simple youtube video to read the code send in Serial monitor.


groundFungus:
In the failed IR sketch I see where you read the IR and print (value variable) it but I don't see where you do anything with the value variable.

Changed code:
[DELETED CODE]

Here an image of my simple setup:

As one can see it only turns the PIN 9 and 10 LEDS on and off via remote push.

It doesn't follow the stuff above.

I'm stuck at that.

Put a Serial.print right before the switch statement to see what is actually passed to the switch.

groundFungus:
Put a Serial.print right before the switch statement to see what is actually passed to the switch.

BTW THANK YOU FOR YOUR TIME!!! Much apreciated!

I'll do that now.

I ran this sketch/code to obtain the IR signals:

[DELETED CODE]

groundFungus:
Put a Serial.print right before the switch statement to see what is actually passed to the switch.

Not sure if done right but this is serial monitor from the FAIL with mod void loop so only 3 buttons respond.

From (serial monitor) back to play to next
[DELETED]

So what is passed to the switch statement in the last code that you posted?

groundFungus:
So what is passed to the switch statement in the last code that you posted?

Sorry for my absence.
[DELETED]

So i did serial.print and serial println

See attached pictures

I hope I'm doing what you ask me to
[DELETED CODE]

No one willing to help? Or everyone tired of helping (in this case, my apologies)?

#define LH_SWITCH_PIN 765 // code received from button no. 1 / back
#define RH_SWITCH_PIN 8925 // code received from button no. 2 / pause
#define ALARM_SWITCH_PIN 49725 // code received from button no. 3 / next

When I look at those NAMES, I expect to see pin numbers as the values. Which Arduino has a pin numbered 49725?

If those are not pin numbers, then the names just plain suck.

I'm too lazy to try to decipher code with meaningless names.

PaulS:

#define LH_SWITCH_PIN 765 // code received from button no. 1 / back

#define RH_SWITCH_PIN 8925 // code received from button no. 2 / pause
#define ALARM_SWITCH_PIN 49725 // code received from button no. 3 / next



When I look at those NAMES, I expect to see pin numbers as the values. Which Arduino has a pin numbered 49725?

If those are not pin numbers, then the names just plain suck.

I'm too lazy to try to decipher code with meaningless names.

[DELETED]

Can we forget about the LEDs and switches and just get the IR remote to work?

Here is tested (Uno IDE ver. 1.8.5) code to demonstrate choosing an action with a switch statement using an IR remote key. Replace the codes with the codes that you have. Use the full 32 bit codes. I prefer HEX codes but your decimal codes should work the same. Note the starred (*****) parts.

#include <IRremote.h>

int RECV_PIN = 4;  // ****** replace with the pin of your choice or connect IR decoder to pin 4

// IR remote button codes  *******replace these with your codes******
const unsigned long UP = 0x4EB322DD;
const unsigned long DOWN =  0x4EB3B847;
const unsigned long LEFT = 0x4EB338C7;
const unsigned long RIGHT = 0x4EB312ED;


IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
   Serial.begin(115200);  //  ***** make sure serial monitor baud matches
   irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
   if (irrecv.decode(&results))
   {
      //Serial.println(results.value, HEX);  //******* uncomment to see what actally goes to the switch

      switch (results.value)
      {
         case UP:
            Serial.println(" up ");
            break;
         case DOWN:
            Serial.println(" down ");
            break;
         case LEFT:
            Serial.println(" left ");
            break;
         case RIGHT:
            Serial.println(" right ");
            break;
         case 0xFFFFFFFF:  // repeat code
            irrecv.resume(); // Receive the next value
            return;
            
         default:
            Serial.println("***  unknown key  ***");
      }

      irrecv.resume(); // Receive the next value
   }
}

I'll clue you in one something. You have WAY too much code for someone that is struggling so hard.

Dump that code. Start over. Write a sketch that does NOTHING more than read the IR, and write the value to the serial port.

void loop()
{
  if (irrecv.decode(&results))
  {
     Serial.print("IR value: ");
     Serial.println(results.value);

     irrecv.resume(); //Receive the next value
  }
}

Then, pay attention to the values being printed.

#define code1 16712445 // code received from button no. 1 / back
#define code2 16720605 // code received from button no. 2 / pause
#define code3 16761405 // code received from button no. 3 / next
#define code4 16754775 // code received from button no. 4 / minus

Do you really think that 16712445 will fit in an unsigned int? Not on most Arduinos.

RTFM, and you'll see that the value member of the results struct is an unsigned long, NOT an unsigned int.

You could observe that there is no reason to make a copy of the value, anyway, as my code shows.

So, then, you can add the switch/case statements, again, do the MINIMUM amount.

  switch(results.value)
   {
      case code1:
         Serial.println("Button 1 was pressed");
         break;
   }

Of course, you will add the other case statements, now or later.

Then, you replace, or supplement, the Serial.print() statement in each case with the real code you want to use.

Thank you, with my the tail between my legs, I'll go at it again. Hope it clicks to get the ahhh moment...
I'll report later.

[DELETED]

Just to be sure I'll swap the remote.

That really is NOT the problem.

The real problem, in the last code you posted, is that results.value is an unsigned long. Storing that value in an unsigned int is going to loose bits. Then, of course, none of the case statements will ever be true.

Thank you, with my the tail between my legs

That's not (quite) the right attitude. In the same way that you wouldn't build a house by starting with roof, you need to get the basics of the program working first, as I suggested, before adding on anything.

"My remote doesn't cause the proper LEDs to light up" has way to many opportunities for error. "My remote cause the Arduino to print a value of 2515615, which I store in an int, but that int doesn't match any case" has a lot fewer opportunities for error, and the one error is (to me) glaringly obvious.

Think of this as an opportunity to practice. You only get better with practice. Unless you are a wine, in which case age is sufficient (if you were any good to start with).