首页 > 解决方案 > If 语句:将analogRead 与int 进行比较

问题描述

我正在尝试完成我的心率检测代码,该代码应该确定每个周期的峰值并使用它来计算心率。

 void setup() {
  Serial.begin(9600);
}

void loop() {
  int i = 0;
  int peak = 0;
  unsigned long startTime = 0;
  unsigned long endTime = 0;

  startTime = millis();

//This section of the code compares the incoming sensor values to an initial value and replaces that initial value with a higher sensor value and does this for 2 seconds

while(millis() - startTime<=2000){
    if(peak <= analogRead(A0)){
      peak = analogRead(A0);
    }
  }
  endTime = millis();
  Serial.println(peak);//this is the highest value it found after 2 seconds

//This section of the code is supposed to take the peak value from above and compare it with more sensor values for 3 seconds and increment the counter every time the peak value and the sensor value are equal.

while(millis() - endTime <= 3000){
    if(analogRead(A0)== peak){
      i++;
     }
    }
    Serial.println(i);
}

我遇到的问题是我得到的 i 数字非常不一致,高达 500 和低至 10。我不确定为什么会这样。

标签: c++arduino

解决方案


推荐阅读