想写在大于hold瞬间加一,之后不可以累加,test_i是可以变化的。为什么不成功

while(1)
{
if(speed1 == 0)
{
speed1 = 0;
count=0;
for(i=0;i<10;++i)
{
count += ll_adc_channel_sample_get(LL_ADC_CH_PB6);
}
test_i = count/10;
if(test_i <= hold)
{
sign = 0;
}
if((test_i > hold)&&(sign == 0))
{
sign = 1;
mode2_count += 1;
}
}

不是很明白你的意思,能详细说一下吗?

因为当test_i<=hold,sign还能再变回0
既然之后不让累加了,你把sign变回0干什么

这么改下试试:

while (1)
{
    if (speed1 == 0)
    {
        speed1 = 0;
        count = 0;
        for (i = 0; i < 10; ++i)
        {
            count += ll_adc_channel_sample_get(LL_ADC_CH_PB6);
        }
        test_i = count / 10;
        if (test_i <= hold)
        {
            sign = 0;
        }
        else if ((test_i > hold) && (sign == 0)) //修改
        {
            sign = 1;
            mode2_count += 1;
        }
    }
}

sign = 0;
while(1)
{
if(speed1 == 0)
{
speed1 = 0;
count=0;
for(i=0;i<10;++i)
{
count += ll_adc_channel_sample_get(LL_ADC_CH_PB6);
}
test_i = count/10;
//if(test_i <= hold)//这里条件满足时会清零sign导致后面的mode2_count有可能继续++,所以sign不能清零
//{
//sign = 0;
//}
if((test_i > hold)&&(sign == 0))
{
sign = 1;
mode2_count += 1;
}
}

了解一下“有限状态自动机”