I want to print the incoming signal from a shift register as a binary number on the monitor,
I am using Serial.print ln(number,BIN) this is fine unless the input has a 0 as the MSB, it then only shows 7 bits.
This causes the monitor to keep shifting the bits left and right, and is distracting.
Is there any way to send the info so it will maintain 8 bits?
Maybe like:
[quote]Rob Tillaart
Looks good! A small trick to get a leading zero (e.g. in a time string)
[code]int h = 14;
int m = 6
Serial << ((h<10)?"0":"") << h << ":" << ((m<10)?"0":"") << m << endl;
[/code]
no not really.
I have tried many ways to get that to work but without any luck.
int sensorValue = 26;
// print out the sensor value in binary:
Serial << ((sensorValue<10)?"0":"");
Serial.println(sensorValue,BIN);
I can't see how that would work and it fails to compile.
What I want is some way of getting a binary readout i.e. 00110101 with 8 characters even if the MSB's are '0's. At present it just chops leading '0's off.
int number = 127;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(number,BIN);
delay(1000);
}
Just gives me 1111111 instead of 01111111
If the input goes to 63 I get 111111 this makes the output on the monitor flash back and forth.
You probably could do it with an I/O stream formatter, but the easy approach if this is a one-off would be to use a for loop to iterate through the eight bits, bitRead() to read the current bit as 0 or 1, and just print each bit in turn. It's not elegant but it's only two lines of code at the end of the day.
thank you sir, that is exactly what I wanted, it took me 4 lines but I have included assigning the variable and putting a space for easy reading.
int a;
for (a=0;a<8;a=a++){
Serial.print ( bitRead (myinput,(7-a)));
Serial.print (" ");}
my thanks to you.
Or
for (int a=7; a >=0; a--) {
Serial.print ( bitRead (myinput, a));
Serial.print (" ");
}
I finally decided it looked better without the spaces, so with your 'assigning the variable locally within the calculation' AWOL it fits into two lines. Thanks.
I bet the repetitive program I am writing to make a pattern of led lights for every instance of input could be shortened considerably in the same way.
matelot:
a=a++
Don't ever do that - the results are undefined. a = a + 1 is fine, a++ is fine, but don't use an assignment to a variable at the same time you are post-incrementing it.
I must admit I hadn't noticed that.
When I looked and changed them the result was the same.
I also have a number of
for (i=1;i<256;i=i*2)
I notice if I change it to
for (i=1;i<256;i*2)
the variable i stays at 1. Is there a similar answer for this calculation?
It works ok as it is but now I am curious.?
matelot:
It works ok as it is but now I am curious.?
It is the subject of long and involved discussions. Basically, the effect of the assignment depends whether the assignment to the left hand side happens before or after the post-increment has occurred, and the language spec doesn't define which order these things happen in.
Is there a similar answer for this calculation?
i *= 2;
matelot:
I must admit I hadn't noticed that.
When I looked and changed them the result was the same.
I also have a number offor (i=1;i<256;i=i*2)
That's OK, you aren't changing 'i' inside the expression, unlike: i = i++;
ok thanks.
i = whatever is "changing i inside the expression". It is taking the right side and assigning it to i.
the expression is:
i=i*2;
not
i*2
"A single expression should not cause the same object to be modified twice or to be modified and then inspected" C programming FAQs by Steve Summit.
so both
i = i++;
// and
a[i] = i++;
are both undefined, and undefined is the strongest language used in the C standard to describe stuff that you should never do.