Assigning names to specific bits

I’m a newcomer to the Arduino, but have used a C compiler with the PIC microcontrollers for many years. I frequently group bits together in bytes, usually for control purposes. For example I am operating a number of relays via the I2C interface. As this sends a byte at a time the relays are set in groups.
So if I use a byte called ‘RelaysA’, this will turn on or off eight relays at a time.
However in the application code each relay may be treated separately.
This can be achieved using the bitSet command, for example bitSet(RelaysA,2). But in the PIC compiler a name can be assigned to a particular bit using a ‘#bit’ directive:

#bit powerSwitch=RelaysA.2

This makes the code much more readable, and more importantly reduces the likelihood for errors.

I’ve tried using #define:

#define powerSwitch RelaysA,2

so that I can then use bitSet(powerSwitch). However the define only inserts ‘RelayA’ and the comma and ‘2’ are ignored.

Is there a way around this?

Thanks

like this

#define AB 4

int x = 0;

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");
  
  Serial.println(x);
  bitSet(x, AB);
  
  Serial.println(x);
}

void loop() 
{
}

Thanks Rob,

This still needs the 'X,AB' format. What I would like to be able to do is replace the whole of the 'X,AB'.

For example: polarityRelay = RelaysA,0 powerRelay = RelaysA,1 chargeRelay = RelaysA,2 etc. . .

This would allow me to use a command such as: bitSet(powerRelay)

Thanks

I think the avr-gcc compilter does not support the x.2 use. But if I remember it right, other compilers for the ATmega chips do allow that. Since the Arduino IDE uses the avr-gcc, you just can not use it.

Please don't try to use something like that with variables or defines, it confuses me :roll_eyes: You can declare a structure with bit elements.

The ATmega microcontroller and avr-gcc compiler use often the bit numer. Bit number 0 = the lowest bit on the right. Bit number 1 = the next bit. You can make defines and use macros for those.

http://arduino.cc/en/Reference/HomePage Look at the section "Bits and Bytes"

typedef union
{
  unsigned char _byte;
  struct
  {
    unsigned char B0 : 1;
    unsigned char B1 : 1;
    unsigned char B2 : 1;
    unsigned char B3 : 1;
    unsigned char B4 : 1;
    unsigned char B5 : 1;
    unsigned char B6 : 1;
    unsigned char B7 : 1;
  } _bits;
}byte_bits_t;

You can change the names around a bit if you want, but that works. Here's a short example sketch showing how to use that union:

typedef union
{
  unsigned char _byte;
  struct
  {
    unsigned char B0_ : 1;
    unsigned char B1_ : 1;
    unsigned char B2_ : 1;
    unsigned char B3_ : 1;
    unsigned char B4_ : 1;
    unsigned char B5_ : 1;
    unsigned char B6_ : 1;
    unsigned char B7_ : 1;
  } _bits;
}byte_bits_t;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  unsigned char testing=0;
  Serial.println( testing );
  byte_bits_t bits;
  
  bits._byte = testing;
  bits._bits.B7_ = 1;
  testing = bits._byte;
  
  Serial.println( testing );
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}