Pages: [1]   Go Down
Author Topic: Simple math doing my head in - when is an int treated like an int?  (Read 887 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there

I'm trying to do something simple - convert analogue pin input (0 - 1023) to byte equivalent (0 - 255).

All variables declared as int.

I do this (i.e. input value x 255/1023) and all starts going strange at about value 130.

  // read potentiometer pin
  potValue = analogRead(potPin);
  Serial.print("Pin Value: ");
  Serial.println(potValue);
  // pot pin value will be from 0 - 1023
  // convert this to 0 - 255
  result = potValue * 255/1023;
  Serial.print("Result: ");
  Serial.println(result);


Pin Value: 128
Result: 31

Pin Value: 131
Result: -31

If I replace the "255 / 1023" with 0.25, then all works fine
 
result = potValue * 0.25; //255 / 1023;

Pin Value: 215
Result: 53

Now I understand that intermidiate int calcs are treated as ints, which may cause a problem with 255/1023, but 0.25 is no more an int than 255/1023?

Can anyone tell me what's up?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

An int can represent any integer value between -32768 and +32767

Anything outside this range wraps around.


130*255/1023.  Think about how that sum works.

130 * 255 = 33150

That's outside the amount that an int will store.

You should either use an "unsigned int" that can store between 0 and 65535, or a "long" which can store considerably more (we're talking billions here).
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

0
Online Online
Shannon Member
****
Karma: 218
Posts: 12596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a reason you are not dividing by 4 (in other words why 255 and not 256, 1023 and not 1024?)

Code:
result = potValue / 4 ;
or with shifting right:
Code:
result = potValue >> 2 ;

Or if the slight difference matters, there is a built-in function specifically for this task:

Code:
result := map (potValue, 0, 1023, 0, 255) ;
http://arduino.cc/en/Reference/map
Logged

[ I won't respond to messages, use the forum please ]

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

Quote
I'm trying to do something simple - convert analogue pin input (0 - 1023) to byte equivalent (0 - 255).
So why are you going about it in such a complicated way?
0..1023 represents a span of ten bits.
0..255  represents a span of eight bits.

1023 / 22 = ?
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.

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

Code:
result := map (potValue, 0, 1023, 0, 255) ;
Have we got our Pascal head on ?   smiley-wink
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.

0
Online Online
Shannon Member
****
Karma: 218
Posts: 12596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, = not := (and its Spin, not Pascal, in fact!)
Logged

[ I won't respond to messages, use the forum please ]

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


130*255/1023.  Think about how that sum works.

130 * 255 = 33150

That's outside the amount that an int will store.


Ah. Ok.  smiley-red  Thanks.

Thanks for the input guys - as for the why not divide by 4, I saw the 1023 and decided it would not quite be 4.  doh.
Logged

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

Is this behaviour (intermediate calcs being influenced by data type of variable) just Arduino, or is it C?  Damn sure this has never happened to me in years of Java..?
Logged

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

Mostly C, I'd say, with a smidgin of AVR - it is all to do with the platform and the default size of an "int".
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.

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6792
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Mostly AVR.  it's one of the few platforms where an int is only 16 bits.
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 323
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this behaviour (intermediate calcs being influenced by data type of variable) just Arduino, or is it C?  Damn sure this has never happened to me in years of Java..?

C/C++ have that issue everywhere... Just try this anywhere =)

Code:
int foo = 20;
int bar = 35;
float baz = 0.0;

baz = foo / bar;
Logged

Pages: [1]   Go Up
Jump to: