Declaring a struct

Hi all,

I have been attempting to google this problem as it seems like a very simple problem to solve yet no answers have provided me with a solution that works (or one that actually reflects my issue) so I appreciate your patience.

I am slowly transitioning from the Arduino IDE to platformIO, and in order to have my entire project transfer, I moved everything into a single file (bad practise I know, but it was the only way I could figure it out at the time to move to this platform and get this project on github). I have now been slowly moving functions into appropriate cpp files as I’m starting to figure out that process from this guide:
https://community.platformio.org/t/tutorial-for-creating-multi-cpp-file-arduino-project/5830

My question is: How do I declare the struct so that I do not get the error “src\loadcell.cpp:30:3: error: ‘conf’ was not declared in this scope”?

Here is the relevant code:
main.cpp

...
struct config_t {
  uint16_t conf_valid;
  long lora_freq;    // 4
  int lora_syncword = 0x12;
  long lora_bw = 250000;
  int lora_spread = 10;
  int lora_coderate = 5;
  int power = 12;
  uint32_t send_interval_mins = 1;
  uint32_t lc_min = 85000;
  uint32_t lc_max = 6500000;
  uint32_t weight_max = 100;
  int32_t resistor1 = 3300000;
  int32_t resistor2 = 560000;
  byte ir1ena = 1;
  byte ir2ena = 1;
  byte pir1ena = 0;
  int16_t battOffset = 0;
  uint8_t sensorModel;              // 1
  uint32_t id;                      // 4
  uint8_t id_has_been_set;
  uint8_t sType[16];
  uint8_t wakeups_per_sec;
  boolean enable_gyro;
  uint8_t country;
  float zero_cal_temperature;      // Temperature is stored here on power up for load cell zero calibration
  int32_t zero_cal_offset_raw;      // Raw offset from 0 - 200g, added to zero cal for load cell when unit powered on
  int32_t last_reading_raw;
  float gyro_activity = 0.3;
  int32_t K = 1;
} conf;
...

loadcell.h:

#ifndef LOADCELL_H
#define LOADCELL_H

#include <stdint.h>
#include "Arduino.h"

void setup();
void loop();
void HX711_power_off();
void HX711_power_on();
void HX711_power_on_zero();
int32_t HX711_read();
bool HX711_is_ready();
int32_t read_scale_average(uint8_t avg);
void read_tc();

#endif  // 

loadcell.cpp

#include <stdint.h>

#include "Arduino.h"

const byte Pin_HX_DOUT = 22;

const byte Pin_HX_SCK = 23;

const byte Pin_TMPV = 2;         // ADC2 - function uses the number only so 7 doesn't mean digital pin 7

const byte Pin_SPWR = 28;         // ** NEW Pin, D14/A0 = power for HX711 and Temp sensor High=on

float temperature = 0.0;

boolean USB_Enabled = false;

void read_tc();

void HX711_power_on(){

  

  pinMode(Pin_TMPV, INPUT);

  pinMode(Pin_SPWR, OUTPUT);                    // D5 = Load Cell & temperature sensor Power

  digitalWrite (Pin_SPWR, HIGH);                // Turn on

  delay(2);

  pinMode(Pin_HX_DOUT, INPUT);                  // D5 = Load Cell Data

  pinMode(Pin_HX_SCK, OUTPUT);                  // D5 = Load Cell Clock

}

void HX711_power_on_zero(){

  HX711_power_on();

  if (USB_Enabled == true){

    Serial.println(F("Power On - ZERO Calibrate"));

  }

  delay(10);

  read_tc();

  conf.zero_cal_temperature = temperature;  //This is where the conf error occurs

  

  if (USB_Enabled == true){

    Serial.print(F("Cal TC = "));

    Serial.println(temperature);

  }
...

There are 100’s of lines of code in this cpp file so I hope i’ve included enough to give the context.

Surely this is a simple question but I’ve been wrong before and I’ll be wrong again, any help will be greatly appreciated even if its sending me to a resource I didn’t see.

Cheers,
Dylan

My approach is to put structure declarations in a file “structs.h”:

struct config {
int display_contrast;
int display_brightness;
} ;

struct command {
byte motion;
byte direction;
} ;

Then define the structs in “main.cpp”:

struct config machine_config;
struct command machine_command;

In a file “main.h” make the structs visible to other files:

extern struct config machine_config;
extern struct command machine_command;

Now any file that accesses the structs should have:

#include “structs.h”
#include “main.h”

machine_config.display_contrast = 10;
machine_command.motion = FWD;

This works for me. It’s a bit untidy having to include “main.h” where structure access is required.

Moving the extern declarations from “main.h” to “structs.h” also works and is a bit neater in that it’s only necessary to

#include “structs.h”

in files where access to the structures is needed.

Ken.

In a new header file, say config.h, declare your struct without conf.

#infndef CONFIG_H
#define CONFIG_H

struct config_t {
...
};

#endif    // CONFIG_H

Then in main.cpp:

#include "config.h"
...
struct config_t conf;

In loadcell.h or any other file that needs to access conf:

#include "config.h"

extern struct config_t conf;

HTH

Cheers,
Norm.