TinyCircuits Forum

TinyCircuits Products => Wirelings => Topic started by: lcpalm on May 13, 2022, 06:47:55 PM

Title: Wireling IR Emitter and IR Detector Example Scripts - Help
Post by: lcpalm on May 13, 2022, 06:47:55 PM
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);
    }
  }
}

Title: Re: Wireling IR Emitter and IR Detector Example Scripts - Help
Post by: lennevia on May 16, 2022, 12:00:37 PM
Here's some background on what the interrupts(); and noInterrupts();  code does: https://www.arduino.cc/reference/en/language/functions/interrupts/interrupts/

The example doesn't work as is, but when the noInterrupts(); are commented out, it will work with the mute code.  I hope that helps get you on the right track.

Cheers,
RĂ©na
Title: Re: Wireling IR Emitter and IR Detector Example Scripts - Help
Post by: lcpalm on May 16, 2022, 05:46:51 PM
Hi Rena,

Thanks SO much -- this was super helpful! I'm getting output that is not quite as expected but close, as you said.
Still have some work to do to figure out  what is going on in the logic of this loop and of IR codes themselves, and the purpose of the interrupts, but it's much clearer now.
At least it is very good to confirm that the IR-E and IR-D are both working - are both sending and receiving codes!

Much appreciated,
Linda
Title: Re: Wireling IR Emitter and IR Detector Example Scripts - Help
Post by: lennevia on May 18, 2022, 11:15:12 AM
Linda,

Glad to hear I was helpful!  ;D