Go Down

Topic: Trying to understnd arrays with analogread (Read 631 times) previous topic - next topic

Lsnyman

A basic question but I am trying to understand how to implement an array with pre-measured analog values. I have 600 values because my sensor is not linear. So sensorvalue of 300 = outputvalue 1, sensorvalue of 301 = outputvalue 3, sensorvalue of 302 = outputvalue 4 etc.I have all the values for each step measured, but instead of creating 600 lines (if sensorvalue == 300. serialPrint 1) I know it must be easier to use array.
I am dumb in how to define and call this array.
So if my analogread value is the 10th value it must fetch the 10th value from an output array, correct?

How do you implement such a structure and how to call it?
Sorry for the basic question.

AWOL

If I'm reading you correctly, the array is called a look up table.
It'll use a lot of RAM, so you may need to employ PROGMEM
"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.

Lsnyman

Yes that is correct. I will use Progmem. I am just unclear about how to define this or these tables and how to print a value from the outputvalue based on a sensorvalue from analogread.

James C4S


I am just unclear about how to define this or these tables and how to print a value from the outputvalue based on a sensorvalue from analogread.


You would treat them like any other array, like:

Code: [Select]
int outputValues[600] = {0,1,2,3,4,5,6};
or using PROGMEM:
Code: [Select]
PROGMEM  prog_uint16_t outputValues[600]  = { 65000, 32796, 16843, 10, 11234};

Assuming you have 1 value in the outputValues array, for every possible analog value then:
Code: [Select]
Serial.print(outputValues[anlaogRead(0)]);
or using PROGMEM:
Code: [Select]
Serial.print(pgm_read_word_near(charSet + analogRead(0)))
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Lsnyman

Thabk you so much, your time is appreciated. Could you explain the Serial Print line
(pgm_read_word_near(charSet + analogRead(0)))

Thanks

James C4S


Thabk you so much, your time is appreciated. Could you explain the Serial Print line
(pgm_read_word_near(charSet + analogRead(0)))


http://arduino.cc/en/Reference/PROGMEM

When reading from PROGMEM you are telling the compiler to read from a specific address.  The tutorial uses "k" as the offset.  In your case, you'd be using whatever your analog value comes back as.

Again, these simple examples only work if outputValues has an indexed value for each value the analogRead() could return.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Lsnyman

Yes, there is an output value for each input.
Thank you very much for your input. Designing analog hardware is so much simpler than learning software lol.

Lsnyman

I guess i am stupi and stilldont understand. I used  the tutorial to try this out and just getting garbage out.

I can make it work with a small array without PROGMEM, but as soon as I try to add too many elements I see no output at all in the monitor, just a blank screen. maybe this is due to memory.
So I want to try PROGMEM, same result,
So I reduce the elements to small array and get wrong values returned.
Here is the code.
#include <avr/pgmspace.h>


void setup()
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
   analogReference(INTERNAL);

}

void loop()
{

  int rawMM;
 
  rawMM = analogRead(1);   // Range : 0..1024
  int mm = (constrain(rawMM, 337, 345)) -336;
  int  distanceValues[10] PROGMEM = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

  Serial.println(mm);

  unsigned int myval;
  myval=pgm_read_word(&distanceValues[mm]);
  Serial.println(myval);
}

i have tried various combinations for the pgm_read-word line but it doesnt help.

The output should be
1
1
2
2
3
3 etc

but instead I get
1
463
2
37296
1
463
2
37296
2
37296
3
464
3
464
2
37296
3
464
3
464
4
38401
3
464
4
38401
3
464
4
38401
3
464
3
464
3
464
4
38401
3
464

I dont know where these values are coming from.
Please help

PaulS

Code: [Select]
  int  distanceValues[10] PROGMEM = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
In loop()? I don't think you yet understand PROGMEM.

Program memory is read-only. It is populated at compile time. You are trying to populate it at run time. Since that doesn't happen, the "garbage" you are printing makes sense (to me).

robtillaart


Given the noise on the Arduino ADC most of the time only 8 bits are significant. This implies that a lookup table of size 256 should be sufficient in practice.

Assuming the lookup table makes a nonlinear function you could use multiMap() to make a far shorter lookup table. MultiMap() uses the most important points and interpolates between them if needed. A full description see - http://arduino.cc/playground/Main/MultiMap -
Rob Tillaart

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

Go Up