我打开一个阀门让流体流动。这里测量的压力是流体被拉入系统的压力。我正在尝试仅测量前10个Pdiff(PMax-PMin)的平均值。一旦计算出平均值,阀就关闭。如何多次计数峰值?
并根据这个平均值,阀门将再次打开和关闭一个峰值,然后为2个峰值和3个峰值等。我将压力值存储在数组中,并将该值与其前后值进行比较,得到最大值和最小值。
我打开一个阀门让流体流动。这里测量的压力是流体被拉入系统的压力。我正在尝试仅测量前10个Pdiff(PMax-PMin)的平均值。一旦计算出平均值,阀就关闭。如何多次计数峰值?
并根据这个平均值,阀门将再次打开和关闭一个峰值,然后为2个峰值和3个峰值等。我将压力值存储在数组中,并将该值与其前后值进行比较,得到最大值和最小值。
您使用++peakcounter
增加您的peakcounter但随后立即在if(peakcounter==0)
if块设置peakcounter=0
既然您重置peakcounter,你永远不会peakcounter == 2
if (valstate == false && Pdelta >= average)
{
{
++peakcounter; // keeps the count of how many times the value has gone
above average
}
// Checks for the number of times and then performs action
if (peakcounter == 1) {
digitalWrite(4, HIGH);
startTime = millis();
valstate = true;
peakcounter = 0; //the offending line
}
你需要什么(注意:代码没有优化,我不完全理解你需要什么,但是这应该解决你写的问题)
int currentMax = 0;
// your code here....
if (valstate == false && Pdelta >= average){
++peakcounter;
if(peakcounter > currentMax){
// Checks for the number of times and then performs action
if (peakcounter == 1) {
digitalWrite(4, HIGH);
startTime = millis();
valstate = true;
peakcounter = 0;
currentMax++;
}
//the rest of your peakcount checking code here
}
您能否提供一个使用开关盒的例子 – Naley