arduino伺服器和延迟

问题描述:

我有一个伺服器,将扫描从0到180度,然后回到0然而我不能使用delay()函数,因为我需要扫描被运动传感器中断。我写了一些代码,但扫描不平滑,伺服有时会跳转。我没有做好什么?在此先感谢arduino伺服器和延迟

int x = 0; 

void pivot(){ 
for (pos = 0; pos <= 180; pos += 5) { // goes from 0 degrees to 180 degrees 
// in steps of 1 degree 
myservo_2.write(pos);    // tell servo to go to position in variable 'pos' 
//delay(15); 
    for (x; x<10; x++) 
    { 
    delay(1); 
    ping(); 
    } 
    x=0; 
} 
for (pos = 180; pos >= 0; pos -= 5) { // goes from 180 degrees to 0 degrees 
myservo_2.write(pos);    // tell servo to go to position in variable 'pos' 
//delay(15); 
for (x; x<10; x++) 
    { 
    delay(1); 
    ping(); 
    } 
    x=0; 
} 
} 

我认为问题很简单,你正试图扫过得太快。

以5为增量的180度只有36步,所以这就是72。在1 ms延迟循环10次的情况下,您试图将伺服器全程移动并返回0.72秒。这比典型的RC伺服可以移动的速度稍快。对于基本伺服系统而言,60度时0.24秒是典型值,所以如果您的满量程约为120度,则需要大约1秒。这就是说,就我所知,你可能正在使用60度范围的快速伺服器。

另一种可能性是,当你说不光滑时,并不意味着它随机跳动,而只是它抖动。我期望每10毫秒跳5度就会得到一点跳跃,10赫兹接近哈利怠速的频率,而5度过大的运动在该速度下显得平滑。

您需要伺服器在10点钟停止,还是可以连续扫动?如果可以的话,我会减少每站停车次数,并按比例减少步长。 IE步骤1度并ping 2次。