How to use PROGMEM

I have been having issues with a 166 value array, ranging from 341 to 336098 (they're values of a thermistor reading). I have looked through a few posts on PROGMEM but am unsure on how to implement it into my code.

At the moment, the code is not outputting anything on my LCD, and looking at the Serial monitor it seems like the issue is resistance values not being printed.

Essentially what I'm trying to do is find the 2 closest array values to "resistance_s" and create a linear relationship between them.

#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
#include <math.h>

const int RS = 12, E = 11, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);

const long int array[] = {341, 350, 359, 368, 378, 388, 399, 410, 421, 432, 444, 457, 
    469, 482, 496, 510, 524, 539, 555, 571, 587, 604, 622, 640, 659, 678, 699, 
    720, 741, 764, 787, 811, 836, 862, 888, 916, 945, 974, 1005, 1037, 1070, 1105, 
    1141, 1178, 1216, 1256, 1289, 1341, 1385, 1432, 1480, 1530, 1582, 1637, 1693, 
    1752, 1813, 1876, 1942, 2011, 2082, 2157, 2234, 2315, 2399, 2487, 2579, 2674, 
    2773, 2877, 2985, 3098, 3216, 3339, 3467, 3601, 3741, 3887, 4040, 4200, 4367, 
    4542, 4724, 4916, 5116, 5325, 5544, 5774, 6014, 6266, 6530, 6807, 7097, 7402, 
    7721, 8056, 8408, 8777, 9165, 9572, 10000, 10450, 10923, 11420, 11943, 12493, 
    13073, 13682, 14324, 15001, 15714, 16465, 17257, 18092, 18973, 19903, 20885, 
    21921, 23016, 24172, 25395, 26687, 28054, 29500, 31031, 32651, 34366, 36184, 
    38110, 40151, 42317, 44613, 47050, 49638, 52385, 55304, 58405, 61703, 65209, 
    68940, 72911, 77138, 81641, 86439, 91553, 97006, 102824, 109032, 115661, 
    122741, 130306, 138393, 147042, 156294, 166198, 176803, 188163, 200339, 213394, 
    227398, 242427, 258563, 275897, 294524, 314553, 336098};



void setup() {
    Serial.begin(9600);
    lcd.begin(16,2);
    
}

void loop() {
    double voltage_read = analogRead(A0);
    double voltage_out = voltage_read * 5/1023;
    //int resistance_o = 1000;
    //int resistance_g = 6000;
    //int resistance_f = 1000;

    // voltage_out = (1+2000/6000)5(Rs/(10000+Rs)-1000/(1000+10000))
    double resistance_s = (-200000 - 330000 * voltage_out)/(33 * voltage_out - 200);
    //double temp = 233 - 22.1log(resistance_s);
    int left = 0;
    int right = sizeof(array) / sizeof(array[0]);

    while (right - left >= 2) {
        if (abs(array[left] - resistance_s) > abs(array[right] - resistance_s)) {
            left++;
        }
        else {
            right--;
        }
    }
    long int closest = array[left];
    long int closeish = array[left + 1];
    
    //linear temperature equation in form y - y1 = m(x - x1)
    double i = 125.0 - left;
    double temp = (1 / (closeish - closeish)) * (resistance_s - closest) + i;

    Serial.print("\n");
    Serial.println(voltage_out);
    Serial.println(temp);
    Serial.println(resistance_s);
    Serial.println(closeish);
    Serial.println(closest);
    
    lcd.print("  Temperature: ");
    lcd.setCursor(4, 1);
    lcd.print(temp, 1);
    lcd.print(" ");
    lcd.print((char)223);
    lcd.print("C");
    delay(100);
    lcd.clear();

}

Is this of any use?

I did take a look at that. However being new to coding I wish I could see more examples. From what I can tell, it looks like I have to add PROGMEM to my declaration, as well as do something in my loop to read from the flash memory. Is that supposed to be an additional code I also write?

There is a worked example there.

You need pgm_read_dword

This is a higher level library that may help. There is documentation here.

You're reading off the end of your array. That will give you bogus data which may cause your search to end early.

2 Likes

do you suggest I simply use right = 165 as it is the index of the last value?

No, I suggest you use:
int right = sizeof array / sizeof array[0] - 1;

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.