Skip to content

Infrared

Tiogaplanet edited this page Jul 28, 2018 · 6 revisions

MiP's infrared sensors can not only detect obstructions and gestures, they can be used for interacting with other MiPs.


enableMiPDetectionMode()

void enableMiPDetectionMode(uint8_t id, uint8_t txPower);

Description

Allows MiP to be discovered by another MiP using IR. MiP broadcasts a user-defined identification number for another MiP to read.

Parameters

  • id is the user-defined identification number to send to another MiP.

  • txPower is the IR transmission power. Valid values are 1 to 120 for about 1-300cm.

Returns

Nothing

Example

#include <mip.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  if (!mip.isMiPDetectionModeEnabled())
    Serial.println(F("I am not discoverable."));

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
}

void loop() {
  delay(3000);

  uint8_t detectedMiP = 0x00;

  // Spend about 10 seconds broadcasting MiP's ID while looking for another MiP.
  for (int i = 0; i < 3; i++) {
    if (mip.readDetectedMiP(detectedMiP)) {
      Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);

    }
    delay(3333);
  }

  // MiP doesn't want to be found anymore.
  mip.disableMiPDetectionMode();

  // Verify it and keep looking for other MiPs.
  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
  else
    Serial.println(F("I am not discoverable."));
  if (mip.readDetectedMiP(detectedMiP)) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);
  }
}

disableMiPDetectionMode()

void disableMiPDetectionMode();

Description

Prevents MiP from being discovered by another MiP using IR.

Parameters

None

Returns

Nothing

Example

#include <mip.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  if (!mip.isMiPDetectionModeEnabled())
    Serial.println(F("I am not discoverable."));

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
}

void loop() {
  delay(3000);

  uint8_t detectedMiP = 0x00;

  // Spend about 10 seconds broadcasting MiP's ID while looking for another MiP.
  for (int i = 0; i < 3; i++) {
    if (mip.readDetectedMiP(detectedMiP)) {
      Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);

    }
    delay(3333);
  }

  // MiP doesn't want to be found anymore.
  mip.disableMiPDetectionMode();

  // Verify it and keep looking for other MiPs.
  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
  else
    Serial.println(F("I am not discoverable."));
  if (mip.readDetectedMiP(detectedMiP)) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);
  }
}

isMiPDetectionModeEnabled()

bool isMiPDetectionModeEnabled()

Description

Checks whether MiP is broadcasting an identification number using IR.

Parameters

None

Returns

  • true if MiP is broadcasting its identification number.

Example

#include <mip.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  if (!mip.isMiPDetectionModeEnabled())
    Serial.println(F("I am not discoverable."));

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
}

void loop() {
  delay(3000);

  uint8_t detectedMiP = 0x00;

  // Spend about 10 seconds broadcasting MiP's ID while looking for another MiP.
  for (int i = 0; i < 3; i++) {
    if (mip.readDetectedMiP(detectedMiP)) {
      Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);

    }
    delay(3333);
  }

  // MiP doesn't want to be found anymore.
  mip.disableMiPDetectionMode();

  // Verify it and keep looking for other MiPs.
  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
  else
    Serial.println(F("I am not discoverable."));
  if (mip.readDetectedMiP(detectedMiP)) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);
  }
}

readDetectedMiP()

int8_t readDetectedMiP(uint8_t& detectedMiP)

Description

Reads the identification number of a detected MiP.

Parameters

  • detectedMiP contains the identification number of a detected MiP after calling the function.

Returns

  • 0 if no MiP was detected.
  • 1 if a MiP was detected.

Example

#include <mip.h>

MiP mip;

// Load this sketch on a pair of MiPs facing each other and use a different MIP_ID_NO for each.
#define MIP_ID_NO       0x10
#define MIP_IR_TX_POWER 0x78

void setup() {
  bool connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("EnableMiPDetectionMode.ino - Enable your MiP to be discovered by another using IR."));

  if (!mip.isMiPDetectionModeEnabled())
    Serial.println(F("I am not discoverable."));

  mip.enableMiPDetectionMode(MIP_ID_NO, MIP_IR_TX_POWER);

  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
}

