if语句:比较AnalogRead与int

时间:2019-03-22 01:20:56

标签: c++ arduino

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

 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。我不确定为什么会这样。

0 个答案:

没有答案