This dosen't load the byte with what bitwrite is telling it:

This dosen't load the byte with what bitwrite is telling it:

#include <SoftwareSerial.h>;

void setup()

{

Serial.begin(9600);

char Billy_Byte;

byte Billy;

bitWrite(Billy_Byte, 0, 1);

bitWrite(Billy_Byte, 1, 0);

bitWrite(Billy_Byte, 2, 0);

bitWrite(Billy_Byte, 3, 1);

bitWrite(Billy_Byte, 4, 1);

bitWrite(Billy_Byte, 5, 0);

bitWrite(Billy_Byte, 6, 0);

bitWrite(Billy_Byte, 7, 0);

Billy = Billy_Byte;

Serial.println();

Serial.println();

Serial.print("Billy contains ");

Serial.print(Billy, BIN);

Serial.println();

}

void loop()

{

}

Your giberish is hard to follow, use code tags as explained in the forum guidelines you read. Try this:

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

  byte x = 0b10000000;    // the 0b prefix indicates a binary constant
  Serial.print("BEFORE: ");
  Serial.println(x, BIN); // 10000000

  bitWrite(x, 0, 1);      // write 1 to the least significant bit of x
  Serial.print("AFTER:  ");
  Serial.println(x, BIN); // 10000001
}

void loop() {}

This was taken from: bitWrite() | Arduino Reference

1 Like

What did it print?

What did you expect it to print?

Why does it print here, for me, exactly what one would predict by reading the code?

a7

Works as expected for me too. Bits 5,6,and 7 were set to zero. Perhaps the OP was expecting the printout to show these zeroes.

Try this:

#include <SoftwareSerial.h>;

void prntBits(byte b)
{
  for(int i = 7; i >= 0; i--)
    Serial.print(bitRead(b,i));
  Serial.println();  
}

void setup()

{

Serial.begin(9600);

char Billy_Byte;

byte Billy;

bitWrite(Billy_Byte, 0, 1);

bitWrite(Billy_Byte, 1, 0);

bitWrite(Billy_Byte, 2, 0);

bitWrite(Billy_Byte, 3, 1);

bitWrite(Billy_Byte, 4, 1);

bitWrite(Billy_Byte, 5, 0);

bitWrite(Billy_Byte, 6, 0);

bitWrite(Billy_Byte, 7, 0);

Billy = Billy_Byte;

Serial.println();

Serial.println();

Serial.print("Billy contains ");

prntBits(Billy);

Serial.println();

}

void loop()

{

}

Thank you very, very much - works perfect - trying to understand the void at the top -

Seems like its an external call from the prntBits(Billy); command?

Going to work this up to the next increment - trying to make a 48 bit long bit vector

also found a new initilization technique

Thank you, thank you thank you again - My name's Scott!

:grinning:

In a message dated 1/27/2024 7:54:26 PM Eastern Standard Time, notifications@arduino.discoursemail.com writes:

Good morning and thanks again!

Here's the increment 2 code:

#include <SoftwareSerial.h>;

void prntBits(byte b)
{
for(int i = 7; i >= 0; i--)
Serial.print(bitRead(b,i));
}

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

byte Billy_Packet[] = {0b11111111, 0b11111111, 0b00001111, 0b10011000, 0b00110001, 0b11100001};

Serial.println("Hello ENOX!");
delay(5000);

Serial.print("Billy_Packet[0] contains ");
prntBits(Billy_Packet[0]);
Serial.println();

Serial.print("Billy_Packet[1] contains ");
prntBits(Billy_Packet[1]);
Serial.println();

Serial.print("Billy_Packet[2] contains ");
prntBits(Billy_Packet[2]);
Serial.println();

Serial.print("Billy_Packet[3] contains ");
prntBits(Billy_Packet[3]);
Serial.println();

Serial.print("Billy_Packet[4] contains ");
prntBits(Billy_Packet[4]);
Serial.println();

Serial.print("Billy_Packet[5] contains ");
prntBits(Billy_Packet[5]);
Serial.println();

Serial.println();
Serial.print("Billy_Packet[] contains ");
int i = 0;
do
{
prntBits(Billy_Packet[i]);
i = i+1;
}
while (i<6);
}

void loop()
{
}

Here's the output:

Seems to work perfect!!!:grinning:

Ultimately (I believe) I'm trying to make a series of "bit vectors" of varying length and then Bluetooth wirelessly transmit them. This all in conformance with National Model RailRoad Association Digital Command Control standards.
Just getting started and am scared to death of the "NMRA Arduino Library"

Thanks to your kindness am starting to understand more about the void "keyword".:grin:

Think it has something to do with how the serial monitor displays bytes versus bits - working on another iteration introducing what I believe will ultimately be a "frame" if I were smart enough to know what a "frame" is.

Thank you for your kindness, favor and interest!

:grinning:

In a message dated 1/27/2024 6:12:17 PM Eastern Standard Time, notifications@arduino.discoursemail.com writes:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.