Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回不可靠值的Arduino中断_Arduino_Interrupt_Sensors - Fatal编程技术网

返回不可靠值的Arduino中断

返回不可靠值的Arduino中断,arduino,interrupt,sensors,Arduino,Interrupt,Sensors,我有一个简单的应用程序,使用一个装有簧片开关的传感器来计算水流量。 因此,应用程序应该只计算开关关闭的次数 我的第一个代码是: const int sensorPin = 2; volatile int counter = 0; void setup() { Serial.begin(115200); pinMode(sensorPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(sensorPin), sensorI

我有一个简单的应用程序,使用一个装有簧片开关的传感器来计算水流量。 因此,应用程序应该只计算开关关闭的次数

我的第一个代码是:

const int sensorPin = 2;
volatile int counter = 0;

void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(sensorPin), sensorISR, FALLING);
}

void loop() {
  Serial.print("Counter: ");
  Serial.println(counter);
}

void sensorISR() {
  counter++;
}
一旦一瓶20升的酒装满了,柜台上就会显示出大约120升

然后我将代码更改如下:

const int sensorPin = 2;
volatile int counter = 0;

void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(sensorPin), sensorISR, FALLING);
}

void loop() { }

void sensorISR() {
  counter++;
  Serial.print("Counter: ");
  Serial.println(counter);  
}
然后计数器下降到40(使用相同的20升瓶子)

计数应该是20L,但这不是我的问题,因为它是簧片开关反弹的结果(我将解决后者)。 由于该项目将有3个传感器和3个isroutine,我想知道为什么将Serial.print()命令放入主循环会产生如此奇怪的结果

谢谢
保罗

序列打印语句依赖于在ISR期间禁用的中断。所以Serial.print语句不属于ISR

你的计数下降的原因是,现在你的ISR需要更长的时间来执行,它掩盖了一些反弹。有无数关于如何用Arduino去弹跳的教程。你一定能找到

最简单的两种方法是在引脚和接地之间使用电容器进行硬件去抖动,或者仅使用毫秒或微秒来记录中断发生的时间,并忽略在此时间内发生的任何中断