Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
|
 |
« on: July 29, 2011, 03:42:11 am » |
Hello, I have a group of data, in which a specific voltage relate to a specific weight, for example 2 volts is almost 1.5kg. In my prgram, I have mapped the analog input reading, which reads the weight applied to the FSR, to the voltage (0 to 5000mV). So, now I am wondering, can I use a loopup table which will find a specific voltage, and then print out the correspinding weight? I have never used lookup takes and would really like some advice, because I could completely be heading down the worng paths. Here is what I have so far, which may make things a bit clearer ...but there is no table in it yet. int analogPin = 0; int fsrReading; int fsrVoltage; long forForce;
void setup() { Serial.begin(9600); pinMode(analogPin, INPUT); }
void loop() { fsrReading = analogRead(analogPin); if(fsrReading == 0) { ;; } else { fsrReading = analogRead(analogPin); //Analog value ranges from 0 to 1023, or 1 to 5000mV fsrVoltage = map(fsrReading, 0, 1023, 0, 5000); Serial.println("The voltage is: "); Serial.println(fsrVoltage); delay(1000); } }
Thanks in advance. Seán
|
|
|
|
|
Logged
|
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 75
Posts: 6977
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #1 on: July 29, 2011, 04:35:42 am » |
Is the relationship between the voltage and the weight linear? If so then you don't need a lookup table unless you really need to be fast.
How much resolution do you need? If you want 1 gram resolution over 10kgs the table will probably be too large.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
|
 |
« Reply #2 on: July 29, 2011, 04:50:37 am » |
Hello,
Thanks for the reply.
From tests I have done, the relationship seems to be linear, yes. As for the resolution, yea, I think 1 gram would be good, but they may settle for 10 gram steps.
Seán
|
|
|
|
|
Logged
|
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.
|
|
|
|
New Jersey
Online
Edison Member
Karma: 26
Posts: 2455
|
 |
« Reply #3 on: July 29, 2011, 07:33:21 am » |
From tests I have done, the relationship seems to be linear, yes. As for the resolution, yea, I think 1 gram would be good, but they may settle for 10 gram steps.
In that case, find the minimum weight that gives an analog reading of 1024 and use map to get the weight. Of course, when the arduino reads 1024, it won't know actually what the weight is, just that it's the max readable.
|
|
|
|
|
Logged
|
|
|
|
|
Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
|
 |
« Reply #4 on: July 29, 2011, 07:45:38 am » |
But I am looking the program to tell me the weight in the range of 0 to 1023 (or 0 to 5V), so, if I get an analog reading of say, 738, the program will spit out the corresponding weight.
Will your method do that? maybe it's just because I am new, but I don't see how it would.
Seám
|
|
|
|
|
Logged
|
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.
|
|
|
|
New Jersey
Online
Edison Member
Karma: 26
Posts: 2455
|
 |
« Reply #5 on: July 29, 2011, 08:51:40 am » |
Given that the response appears to be linear and assuming that when weight is 0, analogread returns 0 and when weight is 1500g, you're getting 2v, we can extrapolate and calculate that a weight of 3750 will give 5v. The voltage is an irrelevance however, you can just map analogread from 0 to 1024 to 0 to 3750 to get the corresponding weight in grams.
Caveats - Does no weight actually give zero reading? Is it linear enough for the accuracy you want? Are the things you're weighing all <= 3750g?
|
|
|
|
|
Logged
|
|
|
|
|
Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
|
 |
« Reply #6 on: July 29, 2011, 08:57:43 am » |
Ah, okay, I'm with you now!
Yea, when there is no weight applied, I get 0 in the analogRead. I haven't applied enough weight to to get a maximum reading, but I have done a number of points in between, and the graph is linear.
Again, thanks!
Seán
|
|
|
|
|
Logged
|
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6615
|
 |
« Reply #7 on: July 29, 2011, 09:09:27 am » |
const int OneKilo = 327; // analogRead() value for a one kilogram test weight, determined experimentally const int TwoKilos = 561; // analogRead() value for a two kilogram test weight, determined experimentally const int countsPerKilo = TwoKilos - OneKilo; int grams(int analogValue) { analogValue -= (OneKilo - countsPerKilo); // Remove any offset from 0 weight = 0 volts return ((analogValue * 1000) / countsPerKilo); // Convert voltage to grams }
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Online
Edison Member
Karma: 26
Posts: 2455
|
 |
« Reply #8 on: July 29, 2011, 12:40:58 pm » |
The possible downside is that it is integer and maybe you need decimal/floating point?
Given the hardware he has, integer will be fine as long as he's working in grams - it'll only resolve down to a resolution of ~4g.
|
|
|
|
|
Logged
|
|
|
|
|
Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
|
 |
« Reply #9 on: July 29, 2011, 02:02:45 pm » |
Yea, I am using map now, and is working fine.
Much thanks.
Seán
|
|
|
|
|
Logged
|
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 32
|
 |
« Reply #10 on: January 12, 2013, 07:05:52 am » |
Hi guys, I know a lot of time has past since the last post, however I need your help!!
I read all the comments, and was wondering how will I go if my analogRead relationship with weight is not linear? According to manufacturer's specs, it's a logarithmic function (FSR).
I have tried the mapping function with no luck!. I read something about the Log10 function but couldn't really keep up with the blogger...
Please advice!
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 101
Posts: 9553
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #11 on: January 12, 2013, 08:12:20 am » |
I read all the comments, and was wondering how will I go if my analogRead relationship with weight is not linear? According to manufacturer's specs, it's a logarithmic function (FSR). I have written multimap() for that to support non-linear lookups. It can be found here - http://playground.arduino.cc/Main/MultiMap -
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #12 on: January 12, 2013, 08:15:56 am » |
I read something about the Log10 function but couldn't really keep up with the blogger... If you know the relationship, you can calculate it off line and use a look-up table. The issue with look-up table is that it trades space for time and it is inflexible.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #13 on: January 12, 2013, 11:15:10 pm » |
However a small lookup table, kept in PROGMEM, might save the necessity of including code to calculate it (eg. to do logarithms). You might need to test empirically, but possibly the lookup table could use less space, and be faster.
|
|
|
|
|
Logged
|
|
|
|
|
|