Sending Bytes to Arduino via Serial Node

Hello everyone,

I have a project where I need 6 function blocks, each performing the functions of a button (0/1), to send their value in binary via the Serial node. As of now, I'm testing this with just 1 such function block. I have the function block for button 1 set up to send out 00000100 as ON and 00000000 as OFF. My question is, how do I configure the Serial Node to send this as a byte so that it will be compatible with my sketch on Arduino (send as uint8). Here is the bit of the Arduino sketch which is reading the button state:

int switch2;
int switch3;
const uint8_t virtual_switch_masks[8] = {
    0,
    0,   
    1<<0,  // pin 2: virtual switch 0
    1<<1,  // pin 3: virtual switch 1
    1<<2,
    1<<3,
    1<<4,
    1<<5
    // etc...
}; 
uint8_t virtual_switch_states = 0;
int virtualDigitalRead(uint8_t pin) {
    uint8_t mask = virtual_switch_masks[pin];

    // If this is not a virtual switch, do a regular digitalRead().
    if (!mask)
        return digitalRead(pin);

    // For a virtual switch, extract the relevant bit
    // from virtual_switch_states.
    if (virtual_switch_states & mask)
        return HIGH;
    else
        return LOW;
}

void loop(){
  if (Serial.available()){
    virtual_switch_states = Serial.read();
  }
  switch2=virtualDigitalRead(2);
  digitalWrite(9,switch2);
}

Here's a screenshot of my flow right now.

What you need to know about the pink nodes: The first 2 are simply for initialization. The next one, returns a value between 0-100. Here's a screenshot of my function block 'Button 1':

image

As per my limited understanding, my LED connected to pin 9, should light up, when the payload from the pink node goes above 50, and the function payload becomes [0,0,0,0,0,1,0,0]. I've set the Serial node output to be binary buffers as well. I don't understand what's wrong.

Please help.

In my opinion you write not an uint8- byte - your buffer is 8 bytes long instead of 1 byte

As your sketch is expecting a single number, you should send a single number

try

const all_off = 0x0;
const pin2 = 0x4; // hex 4 = dec 4, bit3 on, all others off :   00000100
// or const pin2 = 0b00000100; //binary 4 in JS is written as 0b00000100
const pin3 = 0x8; // hex 8 = dec 8, bit4 on, all others off :   00001000
// or const pin3 = 0b00001000; //binary 8 in JS is written as 0b00001000
const pin2and3 = pin2 | pin3;//  the | operator is bitwise OR 
msg.payload = pin2;
return msg;

In fact, no need for function nodes in your whole flow, you could just use a switch node with 2 entries (Output 1: determine >50, Output 2: "otherwise") and 2 change nodes to set payload to a number - 0 (for all off), 4 (for pin2), 8 (for pin3), 16 (for pin4) etc.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.