Pages: [1]   Go Down
Author Topic: writing digitalread states as bits  (Read 530 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46206
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

How to do that ?
Code:
int digitalReadBit = digitalRead(theRightPin);
Serial.print(digitalReadBit);

Quote
How to use a buffer to do that ?
Code:
int buffer[1];
buffer[0] = digitalRead(theRightPin);
Serial.print(buffer[0]);
Though why you want to use a buffer is a mystery.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Am I wrong ? I want raw bits
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46206
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

instead I want:

byte = read_8_bits()
Serial.print(byte)

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46206
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24424
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Serial.print ((byte)digitalRead (mypin));
Logged

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16540
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

« Last Edit: December 10, 2012, 04:23:18 pm by retrolefty » Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11195
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's how I'm seeing the request

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

Shrug ...
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Would this work?

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very easy:

Code:
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
}

...
Logged

Pages: [1]   Go Up
Jump to: