Hello,
The Wireling IR Emitter and IR Detector Example scripts come as two separate scripts; I've tried to combine them into one, by putting the Receiver code into a function,
and calling it after the Emitter sends in the main loop.
But it looks like it never reads the sent IR-code, ie if (irrecv.decode(&results)) is never true... Not sure what I am doing wrong, can you help?
thank you!!!
/************************************************************************
IR Emitter Wireling Example
IR Receiver Wireling Example
Copied from:
https://learn.tinycircuits.com/Wirelings/IR-Emitter_Wireling_Tutorial/ https://learn.tinycircuits.com/Wirelings/IR-Receiver_Wireling_Tutorial//// Combined to transmit and receive on same device 5/13/2022 - Never reads?
************************************************************************/
#include <IRremote.h>
#include <Wire.h>
#include <Wireling.h>
#if defined (ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif
// Define codes as 32 bit unsigned integers [///Edit: fewer codes, for simplicity]
uint32_t powerCode = 0x10AF8877;
uint32_t muteCode = 0x10AFF00F;
// Receive and transmit can be done on any IO pin. Pick A0-A3 for Wireling ports 0-3.
int TX_PIN = A0;
int RECV_PIN = A3; /// added from WirelingIRReceiverExample
IRsend irsend(TX_PIN);
IRrecv irrecv(RECV_PIN);
void setup(void) {
SerialMonitorInterface.begin(9600);
Wire.begin();
Wireling.begin();
while (!SerialMonitorInterface && millis() < 5000); //This will block for 5 seconds or until the Serial Monitor is opened on TinyScreen+/TinyZero platform!
irrecv.enableIRIn(); // Start receiving data /// added from WirelingIRReceiverExample
}
void loop() {
SerialMonitorInterface.println("Sending powerCode!");
noInterrupts(); /// [from example code; I don't know what the interrupt code does]
irsend.sendNEC(powerCode, 32);
/// try adding receive code here ///
receiverExampleCode();
///
interrupts(); /// [from example code; I don't know what the interrupt code does]
/// try adding receive code here ///
receiverExampleCode();
///
delay(1000);
SerialMonitorInterface.println("Sending muteCode!");
noInterrupts();
irsend.sendNEC(muteCode, 32);
/// try adding receive code here ///
receiverExampleCode();
///
interrupts();
/// try adding receive code here ///
receiverExampleCode();
///
delay(1000);
}
/// void loop() {} from WirelingIRReceiverExample code put into below function:
void receiverExampleCode() {
/// add println
SerialMonitorInterface.println("into Receiver function");
decode_results results;
if (irrecv.decode(&results)) {
irrecv.resume(); // Receive the next value
if (results.decode_type = NEC && results.bits == 32) { //Check if there's a match for our expected protocol and bitcount
if (results.value == powerCode) {
SerialMonitorInterface.println("powerCode received!");
} else if (results.value == muteCode) {
SerialMonitorInterface.println("muteCode received!");
} else {
SerialMonitorInterface.print("Unrecognized code! ");
SerialMonitorInterface.println(results.value, HEX);
}
} else {
SerialMonitorInterface.print(results.decode_type);
SerialMonitorInterface.print(" ");
SerialMonitorInterface.print(results.bits);
SerialMonitorInterface.print(" ");
SerialMonitorInterface.println(results.value, HEX);
}
}
}