Using IR Remote sensor with PbHub



  • I have been exprimenting with using sensors on an M5Stack fire via a PaHub and a PbHub. Here is simple working example for a button module:
    *// Important! Set Tools-PSRAM to Disabled otherwise M5Stack display hangs.

    #include <Wire.h>
    #include "ClosedCube_TCA9548A.h"
    #include <M5Stack.h>
    #define PaHub_I2C_ADDRESS 0x70
    ClosedCube::Wired::TCA9548A tca9548a;
    #include "porthub.h"

    #define X_OFFSET 10
    #define Y_OFFSET 18

    PortHub porthub;
    uint8_t HUB_ADDR[6] = {HUB1_ADDR, HUB2_ADDR, HUB3_ADDR, HUB4_ADDR, HUB5_ADDR, HUB6_ADDR};

    void setup() {

    M5.begin();
    M5.Power.begin();
    delay(50);
    Serial.begin(115200); // Initialize serial communications with the PC
    Wire.begin(); // Initialize I2C
    uint8_t returnCode = 0;
    uint8_t channel = 0;
    tca9548a.address(PaHub_I2C_ADDRESS);
    returnCode = tca9548a.selectChannel(channel);
    Serial.print("returnCode = ");
    Serial.println(returnCode);
    //Wire.beginTransmission(channel);
    //returnCode = Wire.endTransmission();

    M5.Lcd.clear(BLACK);
    M5.Lcd.setTextColor(YELLOW);
    M5.Lcd.setTextSize(2);
    M5.Lcd.setCursor(30, 0); M5.Lcd.println("Button/Hub example");
    Serial.println("Button/Hub example: ");
    M5.Lcd.setTextColor(WHITE);
    }

    /*
    Description: Read the button status of BUTTON Unit and display it on the screen
    */
    #include <M5Stack.h>

    int last_value_A = 0;
    int cur_value_A = 0;
    int last_value_B = 0;
    int cur_value_B = 0;

    void loop() {
    cur_value_A = porthub.hub_d_read_value_A(HUB_ADDR[2]);// read the value of A-BUTTON
    cur_value_B = porthub.hub_d_read_value_B(HUB_ADDR[2]);// read the value of B-BUTTON

    M5.Lcd.setCursor(0, 25); M5.Lcd.print("Status A: ");
    M5.Lcd.setCursor(0, 45); M5.Lcd.print("Value A: ");
    M5.Lcd.setCursor(0, 65); M5.Lcd.print("Status B: ");
    M5.Lcd.setCursor(0, 85); M5.Lcd.print("Value B: ");
    if (cur_value_A != last_value_A) {
    Serial.print("A = ");
    Serial.print(cur_value_A);
    M5.Lcd.fillRect(110, 25, 100, 25, BLACK);
    M5.Lcd.fillRect(95, 45, 100, 25, BLACK);
    if (cur_value_A == 0) {
    M5.Lcd.setCursor(110, 25); M5.Lcd.print("pressed"); // display the status
    M5.Lcd.setCursor(95, 45); M5.Lcd.print("0");
    Serial.println("Button Status: pressed");
    Serial.println(" value: 0");
    }
    else {
    M5.Lcd.setCursor(110, 25); M5.Lcd.print("released"); // display the status
    M5.Lcd.setCursor(95, 45); M5.Lcd.print("1");
    Serial.println("Button Status: released");
    Serial.println(" value: 1");
    }

    last_value_A = cur_value_A;
    

    }

    if (cur_value_B != last_value_B) {
    Serial.print("B = ");
    Serial.print(cur_value_B);
    M5.Lcd.fillRect(110, 65, 100, 25, BLACK);
    M5.Lcd.fillRect(95, 85, 100, 25, BLACK);

    if (cur_value_B == 0) {
      M5.Lcd.setCursor(110, 65); M5.Lcd.print("pressed"); // display the status
      M5.Lcd.setCursor(95, 85); M5.Lcd.print("0");
      Serial.println("Button Status: pressed");
      Serial.println("       value:  0");
    }
    else {
      M5.Lcd.setCursor(110, 65); M5.Lcd.print("released"); // display the status
      M5.Lcd.setCursor(95, 85); M5.Lcd.print("1");
      Serial.println("Button Status: released");
      Serial.println("       value:  1");
    }
    
    last_value_B = cur_value_B;
    

    }
    M5.update();
    }*

    I have got several simple M5Stack sensors working this way. I canor however get the IR Remote module to work even just via the PbHub. The problem is that when connected directly the sketch normally reads pin 36 by including "int IR_RECEIVE_PIN = 36;" in the sketch. But my problem is how to do this via the PbHub? I have tried "int IR_RECEIVE_PIN="porthub.hub_a_read_value(HUB_ADDR[i2):" and other commands from porthub.h but then the address is read only once.

    How can I use PbHub and porthub.h to acheive the same effect I get from connecting directly to pin 36?



  • Hello @Steve1

    try replacing this line

    cur_recv_value = digitalRead(ir_recv_pin);
    

    with something like this:

    cur_recv_value = porthub.hub_a_read_value(HUB_ADDR[2]);
    

    Thanks
    Felix



  • Thanks, but I still cannot read the Serial.read() from "porthub.hub_a_read_value(HUB_ADDR[2]);"



  • Hello @Steve1

    sorry, at this point in time I am out of ideas. I don't have the IR remote sensor so I cannot test this myself. Hopefully somebody else can help you.

    Thanks
    Felix