Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: August 18, 2012, 04:00:39 pm » |
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
Edison Member
Karma: 50
Posts: 2457
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #1 on: August 18, 2012, 04:07:11 pm » |
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
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6842
Arduino rocks
|
 |
« Reply #2 on: August 18, 2012, 04:39:13 pm » |
Is there a reason you are not dividing by 4 (in other words why 255 and not 256, 1023 and not 1024?) result = potValue / 4 ; or with shifting right: result = potValue >> 2 ; Or if the slight difference matters, there is a built-in function specifically for this task: result := map (potValue, 0, 1023, 0, 255) ; http://arduino.cc/en/Reference/map
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: August 18, 2012, 04:39:56 pm » |
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 / 2 2 = ?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: August 18, 2012, 04:40:40 pm » |
result := map (potValue, 0, 1023, 0, 255) ; Have we got our Pascal head on ? 
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6842
Arduino rocks
|
 |
« Reply #5 on: August 18, 2012, 05:44:19 pm » |
Sorry, = not := (and its Spin, not Pascal, in fact!)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #6 on: August 19, 2012, 01:35:35 am » |
130*255/1023. Think about how that sum works.
130 * 255 = 33150
That's outside the amount that an int will store.
Ah. Ok.  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
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #7 on: August 19, 2012, 06:21:10 am » |
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
Online
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: August 19, 2012, 06:56:15 am » |
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.
|
|
|
|
SF Bay Area (USA)
Online
Faraday Member
Karma: 80
Posts: 5508
Strongly opinionated, but not official!
|
 |
« Reply #9 on: August 19, 2012, 03:07:02 pm » |
Mostly AVR. it's one of the few platforms where an int is only 16 bits.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 3
Posts: 321
Arduino rocks
|
 |
« Reply #10 on: August 19, 2012, 03:39:13 pm » |
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 =) int foo = 20; int bar = 35; float baz = 0.0;
baz = foo / bar;
|
|
|
|
|
Logged
|
|
|
|
|
|