Pages: [1]   Go Down
Author Topic: Trying to understnd arrays with analogread  (Read 515 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25623
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"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.

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int outputValues[600] = {0,1,2,3,4,5,6};
or using PROGMEM:
Code:
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:
Serial.print(outputValues[anlaogRead(0)]);
or using PROGMEM:
Code:
Serial.print(pgm_read_word_near(charSet + analogRead(0)))
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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).
Logged

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


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 -
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: