C++ 如何获得一个连续提供数据以输出一位整数值的输入
简要总结一下我的项目,这是一个智能停车系统,我让停车用户知道停车场是否空置。我正在实现一个包含1个协调器和2个路由器的XBee网络。我有两个传感器,一个在出口,一个在入口。这两个传感器是路由器,它们收集的任何数据都将传输到协调器(输出)。两个路由器具有相同的代码,即: 输入代码(传输): 输出代码也非常简单。让我知道是否有任何可能的方法来实现我正在尝试做的事情,如果我遗漏了任何其他细节,也让我知道C++ 如何获得一个连续提供数据以输出一位整数值的输入,c++,networking,arduino,wireless,xbee,C++,Networking,Arduino,Wireless,Xbee,简要总结一下我的项目,这是一个智能停车系统,我让停车用户知道停车场是否空置。我正在实现一个包含1个协调器和2个路由器的XBee网络。我有两个传感器,一个在出口,一个在入口。这两个传感器是路由器,它们收集的任何数据都将传输到协调器(输出)。两个路由器具有相同的代码,即: 输入代码(传输): 输出代码也非常简单。让我知道是否有任何可能的方法来实现我正在尝试做的事情,如果我遗漏了任何其他细节,也让我知道 谢谢大家! 在传感器传输代码中,这是一个非常常见的问题,最好在源代码处解决 //... bool
谢谢大家! 在传感器传输代码中,这是一个非常常见的问题,最好在源代码处解决
//...
bool PreviousDetection = false;
void loop()
{
bool Detection = digitalRead(Sensor);
// do not do anything when state hasn't changed.
if (Detection != PreviousDetection)
{
if (Detection)
Serial.println("Car Exit");
else
Serial.println("Clear");
}
PreviousDetection = Detection;
}
您可能需要添加一些去抖动以降低错误读数的风险
//...
// Debounce thresholds the number depends on the frequency of readings,
// speeed of cars, sensitivity and placement of your sensor...
// Dirt, sun exposure, etc... may influence readings in the long term.
const int LowDebounceThreshold = 3;
const int HighDebounceThreshold = 3;
bool PreviousDetection = false;
int DebounceCounter = 0;
bool DebouncedPreviousDetection = false;
void loop()
{
bool Detection = digitalRead(Sensor);
//
if (Detection == PreviousDetection)
++DebounceCounter; // this will rollover, but will not affect
// DebouncedDetection in any meaningfull way.
else
DebounceCounter = 0;
PreviousDetection = Detection;
bool DebouncedDetection = PreviousDebouncedDetection;
if (Detection && DebounceCounter >= HighDebounceThreshold)
DebouncedDetection = true;
else if (!Detection && DebounceCounter >= LowDebounceThreshold)
DebouncedDetection = false;
if (DebouncedDetection != PreviousDebouncedDetection)
{
PreviousDebouncedDetection = DebouncedDetection;
if (DebouncedDetection)
Serial.println("Car Exit");
else
Serial.println("Clear");
}
}
⟼请记住,保持代码尽可能有条理是非常重要的,尤其是在学习和询问有关堆栈溢出的问题时。有助于沟通结构,更重要的是,有助于我们快速找到问题的根源,而无需花费大量时间试图解码正在发生的事情。明白了!我认为我在保持整洁方面做得相当好,但下次我会努力做得更好。非常感谢。
//...
bool PreviousDetection = false;
void loop()
{
bool Detection = digitalRead(Sensor);
// do not do anything when state hasn't changed.
if (Detection != PreviousDetection)
{
if (Detection)
Serial.println("Car Exit");
else
Serial.println("Clear");
}
PreviousDetection = Detection;
}
//...
// Debounce thresholds the number depends on the frequency of readings,
// speeed of cars, sensitivity and placement of your sensor...
// Dirt, sun exposure, etc... may influence readings in the long term.
const int LowDebounceThreshold = 3;
const int HighDebounceThreshold = 3;
bool PreviousDetection = false;
int DebounceCounter = 0;
bool DebouncedPreviousDetection = false;
void loop()
{
bool Detection = digitalRead(Sensor);
//
if (Detection == PreviousDetection)
++DebounceCounter; // this will rollover, but will not affect
// DebouncedDetection in any meaningfull way.
else
DebounceCounter = 0;
PreviousDetection = Detection;
bool DebouncedDetection = PreviousDebouncedDetection;
if (Detection && DebounceCounter >= HighDebounceThreshold)
DebouncedDetection = true;
else if (!Detection && DebounceCounter >= LowDebounceThreshold)
DebouncedDetection = false;
if (DebouncedDetection != PreviousDebouncedDetection)
{
PreviousDebouncedDetection = DebouncedDetection;
if (DebouncedDetection)
Serial.println("Car Exit");
else
Serial.println("Clear");
}
}