# \#define bit of byte for CAN Bus packet

**URL:** <https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266>\
**Category:** Programming\
**Created:** [April 19, 2023, 6:45pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266 "2023-04-19T18:45:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 19, 2023, 6:45pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/1 "2023-04-19T18:45:37Z")

</div>

Hello,  
First post, lots of PLC programming experience, new to Arduino / C++.

I'm building an air suspension controller for a vehicle. One Adafruit Feather M4 CAN Express controls the suspension and communicates with a 2nd unit via CAN bus which is a user interface on the dash. I have the electronics working. and CAN bus talking.

The data being sent is in an eight byte array.  
I need one byte to represent eight bools and the remaining seven bytes will be int values.  
I want to give easy names to all of the data, so I used #define to reference bytes in the array, which works as expected.  
I'd like to do the same with the eight bits (bit 0 = pump\_on, bit2 = pump\_fail, etc...) but can figure out a way to name the individual bits.  
Any ideas?  
I tried sending a structure, but couldnt get it to work.

Thank you.

```cpp
// BODY MODULE (blue light)

#include <CAN.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

#define MY_PACKET_ID 0xAD

uint32_t timestamp;

//initilaize arrays for inbound and outbound data

struct outstruct { //tried sending a structure, no luck so far
  bool bit0;
  bool bit1;
  bool bit2;
  bool bit3;
  bool bit4;
  bool bit5;
  bool bit6;
  bool bit7;
  int int0;
  int int1;
  int int2;
  int int3;
  int int4;
  int int5;
  int int6;
};

byte sendbuff[8];
byte rcvbuff[8];

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

  pinMode(PIN_CAN_STANDBY, OUTPUT);
  digitalWrite(PIN_CAN_STANDBY, false); // turn off STANDBY
  pinMode(PIN_CAN_BOOSTEN, OUTPUT);
  digitalWrite(PIN_CAN_BOOSTEN, true); // turn on booster

  //Light the neopixel blue so we can identify the BODY MODULE on the testing bench
  strip.begin();
  strip.setBrightness(5);
  strip.setPixelColor(0, 0, 0, 255); //set color to green
  strip.show();

  //define easy names for data
  #define TANKPRES sendbuff[0]
  #define LEFTPRES sendbuff[1]
  #define RIGHTPRES sendbuff[2]

      // send some test values.....
      TANKPRES = 55;
      LEFTPRES = 66;
      RIGHTPRES = 77;
  

  // start the CAN bus at 250 kbps
  if (!CAN.begin(250000)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }

  timestamp = millis();
}

void loop() {
  // every 1000 ms send out a packet
  if ((millis() - timestamp) > 1000) {
    
  
    Serial.print("Sending packet");
        
    CAN.beginPacket(MY_PACKET_ID);
        
    CAN.write(sendbuff, 8);

    CAN.endPacket();
  
    
    Serial.print(sendbuff[0]);
    Serial.print(" ");
    Serial.print(sendbuff[1]);
    Serial.print(" ");
    Serial.print(sendbuff[2]);
    Serial.print(" ");
    Serial.print(sendbuff[3]);
    Serial.print(" ");
    Serial.print(sendbuff[4]);
    Serial.print(" ");
    Serial.print(sendbuff[5]);
    Serial.print(" ");
    Serial.print(sendbuff[6]);
    Serial.print(" ");
    Serial.print(sendbuff[7]);
    Serial.println();
    
    timestamp = millis();
  }

```

---

<div class="post-metadata">

**Author:** ![apf1979](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/apf1979/32/966781_2.png) [@apf1979](https://forum.arduino.cc/u/apf1979)\
**Post date:** [April 19, 2023, 6:48pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/2 "2023-04-19T18:48:50Z")

</div>

Did you look at bits and bytes on the reference page?

> **[Arduino Reference - Arduino Reference](https://www.arduino.cc/reference/en/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 19, 2023, 7:08pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/3 "2023-04-19T19:08:27Z")

</div>

I did.  
I (think that I) understand how to set and read bits. Once I start writing the bulk of my code, I want to be able to do something like PUMPON = 1 instead of bitset(sendbuff[0] , 2). (assuming bit #3 of the first bite represents the pump running state)  
I'm just looking for a plain English way to reference the bits, like I'm doing with the #define on the bytes.

Thank you.

---

<div class="post-metadata">

**Author:** ![apf1979](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/apf1979/32/966781_2.png) [@apf1979](https://forum.arduino.cc/u/apf1979)\
**Post date:** [April 19, 2023, 7:26pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/4 "2023-04-19T19:26:20Z")

</div>

> [@m\_d\_r](#):
>
> I want to be able to do something like PUMPON = 1 instead of bitset(sendbuff[0] , 2)

Could you use bitWrite(x, n, PUMPON); and just do all the writes in a function at the end of the loop?

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [April 19, 2023, 7:28pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/5 "2023-04-19T19:28:03Z")

</div>

```cpp
#define CLR(x,y) (x&=(~(1<<y))) //bit y of x will 0
#define SET(x,y) (x|=(1<<y)) //1
#define CHK(x,y) (x & (1<<y)) // gives a value of bit y
#define TOG(x,y) (x^=(1<<y)) //will toggle

```

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [April 19, 2023, 7:35pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/6 "2023-04-19T19:35:52Z")

</div>

another alternative

```cpp
#define PUMPON(value) (bitWrite(pump,2,value))
byte pump=0;

```

sets bit 2 of variable pump to value (0 or 1)

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [April 19, 2023, 7:44pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/7 "2023-04-19T19:44:45Z")

</div>

```cpp
// BODY MODULE (blue light)

#define PIN_NEOPIXEL 2
#define PIN_CAN_STANDBY 3
#define PIN_CAN_BOOSTEN 4
#include <CAN.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

#define MY_PACKET_ID 0xAD
#define TANKPRES sendbuff[0] //define easy names for data
#define LEFTPRES sendbuff[1]
#define RIGHTPRES sendbuff[2]
#define CLR(x,y) (x&=(~(1<<y))) //bit y of x will 0
#define SET(x,y) (x|=(1<<y)) //1
#define CHK(x,y) (x & (1<<y)) // gives a value of bit y
#define TOG(x,y) (x^=(1<<y)) //will toggle

uint32_t Timestamp;
const uint16_t Period=1000UL;
byte sendbuff[8];
byte rcvbuff[8];

void setup() {
  Serial.begin(9600);
  pinMode(PIN_CAN_STANDBY, OUTPUT);
  digitalWrite(PIN_CAN_STANDBY, false); // turn off STANDBY
  pinMode(PIN_CAN_BOOSTEN, OUTPUT);
  digitalWrite(PIN_CAN_BOOSTEN, true); // turn on booster

  //Light the neopixel blue so we can identify the BODY MODULE on the testing bench
  strip.begin();
  strip.setBrightness(5);
  strip.setPixelColor(0, 0, 0, 255); //set color to green
  strip.show();

  // send some test values.....
  TANKPRES = 55;
  LEFTPRES = 66;
  RIGHTPRES = 77;
  if (!CAN.begin(250000)) {// start the CAN bus at 250 kbps
    Serial.println("Starting CAN failed!");
    while (1);
  }
  Timestamp = millis();
}

void loop() {
  // every 1000 ms send out a packet
  if ((millis() - Timestamp) > Period) {
    Serial.print("Sending packet");
    CAN.beginPacket(MY_PACKET_ID);
    CAN.write(sendbuff, 8);
    CAN.endPacket();

    for (int8_t i = 0; i < 8; i++) {
      Serial.print(sendbuff[i]);
      if (i % 7)Serial.print(" ");
      else Serial.println();
    }
    Timestamp += Period;
  }

```

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [April 19, 2023, 8:09pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/8 "2023-04-19T20:09:56Z")

</div>

> [@m\_d\_r](#):
>
> The data being sent is in an eight byte array.  
> I need one byte to represent eight bools and the remaining seven bytes will be int values.

The last time I looked a byte was 8 bits and an int was 16 bits in Arduino language, so what magic will be used to accomplish this. CAN is bytes, 0-7 what they are is entirely up to your software. Did you bytes instead of int?

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 19, 2023, 8:38pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/9 "2023-04-19T20:38:35Z")

</div>

Whoops, yes, I meant byte not int.

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 19, 2023, 8:40pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/10 "2023-04-19T20:40:20Z")

</div>

Thanks,  
I this works:

```cpp
//define easy names for data
  #define PUMPRUN(value) (bitWrite(sendbuff[0],0,value))
  #define PUMPFAULT(value) (bitWrite(sendbuff[0],1,value))
  #define TANKPRES sendbuff[1]
  #define LEFTPRES sendbuff[2]
  #define RIGHTPRES sendbuff[3]
  

      // send some test values.....
      TANKPRES = 55;
      LEFTPRES = 66;
      RIGHTPRES = 77;
      PUMPRUN(1);
      PUMPFAULT(1);

```

I missed the fact that #define can be passed a value. Thanks.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 19, 2023, 8:58pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/11 "2023-04-19T20:58:33Z")

</div>

> [@m\_d\_r](#):
>
> I tried sending a structure, but couldnt get it to work.

You should endeavor to get that working. Structs provide bitfield capability which is exactly what you're asking for. Generally speaking, pre-processor macros should used only as a last resort.

```arduino
struct ControllerData {
  uint8_t pumpOn : 1;
  uint8_t pumpFail : 1;
  uint8_t otherBit1 : 1;
  uint8_t otherBit2 : 1;
  uint8_t otherBit3 : 1;
  uint8_t otherBit4 : 1;
  uint8_t otherBit5 : 1;
  uint8_t otherBit6 : 1;

  uint8_t tankPres;
  uint8_t leftPres;
  uint8_t rightPres;
  uint8_t otherByte1;
  uint8_t otherByte2;
  uint8_t otherByte3;
  uint8_t otherByte4;
};

ControllerData data;

void setup() {
  data.pumpOn = 1;
  data.pumpFail = 0;
  data.tankPres = 100;
  //
  //
  //
  CAN.write(&data, sizeof(data));

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [April 20, 2023, 9:15am UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/12 "2023-04-20T09:15:08Z")

</div>

> [@gfvalvo](#):
>
> Structs provide bitfield capability which is exactly what you're asking for.

Amen 👍

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [April 20, 2023, 9:44am UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/13 "2023-04-20T09:44:11Z")

</div>

> [@gfvalvo](#):
>
> `data.pumpOn = 1;`

```arduino
if ( data.pumpOn == 1 ) {...} // right?

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 20, 2023, 6:50pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/14 "2023-04-20T18:50:54Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 20, 2023, 8:23pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/15 "2023-04-20T20:23:04Z")

</div>

Hi,  
I think I follow most of this, however the code won't compile with this line:  
`CAN.write(&data, sizeof(data));`  
If I comment out the one line it compiles.

The thrown error is:

```cpp

D:\..some file path...\Body_Module_02.ino: In function 'void loop()':
D:\..some file path...\Body_Module_02.ino:94:34: error: no matching function for call to 'CANSAME5x::write(ControllerData*, unsigned int)'
   94 | CAN.write(&data, sizeof(data));
      | ^
In file included from c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CANSAME5x.h:5,
                 from c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CAN.h:8,
                 from D:\..some file path...\Body_Module_02.ino:3:
c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CANController.h:26:18: note: candidate: 'virtual size_t CANControllerClass::write(uint8_t)'
   26 | virtual size_t write(uint8_t byte);
      | ^ ~~~~
c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CANController.h:26:18: note: candidate expects 1 argument, 2 provided
c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CANController.h:27:18: note: candidate: 'virtual size_t CANControllerClass::write(const uint8_t*, size_t)'
   27 | virtual size_t write(const uint8_t *buffer, size_t size);
      | ^ ~~~~
c:\Users\..some file path...\libraries\CAN_Adafruit_Fork\src/CANController.h:27:39: note: no known conversion for argument 1 from 'ControllerData*' to 'const uint8_t*' {aka 'const unsigned char*'}
   27 | virtual size_t write(const uint8_t *buffer, size_t size);
      | ~~~~~~~~~~~~~~~^~~~~~

exit status 1

Compilation error: no matching function for call to 'CANSAME5x::write(ControllerData*, unsigned int)'

```

If required, here is the content of CANSAME5x:

```cpp
// Copyright 2020 © Jeff Epler for Adafruit Industries. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full
// license information.

#include "CANController.h"

class CANSAME5x : public CANControllerClass {
public:
  CANSAME5x();
  CANSAME5x(uint8_t tx_pin, uint8_t rx_pin);
  ~CANSAME5x() final;

  int begin(long baudRate) final;
  void end() final;

  int endPacket() final;

  int parsePacket() final;

  void onReceive(void (*callback)(int)) final;

  using CANControllerClass::filter;
  int filter(int id, int mask) final;
  using CANControllerClass::filterExtended;
  int filterExtended(long id, long mask) final;

  int observe() final;
  int loopback() final;
  int sleep() final;
  int wakeup() final;

  void dumpRegisters(Stream &out);

private:
  void bus_autorecover();

  void handleInterrupt();

  int _parsePacket();

private:
  int8_t _tx, _rx;
  int8_t _idx;
  // intr_handle_t _intrHandle;
  void *_state;
  void *_hw;
  static CANSAME5x *instances[2];

  static void onInterrupt();

  friend void CAN0_Handler(void);
  friend void CAN1_Handler(void);
};

extern CANSAME5x CAN;

```

And also CAN.h:

```cpp

// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef CAN_H
#define CAN_H

#if defined(ADAFRUIT_FEATHER_M4_CAN)
#include "CANSAME5x.h"
#elif defined(ARDUINO_ARCH_ESP32)
#include "ESP32SJA1000.h"
#else
#include "MCP2515.h"
#endif

#endif

```

Thank you.

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 20, 2023, 8:28pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/16 "2023-04-20T20:28:51Z")

</div>

Also,  
What is the function of this line:

`ControllerData data;`

I assume we are assigning (defining?) the struct ControllerData to data? But why?  
I tried looking it up but don't know what terminology to use. I tried Alias, etc.

I see that from this point on the struct is referenced as "data", i.e., **data.pumpOn** , etc.

Thank you again

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 20, 2023, 8:29pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/17 "2023-04-20T20:29:50Z")

</div>

> [@m\_d\_r](#):
>
> however the code won't compile

Looks like you may just need a cast to the right pointer type:

```arduino
CAN.write(reinterpret_cast<uint8_t *> (&data), sizeof(data));

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 20, 2023, 8:31pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/18 "2023-04-20T20:31:08Z")

</div>

> [@m\_d\_r](#):
>
> What is the function of this line:
> 
> `ControllerData data;`

It instantiates an object (variable) named 'data' of type 'ControllerData'.

The same as

```arduino
int dog;

```

instantiates a variable named 'dog' of type 'int'.

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 20, 2023, 8:48pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/19 "2023-04-20T20:48:24Z")

</div>

Ahh, OK.  
So you can therefore have multiple objects using the type defined by the structure?  
Like:

```cpp
ControllerData inbounddata;
ControllerData outbounddata;

```

---

<div class="post-metadata">

**Author:** ![m\_d\_r](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@m\_d\_r](https://forum.arduino.cc/u/m_d_r)\
**Post date:** [April 20, 2023, 8:49pm UTC](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266/20 "2023-04-20T20:49:46Z")

</div>

> [@gfvalvo](#):
>
> Looks like you may just need a cast to the right pointer type:
> 
> ```arduino
> CAN.write(reinterpret_cast<uint8_t *> (&data), sizeof(data));
> 
> ```

That works! Thanks so much.  
I'll go read up on casting.

[Next page](https://forum.arduino.cc/t/define-bit-of-byte-for-can-bus-packet/1117266.md?page=2)
