Assign names to bit locations

I'm implementing a control program which uses a number of bits as flags.

I have a byte assigned to group these together:

byte Flags =0;

I can set an individual bit using bitSet(Flags,n) ( also clear & read using the relevant function )

So I may have:

bitSet(Flags,0) etc.

However to make the code more readable is there a way to substitute the parameter 'Flags,n' with a named constant, i.e.

OverTemp = Flags,0
MotorRun = Flags,1

To allow bitRead(OverTemp) etc?

Thanks

You can make use of bitfields in a struct.

struct FLAGS
{
  uint8_t rdy: 1;
  uint8_t ovf; 1;
  ...
  ...
};

FLAGS flags;

void setup()
{
  flags.rdy = 1;
  flags.ovf = 0;

  Serial.print("rdy = "); Serial.println(flags.rdy);
  Serial.print("ovf = "); Serial.println(flags.ovf);
}

void loop()
{
}

The struct will use the smallest amount needed based on the type of the flags. The above will use one byte; if you would have used uint32_t, it would have been four bytes for 2 bits (a bit of a waste).

You can also use the remaining 6 bits (in the example to store a 6 bit number

struct FLAGS
{
  uint8_t rdy: 1;
  uint8_t ovf: 1;
  uint8_t value: 6;
  ...
  ...
};

you can do exactly what you ask with #define, but it's not recommended.
name only the bits and use like bitSet(flags, OverTempFlag)

Thanks for the reply.
I don't understand the ' uint8_t ' Does this assign a series of bits in the flags structure or a series of bytes?

unsigned integer 8 bit. Its the portable way to say 'unsigned char'.

Thanks, I've got this working.
I don't fully understand about the structures, after the 'struct FLAGS', what does the line 'FLAGS flags;' do?

struct FLAGS
{
uint8_t rdy: 1;
uint8_t ovf; 1;
...
...
};

FLAGS flags;

If you look at any library code that manipulates processor registers, it's done all the time. Especially if efficiency is important. No reason you can't do the same with your flag "registers":

#define field0 0x01
#define field1 0x02
#define field2 0x04
#define field3 0x08
#define field4 0x10
#define field5 0x20
#define field6 0x40
#define field7 0x80

uint8_t flags = 0;

void someFunction() {

  // set a field
  flags |= field3;

  // clear a field
  flags &= ~field6;  
}

That being said, the "struct" approach is probably more readable and maintainable.

NevilleC:
after the 'struct FLAGS', what does the line 'FLAGS flags;' do?

Defines a variable of type "FLAGS" that is named "flags".

Got it sorted, many thanks