Just a quick question: I'm trying to get a byte (declared unsigned char) printed via Serial.print() in binary format. I initialised it as 0x00, so I expected 00000000, but instead, I get 10111111 repeatedly. Does anyone know what causes this? I'm using this byte to record certain events bitwise (8 different events, 1 or 0).
I was hoping someone knew a reason offhand so I didn't have to reprogram it! I'm using a standalone AVR without a bootloader on a circuit board connected via SPI to another AVR. Complicates things a little to reprogram it.
Well my entire code is very long, but I've checked through the entire thing, the only relevant parts are:
unsigned char errors = 0x00; //Global
Serial.println(errors, BIN); //Stuck somewhere in a function
I must add that "errors" currently does nothing! I haven't written the code to test for the errors and flip the bits. I'm only printing it as an acknowledgement when I send a command over serial.
There's nothing wrong with the two lines of code your provided. I run that code, get the expected output (which is zero 0, not 00000000, that's just the way println() works. It doesn't print leading zeros.)
So, by process of elimination, the problem HAS to be somewhere in the rest of your code. Your reluctance to provide the information necessary to help you find your problem only delays getting useful feedback. If you'd just provided your code in your first post, you very likely would have received some useful feedback already.
Honestly, I didn't think the problem would be with those two lines, since I've used Serial.print() many many times before. I thought perhaps I would be caused by attempting it on an unsigned char specifically.
My code is over a thousand lines long so I hardly think it'll be helpful to post it here.
Thanks AWOL for the helpful suggestion! Since the problem probably doesn't lie with the two lines, I'll just have to test entire code again.
Well I'll just start with printing HEX and DEC. Considering it's just 8 bytes for the USART buffer once every few seconds, it's unlikely to be that as well. I'm not sure how I'll test that though.
Thanks for offering to help debug the entire code. The code is actually for an autonomous quadrotor using distributed algorithms of a quaternion attitude estimator and it requires a lot of specific hardware to even run so I'll just try to fix this myself.