I have been diving into and reading endless internet forums and pages trying to find an answer to this, but have had no luck, so here we are. For reference, I am using an A/Dconverter and I am changing the value of the A/Dconverter using a variable resistor plugged into ground and 5V. So when I write my C code, if I am using a signed integer--that integer being the value of the analog to dig conversion--will the value be automatically split on both sides of zero. For example, let's say I have get an 8 bit integer and I am using signed int. Because I am using signed int, will the values that I get automatically be anywhere from 0 to +255 or should I automatically receive a value that is anywhere between -128 to +127 when I move the wiper on the resistor?
2 Answers
Welcome to stackoverflow! It seems like a minor detail that you can easily test. However, most tutorials I have checked just now use 'int' rather than 'unsigned int'. One of the tutorials says the following:
"The value that is returned and stored in x will be a value from 0 to 1023. The Arduino has a 10-bit ADC (2^10 = 1024). We store this value into an int because x is bigger (10 bits) than what a byte can hold (8 bits)."
which makes me believe the values will be somewhere from 0-X, dependent on the amount of bits available. But as I said, this will be easily testable.

- 459
- 3
- 9
-
I have already changed my code around to see if it made a difference whether I use int(unsigned int) or signed int and I get the same values either way, that being anywhere from 0-X just as you said. Let's say it is a 10 bit ADC and I wanted the values to be -512 to +511 would I need to write extra code for that or SHOULD using signed int make that happen without any extra code? – C_GNARWIN Jul 27 '21 at 07:27
Let me start off by stating this, an 8-bit integer contains 8 bits. 2 8 is 256, so an 8-bit integer can hold 256 possible values. There are 256 possible values between -128 to 127, inclusive.
So, you should be expecting values ranging from -128 to 127, not 0 to 255

- 111
- 1
- 6