Go Down

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

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