Importing a large array from another .h file

I have a sketch that uses a large array. I thought it would be easier to keep this in a separate file (like how arduino_secrets.h is typically done) and import it into a local variable

I couldnt get it to work so currently have the file laid out like this. My intention is that I would ultimately have more than one array in the file, and I can pick the one I want to use and save into a local variable in the main sketch.


#include <WiFi.h>
#include <PubSubClient.h>
#include "arduino_secrets.h"
// #include "thermistor.h"

const int thermistor[] = {
  -50,667828,
  -40,335671,
  -30,176683,
  -20,96974,
  -15,72895,
  -10,55298,
  -5,42314,
  0,32650,
  1,31030,
  2,29500,
  3,28054,
  4,26688,
  5,25396,
  6,24173,
  7,23016,
  8,21921,
  9,20885,
  10,19904,
  11,18974,
  12,18092,
  13,17257,
  14,16465,
  15,15714,
  16,15001,
  17,14325,
  18,13623,
  19,13053,
  20,12494,
  21,11943,
  22,11420,
  23,10923,
  24,10450,
  25,10000,
  26,9572,
  27,9165,
  28,8777,
  29,8408,
  30,8056,
  35,6530,
  40,5325,
  45,4367,
  50,3601,
  55,2985,
  60,2487,
  65,2082,
  70,1751,
  75,1480,
  80,1256,
  85,1070,
  90,916.1,
  95,787.0,
  100,678.6
  };

// Replace with your network credentials
const char* ssid = SECRET_SSID;
const char* password = SECRET_PASS;

as you can see I commented out the line but left it there. I was that array, to be in another file with other arrays, and essentially do what is happening with the wifi credentials.

I have read some other posts but couldnt work out what I was missing. Im sure its simple but copying the wifi credentials didnt seem to like it.

I have file thermistor.h already with the same array like this

#define TABLE {
  -50,667828,
  -40,335671,
  -30,176683,
  -20,96974,
  -15,72895,
  -10,55298,
  -5,42314,
  0,32650,
  1,31030,
  2,29500,
  3,28054,
  4,26688,
  5,25396,
  6,24173,
  7,23016,
  8,21921,
  9,20885,
  10,19904,
  11,18974,
  12,18092,
  13,17257,
  14,16465,
  15,15714,
  16,15001,
  17,14325,
  18,13623,
  19,13053,
  20,12494,
  21,11943,
  22,11420,
  23,10923,
  24,10450,
  25,10000,
  26,9572,
  27,9165,
  28,8777,
  29,8408,
  30,8056,
  35,6530,
  40,5325,
  45,4367,
  50,3601,
  55,2985,
  60,2487,
  65,2082,
  70,1751,
  75,1480,
  80,1256,
  85,1070,
  90,916.1,
  95,787.0,
  100,678.6
  }

Can anyone advise how I would make use of this set up or if Ive made some fundamental mistake.

Thanks

if you wish to define data in one file and use it in another look at extern
what microcontroller are you using?

First, since your array contains non-integral values (916.1 & 678.6) it seems odd that you're declaring it to be an 'int'. To fix this, I used 'float' in my example below.

More importantly, you should never define variables in a .h file, only declare them (if you don't know the difference, see: https://www.cprogramming.com/declare_vs_define.html)
Otherwise, you'll get multiple definition linker errors should that .h file be #include(d) in multiple source files. So, declare the array in the .h and define it in its own .cpp. Also, since the size is not known in other source files at compile time, you'll need to declare an extra variable for that:

Main .ino File:

#include "Thermister.h"

void setup() {
  Serial.begin(115200);
  delay(3000);

  for (size_t i = 0; i < thermisterArrySize; i++) {
    Serial.println(thermistor[i]);
  }
}

void loop() {
}

Thermister.h:

#ifndef THERMISTER_H
#define THERMISTER_H

#include <Arduino.h>

extern const float thermistor[];
extern const size_t thermisterArrySize;

#endif

Thermister.cpp:

#include "Thermister.h"

const float thermistor[] = {
  -50, 667828,
  -40, 335671,
  -30, 176683,
  -20, 96974,
  -15, 72895,
  -10, 55298,
  -5, 42314,
  0, 32650,
  1, 31030,
  2, 29500,
  3, 28054,
  4, 26688,
  5, 25396,
  6, 24173,
  7, 23016,
  8, 21921,
  9, 20885,
  10, 19904,
  11, 18974,
  12, 18092,
  13, 17257,
  14, 16465,
  15, 15714,
  16, 15001,
  17, 14325,
  18, 13623,
  19, 13053,
  20, 12494,
  21, 11943,
  22, 11420,
  23, 10923,
  24, 10450,
  25, 10000,
  26, 9572,
  27, 9165,
  28, 8777,
  29, 8408,
  30, 8056,
  35, 6530,
  40, 5325,
  45, 4367,
  50, 3601,
  55, 2985,
  60, 2487,
  65, 2082,
  70, 1751,
  75, 1480,
  80, 1256,
  85, 1070,
  90, 916.1,
  95, 787.0,
  100, 678.6
};

const size_t thermisterArrySize = sizeof(thermistor) / sizeof(thermistor[0]);

For further guidance on multi-file projects, see My Post #5 in this Thread.

Thanks, this has compiled so I will be able to do some testing now. I had missed the int/float issue.

as for the define/declare, I was trying to copy how it was done with the arduino_secrets.h file and this uses define too. Does this mean every example is based on one using bad habits?

If you mean its use of defien as in

# define blaNkaBlah  42

that's a different thing altogether than differentiating between the declaration of a variable and the definition of a variable.

So while # define is old fashioned and some consider using it to be bad, that's not what @anyone was talking g about.

a7

I just had a read through the link provided and I see the difference now.

if the #define only relevant to the file its contained in? So the use of it in arduino_secrets.h would have any affect in the main sketch?

When you include a file, it is identical to going to that file, copying it in its entirety and pasting it at the point where the # include was originally.

So yes, a # define in an included file is available to the source file that did the including.

a7

good to know, thanks

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