Not declared in this scope. I'm stuck no matter what I try
Matthew Barrera
This is probably simple but I'm banging my head against the wall for sometime now. I'm trying to split a simple program into multiple files so I can add to it. I'm trying to logically split it into parts. If anyone can give me some pointers as to my mistakes that'd be awesome.
Main.cpp
#include <Arduino.h>
#include "measure.h"
#include "LoRa.h"
void setup()
{ Serial.begin(9600); // STart serial for monitoring/debugging Serial2.begin(9600); // Start 2nd serial interface for use with EBYTE module Serial.println("Starting Reader"); Serial.println(Transceiver.init()); // This init will set the pinModes for you Transceiver.PrintParameters(); // Display parameters of EBYTE (Can be changed. See example sketches)
}
//=================================================================
//======LOOP=======================================================
//=================================================================
void loop()
{ measure(); // Take measurements delay(1000); // For testing. Will not exist in final
}measure.cpp
// Takes various measurements, updates struct and sends to receiver
#include <Arduino.h>
#include "measure.h"
#include "MyData.h"
#include "LoRa.h"
unsigned long Last;
void measure()
{ // measure some data and save to the structure MyData.Count++; MyData.Bits = analogRead(A0); MyData.Volts = MyData.Bits * (5.0 / 1024.0); // Send struct Transceiver.SendStruct(&MyData, sizeof(MyData)); // Let us know something was sent Serial.print("Sending: "); Serial.println(MyData.Count);
}measure.h
#ifndef MEASURE_H
#define MEASURE_H
#include <Arduino.h>
#include "LoRa.h"
void measure();
#endifLoRa.cpp
// Somewhere to define/declare everything to do with the Ebyte E32 LoRa module
#include <Arduino.h>
#include "EBYTE.h"
#include "LoRa.h"
#define PIN_RX 16 // Serial2 RX (connect this to the EBYTE Tx pin)
#define PIN_TX 17 // Serial2 TX pin (connect this to the EBYTE Rx pin)
#define PIN_M0 4 // D4 on the board (possibly pin 24)
#define PIN_M1 22 // D2 on the board (possibly called pin 22)
#define PIN_AX 21 // D15 on the board (possibly called pin 21)
// create the transceiver object, passing in the serial and pins
EBYTE Transceiver(&Serial2, PIN_M0, PIN_M1, PIN_AX);LoRa.h
#ifndef LORA_H
#define LORA_H
#include "Arduino.h"
#endifMyData.h
#ifndef MYDATA_H
#define MYDATA_H
struct DATA
{ unsigned long Count; int Bits; float Volts; float Temp;
} MyData;
#endifERRORS
src\main.cpp: In function 'void setup()':
src\main.cpp:45:20: error: 'Transceiver' was not declared in this scope Serial.println(Transceiver.init()); // This init will set the pinModes for you ^
src\measure.cpp: In function 'void measure()':
src\measure.cpp:18:5: error: 'Transceiver' was not declared in this scope Transceiver.SendStruct(&MyData, sizeof(MyData)); ^
*** [.pio\build\esp32doit-devkit-v1\src\measure.cpp.o] Error 1
*** [.pio\build\esp32doit-devkit-v1\src\main.cpp.o] Error 1Thanks!!
101 Answer
You are creating a Transceiver object in LoRa.cpp, but it hasn't been made accessible to your measure.cpp file. To reach an object that is in file scope in a different file, you add extern in the declaration:
extern EBYTE Transceiver;Also, for the measure.cpp file to understand what kind of object Transceiver is (for example which methods it contains) you should include the EBYTE header in measure.cpp:
#include "EBYTE.h"An example using the EBYTE code.
For example:
#include <Arduino.h>
#include "EBYTE.h"
#include "measure.h"
#include "MyData.h"
#include "LoRa.h"
unsigned long Last;
extern EBYTE Transceiver;
void measure()
{