我对C++和除了Arduino的基础知识以外的任何东西都是新手,所以我坚持使用它。我得到了一个函数,让它包含在我的代码中,对我正在读取的感应值进行一些计算。我在这里和一般的网络上搜索和阅读了很多类似的话题,但没有找到任何帮助我解决我缺乏理解的东西。
我设置了一个基本的Arduino草图和一个循环来读取传感器的值,一切正常。然后,我将头文件包含在内,并将该函数添加到循环外的代码底部,以查看它是否可以编译,它做到了,没有任何错误。所以现在需要调用该函数并向其传递两个值,millis()和我刚刚读取的感应值。
头文件的一部分...
struct TCS1000v {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
.....
};
typedef struct TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime);草图的回路部分
void loop() {
// reading the sensor...
unsigned short int u16CycleTime = millis();
unsigned short int u16RawVal = adc.readsensor(channel);
// the function to call - not sure about this?
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
}所提供的函数...
void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
{
ptCS1000->u16RawVal = u16RawVal;
ptCS1000->u16CycleTime = u16CycleTime;
//.... and the rest of the code in the function
}编译错误...
test.ino: In function 'void loop()':
cs1000:30:25: error: expected primary-expression before '*' token
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:27: error: 'ptCS1000' was not declared in this scope
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:37: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:67: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
exit status 1
expected primary-expression before '*' token所以结构是在头文件中设置的,但是问题似乎出在指针上?或者我只是没有正确地调用函数?
发布于 2019-08-30 12:12:22
最后,我不得不设置一个全局变量,现在一切都正常了。感谢您的关注:)
TCS1000v g_tCS1000v;
vCalcCS1000v2(&(g_tCS1000v), RawVal, CycleTime);发布于 2019-08-23 07:52:54
在头文件中,您定义了一个名为TCS1000v的新类型,但并不正确。使用
typedef struct {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
} TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000,
unsigned short int u16RawVal,
unsigned short int u16CycleTime);https://stackoverflow.com/questions/57608111
复制相似问题