Go Down

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

robse

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

PaulS

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

How to do that ?

Code: [Select]
int digitalReadBit = digitalRead(theRightPin);
Serial.print(digitalReadBit);


Quote
How to use a buffer to do that ?

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

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

robse

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

Am I wrong ? I want raw bits

PaulS

Quote
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.


robse

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

instead I want:

byte = read_8_bits()
Serial.print(byte)


PaulS

Quote
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().

robse

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

AWOL

Quote
byte = read_8_bits()
Serial.print(byte)

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

Quote
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

Code: [Select]
Serial.print ((byte)digitalRead (mypin));
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

#8
Dec 10, 2012, 10:20 pm Last Edit: Dec 10, 2012, 10:23 pm by retrolefty Reason: 1

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:

Code: [Select]
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


PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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