Serial read 1 bit at a time

I want to read a single bit (not a byte) from the Serial communication. Serial.read() reads one byte, so I can't use that. I want to read a 15-bit message from my Java program, so just reading a couple of bytes and splitting them up into bits is not an option.

Is there any way to read just one bit from Serial?

And if you read 2 bytes (16 bits) and throw away the bit that you don’t want? That would work for you?

You can read a serial stream in software so you have full control:

#define INITDELAY 130//time for startbit + 1/2 bit - time för reading bits in us
#define BITDELAY 98//time for 1 bit - time for reading in us
byte recByte96_1_8(int pin){//receive 1 byte with 1 startbit and 8 data bits
  byte r = 0;//for storing returnvalue
  delayMicroseconds(INITDELAY);//wait for 1,5 bit
  for(int i = 0; i<8; i++){//8 bits
    r = (r>>1)+128*digitalRead(pin);//bits come in with lsb first
    delayMicroseconds(BITDELAY);//bit time = 104us
  }//for(int i)
  return r;
}//Receive(int pin)

I want to read a single bit (not a byte) from the Serial communication. Serial.read() reads one byte, so I can't use that.

Yes you can.All you need to do is read the byte and then do some bitwise operations to read the bit you want or if you don't know how to do this just use the function bitRead()

Here are the reference for the butRead function:

Reads a bit of a number. Syntax

bitRead(x, n) Parameters

x: the number from which to read

n: which bit to read, starting at 0 for the least-significant (rightmost) bit

Is there any way to read just one byte from Serial?

Sure, that is the way the arduino receives serial bytes, one at a time. You edited your original post and seem to be confused between bits and bytes, so you may need to clarify just what you want to do.

zoomkat: Sure, that is the way the arduino receives serial bytes, one at a time. You edited your original post and seem to be confused between bits and bytes, so you may need to clarify just what you want to do.

Sorry, I'm editing my post right now...

Serial.read() reads a byte, but I want to read a bit...


I'm experimenting with all the solutions my question received, thanks!

Serial.read() reads a byte, but I want to read a bit...

You will probably have to develop your own serial protocol to do that.

flaghacker: Serial.read() reads a byte, but I want to read a bit...

To the best of my knowledge you can only send bytes so what would be wrong with reading a byte?

If you want to experiment with your own software serial code this demo may be a suitable starting point.

...R

I noticed that in my Java code, so now I'm using bytes instead of bits. Thanks!