Help a very beginner. I'm trying to connect the IR transmitter and receiver, but apparently I don't understand something obvious to the guru, so it doesn't work. I see that the transmitter transmits something and the receiver receives something, but does not give anything to the serial monitor.
IDE version 2.1.0. Latest IRremote library. Windows 10.
Compilation works fine. Arduino nano Chinese clone. Here are the sketches.
Transmitter:
#include <IRremote.hpp>
#define PIN_SEND 3
void setup()
{
Serial.begin(9600); //Init monitor
IrSender.begin(PIN_SEND); // Initializes IR sender
}
void loop()
{
pinMode(7,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);//Four buttons
pinMode(9,INPUT_PULLUP);
pinMode(10,INPUT_PULLUP);
if(digitalRead(7)==0)
{ //if 1 button pressed
IrSender.sendNEC(0x0102, 0x34, 1); // the address 0x0102 with the command 0x34 is sent
Serial.println("sent 0x34");
}
else if(digitalRead(8)==0)
{
IrSender.sendNEC(0x0102, 0x35, 1);
Serial.println("sent 0x35");
}
else if(digitalRead(9)==0)
{
IrSender.sendNEC(0x0102, 0x36, 1);
Serial.println("sent 0x36");
}
else if(digitalRead(10)==0){
IrSender.sendNEC(0x0102, 0x37, 1);
Serial.println("sent 0x37");
}
delay(1000); // wait for one second
}
Receiver:
#define PIN_RECV 3
#include <IRremote.hpp>
void setup()
{
Serial.begin(9600); //Init monitor
IrReceiver.begin(PIN_RECV); // Initializes the IR
}
void loop()
{
if (IrReceiver.decode())
{
Serial.println("Received something...");
IrReceiver.printIRResultShort(&Serial);
// Prints a summary of the received data
IrReceiver.resume(); // Enables NEXT
}
delay(1000);
}
When I press the transmitter buttons, the monitor gives out:
34
35
36
37
Hello, do yourself a favour and please read How to get the best out of this forum and post accordingly — including necessary documentation for your ask like your exact circuit and power supply, links to components etc.
As the receiver code should respond to almost any remote control, have you tried it with e.g. your TV remote? Knowing whether or not this works would help you to know whether your receiver is working or not, without relying on the unknown of whether the transmitter is working or not.
First observation about your code.
This delay (1000) requires that we keep the transmission button pressed for at least 1 second, otherwise nothing works.
Use this code to test your hardware.
Use LED at pin 3 and receiver at pin 2. (Using 1 arduino UNO only).
PS: I redited the code after last post.
#include <Arduino.h>
// select only NEC and the universal decoder for pulse distance protocols
#define DECODE_NEC // Includes Apple and Onkyo
#define DECODE_DISTANCE_WIDTH // In case NEC is not received correctly. Universal decoder for pulse distance width protocols
#include "PinDefinitionsAndMore.h" // Define macros for input and output pin etc.
#include <IRremote.hpp>
#define DELAY_AFTER_SEND 2000
#define DELAY_AFTER_LOOP 5000
void setup() {
Serial.begin(115200);
while (!Serial)
; // Wait for Serial to become available. Is optimized away for some cores.
// Just to know which program is running on my Arduino
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial);
Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
IrSender.begin(); // Start with IR_SEND_PIN -which is defined in PinDefinitionsAndMore.h- as send pin and enable feedback LED at default feedback LED pin
Serial.println(F("Send IR signals at pin " STR(IR_SEND_PIN)));
}
uint16_t sAddress = 0x0102;
uint8_t sCommand = 0x34;
uint8_t sRepeats = 1;
/*
* Send NEC IR protocol
*/
void send_ir_data() {
Serial.print(F("Sending: 0x"));
Serial.print(sCommand, HEX);
Serial.flush(); // To avoid disturbing the software PWM generation by serial output interrupts
// clip repeats at 4
if (sCommand > 0x36) {
sCommand = 0x33;
}
// Results for the first loop to: Protocol=NEC Address=0x102 Command=0x34 Raw-Data=0xCB340102 (32 bits)
//IrSender.sendNEC(sAddress, sCommand, sRepeats);
IrSender.sendNEC(0x0102, sCommand, 1);
}
void receive_ir_data() {
if (IrReceiver.decode()) {
Serial.print(F("Decoded protocol: "));
Serial.print(getProtocolString(IrReceiver.decodedIRData.protocol));
Serial.print(F(", decoded raw data: "));
Serial.print(F(", decoded address: "));
Serial.print(IrReceiver.decodedIRData.address, HEX);
Serial.print(F(", decoded command: "));
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.resume();
}
}
void loop() {
/*
* Print loop values
*/
Serial.println();
Serial.print(F("address=0x"));
Serial.print(sAddress, HEX);
Serial.print(F(" command=0x"));
Serial.print(sCommand, HEX);
Serial.print(F(" repeats="));
Serial.println(sRepeats);
Serial.flush();
send_ir_data();
IrReceiver.restartAfterSend(); // Is a NOP if sending does not require a timer.
// wait for the receiver state machine to detect the end of a protocol
delay((RECORD_GAP_MICROS / 1000) + 5);
receive_ir_data();
// Prepare data for next loop
sCommand ++;
delay(100); // Loop delay
}