writing digitalread states as bits

Hi,

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

How to do that ? How to use a buffer to do that ?

Thanks

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

How to do that ?

int digitalReadBit = digitalRead(theRightPin);
Serial.print(digitalReadBit);

How to use a buffer to do that ?

int buffer[1];
buffer[0] = digitalRead(theRightPin);
Serial.print(buffer[0]);

Though why you want to use a buffer is a mystery.

that way i would print on the serial a bit as a byte.

Am I wrong ? I want raw bits

Am I wrong ? I want raw bits

Well, don't cook them.

What do you mean? The digitalRead() function returns an int, whose values are either 0 or 1. That looks like bits to me.

if you Serial.print(0) it should give 00110000, not 00000000

instead I want:

byte = read_8_bits()
Serial.print(byte)

if you Serial.print(0) it should give 00110000

How do you figure that 0 is 110,000? If that value is binary, the decimal equivalent is 48, which, coincidentally enough, just happens to be the ASCII code for a 0.

So, if you print '0' like so:
Serial.print('0', BIN);
you will get 00110000.

But that has NOTHING to do with raw bits from digitalRead().

I guess. From the manual:
Serial/Print: Prints data to the serial port as human-readable ASCII text.

If digitalRead(mypin) is == 0 then:
print somehow to the serial a 0 bit
if digitalRead(mypin) == 1 then:
print somehow to the serial a 1 bit

That's what I need. I hope i explained it better.
Thanks

byte = read_8_bits()
Serial.print(byte)

Trying to print the name of a datatype is going to give you problems.

If digitalRead(mypin) is == 0 then:
print somehow to the serial a 0 bit
if digitalRead(mypin) == 1 then:
print somehow to the serial a 1 bit

Serial.print ((byte)digitalRead (mypin));

robse:
if you Serial.print(0) it should give 00110000, not 00000000

instead I want:

byte = read_8_bits()
Serial.print(byte)

Perhaps you mean you wish to read and print out AVR I/O ports that can contain up to 8 bits?

as in:

int portByte = PIND; //Read AVR port D bits which correspond to arduino pins 0-7 on a 328 based board
Serial.print(portByte, BIN);

http://playground.arduino.cc/Learning/PortManipulation

Lefty

robse:
I want raw bits

I'm not clear what format you're trying to output, but if you want to output binary data instead of ascii characters then you might want to look at write() instead of print().

If you want to collect eight digitalRead() results and pack them into a byte and then output that as a binary value then you need to use bitSet()/bitClear() or the equivalent bitwise operators to build the byte and then write that value out. I'm only guessing what you're trying to achieve, though, because you haven't actually said.

Here's how I'm seeing the request

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

Shrug ...

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

Would this work?

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

Very easy:

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
}

...