I am having trouble finding a good solution to my problem.
I need to assign a single byte variable and give it a name. For exampled status. which is a single byte.
I would then like to name each bit of status. For example bit 0 is called home_bit and assign it as bit or Boolean type of variable.
Then I want to be able to set or clear each of the bits, by name, so I can i keep track of some things in my code. I want everything contained in one byte so I can access the status serially. As in send the status byte out on the UART port.
Basically, I want to create a register called status and access the all the named bits withing status.
I tried using Structs but failed.
It will actually allow a lot more. Assume that you only need 4 status bits; the below shows how you can have an additional 4-bit counter in the struct.
3. C++ allows to use a Tag (called user-defiined data type) so that we don't need to type the data items of the structure everytime we declare a variable. The syntax is:
struct STATUS
{
byte x;
int y
float z;
bool status0;
};
Here, STATUS is a Tag (also known as user-defined data type) which refers to all the data items of the structure.
4. Now, we can simplify the declaration of variables y1 and y2 of Step-2 as follows:
STATUS y1;
STATUS y2;
Here, y1/Y2 is a variable whose data type is STATUS which refers to four data items of different types (byte, int, float, and bool) of the structure.
5. Now, we can access the variables of the structure using member operator (.). Example:
struct STATUS
{
byte x;
int y;
float z;
bool status0;
};
STATUS y1; //variable declaration of user-defined type
void setup()
{
Serial.begin(9600);
y1.x = 0x06; //value is assigned to variable x which is a member of variable y1
Serial.println(y1.x); //shows: 6
y1.status0 = true;
byte rdStatus0 = y1.status0; //reading the value of status0 -- member of y1
Serial.println(rdStatus0); //shows 1 = HIGH = true
}
void loop()
{
}
What would be the purpose of doing this? The write() function sends the data as binary. I don't know why you'd want to do that. But .... Serial.write(reinterpret_cast<uint8_t *>(&status), sizeof(status));