How would i serial print the entire binary her i.e "0000 1111" to serial monitor

do{
 PORTB = PORTB | 0xF; // Writes PORTB high - Bin 0000 1111
 // using an OR mask so other pins are
// unaffected
 Serial.println("You pushed button one");
 Serial.println(0xF, BIN);
 
 Serial.println("All LEDS are on");
 delay(5000);
 PORTB = PORTB & 0xF0; // Writes PORTB low - Bin 1111 0000
 // using an AND mask so other pins are
// unaffected
 Serial.println("All LEDS are off");              
 delay(5000);

have a read of how-to-get-the-best-out-of-this-forum
what arduino board are you using?
I assume you wish to print leading 0's?

Thank you. I'm using a simulated arduino uno, yeah that's what i'm after

Hi,
There must be several ways to do it, this is one of them.
Test this code and see if it meets your needs.

void setup() {
  Serial.begin(115200);
  delay(10);
  int value = 0x1F;
  for(int i = 7; i>= 0; i--)
  {
  Serial.print(bitRead(value,i));
  }
Serial.println(" ");
}

void loop() {
}

Install and #include LibPrintf from the IDE library manager.

#include <LibPrintf.h>

void setup() {
  Serial.begin(115200);
  delay(300);

  printf("%s\n", "--------------------------------");

  byte someNumber = 0b00100011; // a binary formatted number
  printf("%08b\n", someNumber); // print with formatting

  someNumber = 0b01;
  printf("%08b\n", someNumber); // print with formatting

  printf("%08b\n", 0xA3); // print with formatting

}

void loop() {
}
void setup() {
  Serial.begin(115200);
  uint8_t arbval = 0xf; // arbitrary value
  uint8_t bitmask = B10000000; // 8 bits, MSB set. Could be written 0x80 or 128
  for (/*uint8_t bitmask = B10000000 */; bitmask /*  >  0 */; bitmask >>= 1) { // bitmask shift right until bitmask is zero
    Serial.print(bitmask & arbval ? '1' : '0');  // AND each bit in arbval with the shifting bitmask to print character 1 or 0
  }
}
void loop() {}

TLDR;

void setup() {
  Serial.begin(115200);
  uint8_t arbval = 0xf;
  uint8_t bitmask = B10000000;
  for (; bitmask; bitmask >>= 1) {Serial.print(bitmask & arbval ? '1' : '0');
  }
}
void loop() {}

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