这可能很简单,但我现在正把头撞在墙上。我试图将一个简单的程序分割成多个文件,这样我就可以添加到其中了。我在逻辑上把它分成几个部分。如果有人能给我指点我的错误,那就太棒了。
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;
#endif偏误
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 1谢谢!!
发布于 2022-03-16 13:44:39
您正在LoRa.cpp中创建一个LoRa.cpp对象,但它尚未被measure.cpp文件访问。要到达另一个文件中的文件范围中的对象,可以在声明中添加extern:
extern EBYTE Transceiver;此外,为了让measure.cpp文件了解什么类型的对象收发器(例如,它包含哪些方法),您应该在measure.cpp中包含EBYTE报头:
#include "EBYTE.h"一个使用EBYTE码的示例。
例如:
#include <Arduino.h>
#include "EBYTE.h"
#include "measure.h"
#include "MyData.h"
#include "LoRa.h"
unsigned long Last;
extern EBYTE Transceiver;
void measure()
{https://stackoverflow.com/questions/71493403
复制相似问题