Is a lookup table the best option?

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

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

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

o.fithcheallaigh:
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.

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

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?

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

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
    }

KE7GKP:
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.

Yea, I am using map now, and is working fine.

Much thanks.

Seán

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!

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 -

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.

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.