c++ - 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。我不确定为什么会这样。
解决方案
推荐阅读
- spring - 我用spring security无法登录,密码总是错误
- typeahead - 有没有办法强制选择,即不允许在 TypeAHead 组件中输入自由文本?
- swift - 邮递员响应 200 但我的应用程序没有 [ERROR 403] Swift
- javascript - 根据先前的选择,下拉框在可填写的 PDF 中显示信息时出现问题 - JS
- swift - SwiftUI:NavigationLink 无意中点击并在表单内激活
- python - 有效地检查一列中的值是否属于其他两列定义的阈值
- java - 尽管在 Java 代码中使用了 lock(),但两个线程使用相同的方法?
- java - 将当前行作为参数发送到 Oracle DB 触发器中的 http 请求
- python - 序列化不会在创建方法中保存生成的值
- php - 尝试使用 PHP 将带有 UNICODE 代码的 JSON 字符串转换为 UTF8