Skip to main content
added 428 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

You can use the following array initialization:

In the setup I shows how to set a bit. For this, the bit operator or (|) is used. To reset a bit, you can use &. You can set/reset multiple bits this way.

Also you can use 1 << 2 which means 1 (most right bit) shifted left two places (thus B100).

Btw, it is common practice to initialize all values.

byte lcd[8] = 
{
  B10000,
  B01000,
  B00100,
  B00010,
  B00001,
  B11000,
  B11100,
  B00000  // Also initialize last element
};

void setup() 
{
   lcd[3] |= B100; // Set 4th row (element 3), 3th bit (from the right)
   lcd[3] |= 1 << 2; // Alternative
}

void loop() 
{
}

Explanation about the or arithmetics: OR means: if at least one bit is 1, the result is 1, otherwise 0.

Truth table:

A | B | A or B
--+---+-------
0 | 0 |   0
0 | 1 |   1
1 | 0 |   1
1 | 1 |   1

For your example it means:

Original value of lcd[3]: B00010
Or mask (in setup):       B00100
                          ------ OR
Result                    B00110

You can use the following array initialization:

In the setup I shows how to set a bit. For this, the bit operator or (|) is used. To reset a bit, you can use &. You can set/reset multiple bits this way.

Also you can use 1 << 2 which means 1 (most right bit) shifted left two places (thus B100).

Btw, it is common practice to initialize all values.

byte lcd[8] = 
{
  B10000,
  B01000,
  B00100,
  B00010,
  B00001,
  B11000,
  B11100,
  B00000  // Also initialize last element
};

void setup() 
{
   lcd[3] |= B100; // Set 4th row (element 3), 3th bit (from the right)
   lcd[3] |= 1 << 2; // Alternative
}

void loop() 
{
}

You can use the following array initialization:

In the setup I shows how to set a bit. For this, the bit operator or (|) is used. To reset a bit, you can use &. You can set/reset multiple bits this way.

Also you can use 1 << 2 which means 1 (most right bit) shifted left two places (thus B100).

Btw, it is common practice to initialize all values.

byte lcd[8] = 
{
  B10000,
  B01000,
  B00100,
  B00010,
  B00001,
  B11000,
  B11100,
  B00000  // Also initialize last element
};

void setup() 
{
   lcd[3] |= B100; // Set 4th row (element 3), 3th bit (from the right)
   lcd[3] |= 1 << 2; // Alternative
}

void loop() 
{
}

Explanation about the or arithmetics: OR means: if at least one bit is 1, the result is 1, otherwise 0.

Truth table:

A | B | A or B
--+---+-------
0 | 0 |   0
0 | 1 |   1
1 | 0 |   1
1 | 1 |   1

For your example it means:

Original value of lcd[3]: B00010
Or mask (in setup):       B00100
                          ------ OR
Result                    B00110
Source Link
Michel Keijzers
  • 13k
  • 7
  • 42
  • 59

You can use the following array initialization:

In the setup I shows how to set a bit. For this, the bit operator or (|) is used. To reset a bit, you can use &. You can set/reset multiple bits this way.

Also you can use 1 << 2 which means 1 (most right bit) shifted left two places (thus B100).

Btw, it is common practice to initialize all values.

byte lcd[8] = 
{
  B10000,
  B01000,
  B00100,
  B00010,
  B00001,
  B11000,
  B11100,
  B00000  // Also initialize last element
};

void setup() 
{
   lcd[3] |= B100; // Set 4th row (element 3), 3th bit (from the right)
   lcd[3] |= 1 << 2; // Alternative
}

void loop() 
{
}