Go Down

Topic: writing digitalread states as bits (Read 785 times) previous topic - next topic

Jimmy60

Here's how I'm seeing the request

Code: [Select]

byte pinState = digitalRead(mypin);
if (pinState == HIGH)  Serial.println("1";
else Serial.println("0");


Shrug ...

Nick Gammon


I just want to print to the serial every digitalread bit I catch.


Would this work?

Code: [Select]

void setup ()
  {
  Serial.begin (115200);
  }  // end of setup
 
void loop ()
  {
  Serial.println (PINB, BIN); 
  delay (1000);
  }  // end of loop

dhenry

Very easy:

Code: [Select]

void sprintf_byte(unsigned char dat) {
  unsigned char mask = 0x80; //msb first
  do {
    if (dat & mask) Serial.print("1"); //print1
    else Serial.print("0"); //print 0
    mask = mask >> 1; //shift to the next bit
  } while (mask);
}

void sprintf_short(unsigned short dat_word) {
  sprintf_byte(dat_word >> 8); //printf msb
  sprintf_byte(dat); //print lsb
}

...


Go Up