Notifications
Clear all

I cannot get an IrReceiver to turn an Output On

12 Posts
4 Users
5 Likes
480 Views
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Hi to all

I am building two remote control cars that can play laser tag with each other. I want to be able to drive them with  a TV remote and IrReceicer module. That is my end goal. The laser/receiver part works okay. For now, I just want to see if I can get my UNO to respond to the TV remote . With that, I just want to turn an LED on, then off after 2 seconds, just to see if all the components/coding works. After modifying the  code, I can see the HEX addresses of several remotes in the serial monitor. However; I cannot  get this info to turn on an LED. I tried using  switch/case, then an if statement saying if the Hex address is 0x____, turn on the LED. You can see that the program is not too  complicated. I think the issue is in the switch(), but I'm not sure what to put in there. This is a modified code of what has been out there for a long while. It gets rid of the concatenate errors and the one where it prints zeroes instead of the button addresses. My wiring is using a resistor and LED on pin 5. Pin 5 connects to the +ve side of the resistor/LED, while the left over side of the diode connects to GND. I know my wiring is good. I removed the wire from pin5 and touched the 5V rail with  it, and the LED turned on.

#include <IRremote.hpp>
const int RECV_PIN = 4;
int ledPin =5;

void setup(){

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
IrReceiver.begin(RECV_PIN, ENABLE_LED_FEEDBACK);
}

void loop(){

if (IrReceiver.decode()){
Serial.println(IrReceiver.decodedIRData.decodedRawData,HEX);
switch (IrReceiver.decodedIRData.decodedRawData, HEX){
case 0xFB040707:
// Turn on LED for 2 Seconds
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
}
IrReceiver.resume();
delay(500);
}
}

Than You


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

@gameworn

I'd suggest losing the switch statement and just have ...

if (IrReceiver.decodedIRData.decodedRawData==0xFB040707) {

...

}

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6960
 

@gameworn What is being printed?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6960
 

@gameworn Your switch statement appears to have extra 'stuff' a switch is just switch(var). If you only have 1 case, then as @will said, just use an if.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

@gameworn

  1. I see you have a print statement... can you show us the output?  
  2. You might be better off assigning it once and then doing the switch statement on the variable...
unsigned long rtn = IrReceiver.decodedIRData.decodedRawData;
Serial.println(rtn);
switch (rtn)
{
...

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Inst-Tech reacted
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
 

Posted by: @zander

@gameworn Your switch statement appears to have extra 'stuff' a switch is just switch(var). If you only have 1 case, then as @will said, just use an if.

Based on his plans, I'm betting he'll have a lot more in the switch statement... this is just the beginning of his trek!

 

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

@inq 

 

If (...) {

} else if (...){

... etc ...

}

🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6960
 

@inq fyi @will @gameworn I was looking into replacing the IR transmitter for a ceiling vent, this is the data produced from just one button pressed once, I think it was on/off but they all produced the same amount of data. Not newbie territory for sure.

Screenshot 2022 11 30 at 10.41.54

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

Thanks to you all. I was soo close. After seeing the HEX codes on my Serial port, I thought it would be  apiece of cake to output it to an LED. The if statement works. I was close but like you all said, I had too much inside the if and switch functions. I tried so many combinations, but did not think to get rid of the word HEX from that line of code. I am indeed off to bigger and better things, so I will try the switch/case, next and see if I can get the LED to turn off from one and on from another button.

Thanks again

Steve


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6960
 

@gameworn I am wondering why you didn't get a compile error? I have to leave in a couple minutes and will be recovering for a few days after minor surgery so maybe someone else can try compiling that code with ALL errors.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@gameworn)
Member
Joined: 4 years ago
Posts: 30
Topic starter  

@Inq  

I used your switch/case code example. The only thing is that when you print rtn, it prints in decimal and I wanted to see it as a HEX value., so I  changed Serial.println(rtn); to Serial.println(rtn, HEX);. It all worked out quite well.

I saw this ad in the Hammacher Schlemer catalog for Laser Battling Cars, and I thought, hey, I can make those. I'll have to encase the electronics in concrete after I'm done to make them grandkid proof.

Thank You

void loop(){

if (IrReceiver.decode()){

unsigned long rtn = IrReceiver.decodedIRData.decodedRawData;

Serial.println(rtn, HEX);
switch (rtn){
case 0xFB040707:
digitalWrite(ledPin, HIGH);
break;

case 0XFA050707:
digitalWrite(ledPin, LOW);
break;
}
IrReceiver.resume();
delay(500);
}
}


   
Inq and Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

@gameworn 

An unbreakable toy may always be used to break another toy 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote