CAN BUS I/O

I’m trying to setup a simple 16ch output canbus

I want to send a Can Message with 16 outputs in it

for example

Bit 1 would be 0001 and that would be the first of 8 bytes in the message.

The receiver arduino would look at the message and depending on what bit was changed to a 1 would turn on that output

so the CAN message would be

0x00, 0, 8,xxxx,xxxx,xxxx,xxxx,

I’m having issues getting it to work

for one not sure how I get each byte to send a binary value. I was think binary to hex.

this is my code so far

Send test

#include <mcp_can.h>
#include <SPI.h>

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

unsigned int bit1 = 0001;
unsigned int bit2 = 0010;
unsigned int bit3 = 0100;
unsigned int bit4 = 1000;
unsigned int bit5 = 1001;
unsigned int bit6 = 1010;
unsigned int bit7 = 1100;
unsigned int bit8 = 1111;

void setup()
{
    Serial.begin(115200);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_250KBPS))                   // init can bus : baudrate = 500k
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}


void loop() {

unsigned int onoff1 = bit1;
unsigned int onoff2 = bit2;

unsigned int onoff3 = bit3;
unsigned int onoff4 = bit4;

uint8_t onoff5 = bit5;
uint8_t onoff6 = bit6;

uint8_t onoff7 = bit7;
uint8_t onoff8 = bit8;




unsigned char onoff[8] = {onoff1 , onoff2 , onoff3, onoff4, onoff5, onoff6, onoff7, onoff8};

    // send data:  id = 0x00, standrad frame, data len = 8, stmp: data buf
    CAN.sendMsgBuf(0x0, 0, 8, onoff);
     
    delay(100);                       // send data per 100ms
}

Receive Test

 if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
    {
        CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf

        unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
        Serial.println("get data from ID: ");
        Serial.println(canId);

        for(int i = 0; i<len; i++)    // print the data
        {
            Serial.print(buf[i]);
            Serial.print("\t");
    if(canId == 0x0)
    {
      digitalWrite(16, HIGH);
    }
    else
    {
      digitalWrite(16, LOW);    
    }

   delay(100); 
    }
    }
} // loop

For the test code I’m just trying to have the led turn on if it see’s that canId.

It does see the id and turn on but doesn’t turn off once the message is being sent.

I’m really wanting it to read the message and then turn on what ever out it sees a 1 for

I may be going about this all wrong. In the end I’m trying to setup a CAN message that sends binary status of onoff states.

Thanks,

Bit 1 would be 0001 and that would be the first of 8 bytes in the message.

I stopped reading here.

This makes absolutely no sense.

Bit 1 can only be ‘0’ or ‘1’.
A byte is 8 bits…so 0001 would not even be the first byte…it is the first half-byte or “nibble”.