Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: December 10, 2012, 03:21:22 pm » |
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
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #1 on: December 10, 2012, 03:23:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #2 on: December 10, 2012, 03:28:44 pm » |
that way i would print on the serial a bit as a byte.
Am I wrong ? I want raw bits
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #3 on: December 10, 2012, 03:34:13 pm » |
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
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #4 on: December 10, 2012, 03:54:30 pm » |
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
Offline
Brattain Member
Karma: 316
Posts: 35535
Seattle, WA USA
|
 |
« Reply #5 on: December 10, 2012, 03:59:35 pm » |
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
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #6 on: December 10, 2012, 04:11:57 pm » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: December 10, 2012, 04:17:58 pm » |
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));
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #8 on: December 10, 2012, 04:20:33 pm » |
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/PortManipulationLefty
|
|
|
|
« Last Edit: December 10, 2012, 04:23:18 pm by retrolefty »
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6396
-
|
 |
« Reply #9 on: December 10, 2012, 04:25:46 pm » |
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
|
|
|
|
|
Saskatchewan
Offline
Full Member
Karma: 10
Posts: 223
When the going gets weird, the weird turn pro. - Hunter S. Thompson
|
 |
« Reply #10 on: December 10, 2012, 04:34:03 pm » |
Here's how I'm seeing the request byte pinState = digitalRead(mypin); if (pinState == HIGH) Serial.println("1"; else Serial.println("0");
Shrug ...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #11 on: December 10, 2012, 04:36:13 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #12 on: December 10, 2012, 05:28:20 pm » |
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 }
...
|
|
|
|
|
Logged
|
|
|
|
|
|