void loop() {
  delay(3000);

  uint8_t detectedMiP = 0x00;

  // Spend about 10 seconds broadcasting MiP's ID while looking for another MiP.
  for (int i = 0; i < 3; i++) {
    if (mip.readDetectedMiP(detectedMiP)) {
      Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);

    }
    delay(3333);
  }

  // MiP doesn't want to be found anymore.
  mip.disableMiPDetectionMode();

  // Verify it and keep looking for other MiPs.
  if (mip.isMiPDetectionModeEnabled())
    Serial.println(F("Now I can be discovered."));
  else
    Serial.println(F("I am not discoverable."));
  if (mip.readDetectedMiP(detectedMiP)) {
    Serial.print(F("I detected MiP with ID number ")); Serial.println(detectedMiP, HEX);
  }
}

sendIRDongleCode()

void sendIRDongleCode(uint8_t sendCode[], uint8_t dataNumbers, uint8_t transmitPower)

Description

Sends code to another MiP using IR. The code to send may be of two, three or four bytes in length.

For two-byte codes any value may be sent, i.e. { 0xNN, 0xNN }.

For three-byte codes the following formats are valid: { 0x00-0x03, 0xNN, 0xNN } or { 0xNN, 0xNN, 0xFF }.

For four-byte codes the following formats are valid: { 0xNN, 0xNN, 0xFF, 0xFF } or { 0x00-0x03, 0x00-0x03, !0xFF, !0xFF }.

Parameters

  • sendCode is a four-byte array containing code to send to another MiP. sendCode[3] is the most significant byte. In sends of two significant bytes, sendCode[2] and sendCode[3] are sent. In sends of three significant bytes, sendCode[1], sendCode[2] and sendCode[3] are sent.
  • dataNumbers is the number of bytes to send. Allowable values are 2, 3 or 4.
  • txPower is the IR transmission power. Valid values are 1 to 120 for about 1-300cm.

Returns

Nothing

Example

#include <mip.h>

// Try different values for VALID_DATA_BYTES (2, 3 or 4)
#define VALID_DATA_BYTES 4

// Try different values for transmission power (0x01 - 0x78)
#define MIP_IR_TX_POWER  0x78

#define MAX_DATA_BYTES 4

MiP  mip;
bool connectResult;

// Try different codes for dongleCode[].
const uint8_t dongleCode[4] = { 0xBB, 0x0AB, 0xFF, 0xFF };

void setup() {
  connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("SendIRDongleCode.ino - Send code to another MiP using IR."));
}

void loop() {
  Serial.print(F("Sending "));
  for (int i = MAX_DATA_BYTES - VALID_DATA_BYTES; i < MAX_DATA_BYTES; i++) {
    Serial.print(dongleCode[i], HEX); Serial.print(F(" "));
  }
  Serial.println();

  mip.sendIRDongleCode(dongleCode, VALID_DATA_BYTES, MIP_IR_TX_POWER);

  delay(1000);
}

readIRDongleCode()

int8_t readIRDongleCode(MiPIRCode& codeEvent)

Description

Reads code sent by another MiP using IR.

Parameters

  • codeEvent is the MiPIRCode object containing the received code after a call to the function.

Returns

  • MIP_ERROR_NO_EVENT if no transmitted data was found.
  • 0 if the data was received.

Example

#include <mip.h>

MiP       mip;
uint8_t   dongleCode[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
MiPIRCode receiveCode;
bool      connectResult;

void setup() {
  connectResult = mip.begin();
  if (!connectResult)
  {
    Serial.println(F("Failed connecting to MiP!"));
    return;
  }

  Serial.println(F("ReadIRDongleCode.ino - Receive code from another MiP using IR."));
}

void loop() {
  mip.readIRDongleCode(receiveCode);

  if (receiveCode.dataNumbers) {
    Serial.print(F("Receiving ")); Serial.print(receiveCode.dataNumbers); Serial.println(F(" data numbers."));
    for (int i = 0; i < receiveCode.dataNumbers ; i++)
    {
      Serial.print(receiveCode.code[i], HEX); Serial.print(F(" "));
    }
    Serial.println();
  }

  receiveCode.clear();

  delay(500);
}

Clone this wiki locally