Pages: [1]   Go Down
Author Topic: Is a lookup table the best option?  (Read 1340 times)
0 Members and 1 Guest are viewing this topic.
Just past the last tree on the left
Offline Offline
Full Member
***
Karma: 0
Posts: 125
Ó Fithcheallaigh
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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
Offline Offline
Tesla Member
***
Karma: 127
Posts: 8519
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Gray aka the GRAYnomad www.robgray.com

Just past the last tree on the left
Offline Offline
Full Member
***
Karma: 0
Posts: 125
Ó Fithcheallaigh
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 125
Ó Fithcheallaigh
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 125
Ó Fithcheallaigh
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 208
Posts: 8855
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 125
Ó Fithcheallaigh
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13718
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: