简体   繁体   English

Arduino伺服没有响应按键

[英]Arduino servo not responding to button press

I'm pretty sure there is a stupid error here but I'm afraid I can't for the life of me work it out! 我敢肯定这里有一个愚蠢的错误,但我恐怕一生都无法解决!

Simple test program that gets the error: 得到错误的简单测试程序:

#include <Servo.h> 

Servo myservo;

int testPIN = 13;
int inputPIN = 5;

void setup() 
{ 
  myservo.attach(8);
  pinMode(testPIN, OUTPUT);
  pinMode(inputPIN, INPUT);
} 

void loop() 
{ 
  if (digitalRead(inputPIN) == HIGH) 
  {
    digitalWrite(testPIN, HIGH);
    myservo.write(90);
  }
  else
  {    
    digitalWrite(testPIN, LOW);
    myservo.write(0);
  }
}

The arduino sweep example ( http://arduino.cc/en/Tutorial/Sweep ) works, so I'm fairly confident the electronics works. arduino扫描示例( http://arduino.cc/en/Tutorial/Sweep )可以工作,因此我对电子产品的工作很有信心。

The testPIN also goes on and off as expected so the if statement is working as expected. testPIN也会按预期打开和关闭,因此if语句按预期运行。

Any ideas/suggestions welcome! 欢迎任何想法/建议!

EDIT - Sorry the error is that the servo doesn't move at all 编辑-对不起,错误是伺服器根本没有移动

EDIT 2 - Something a bit odd is going on here. 编辑2-此处有些奇怪。 If I copy/paste the sweep loop into the if clause, the servo reacts as expected (ie input = high makes the servo run a sweep loop, which it doesn't break out of until it reaches the end of, as expected). 如果我将扫描循环复制/粘贴到if子句中,则伺服器将按预期方式做出反应(即input = high使伺服器运行一个扫描循环,直到达到预期的结束,它才会中断)。 My immediate thought was delays were needed, but they seem to make no difference no matter how long they are or where they are added in the if/else clauses. 我立即想到的是需要延迟,但是无论延迟多久或在if / else子句中添加到何处,延迟似乎都没有影响。

I don't know which arduino board you have, but on the arduino uno, i'm pretty sure that the pin 8 is not a PWM output. 我不知道您拥有哪块arduino板,但是在arduino uno上,我很确定引脚8不是PWM输出。 And you can not run a servo on a non-PWM output. 而且您不能在非PWM输出上运行伺服。

See this image of the Uno board, and notice that there is no tilde (the indication that a port supports PWM) on pin 8: 请参见Uno板的此图像,并注意在引脚8上没有波浪号(表示端口支持PWM):

Arduino Mega2560开发板

Have you tried using SoftwareServo.h instead? 您是否尝试过使用SoftwareServo.h? This example looks like what you are trying to accomplish: http://playground.arduino.cc/ComponentLib/servo 该示例看起来像您要完成的任务: http : //playground.arduino.cc/ComponentLib/servo

The sweep program that you're linking to is using pin 9, which is a PWM on an uno. 您链接到的扫描程序使用的是引脚9,这是uno上的PWM。 Your code is using pin 8, which is NOT a PWM output. 您的代码使用的是引脚8,它不是PWM输出。 Switch your servo over to pin 9 and change the attach in your code to pin 9 and, assuming that this is your only issue, your code should work. 将您的伺服系统切换到引脚9,并将代码中的附件更改为引脚9,并假设这是您的唯一问题,您的代码应该可以工作。

As suggested in the comments, I've just written a function that moves the servo slowly. 如评论中所建议,我刚刚编写了一个使伺服器缓慢移动的函数。 Not an elegant solution but servo response time isn't issue so it does the trick. 这不是一个很好的解决方案,但是伺服响应时间不是问题,因此可以解决问题。

Thanks for all the help and suggestions, and to @praks411 for the wrapper function work around. 感谢所有帮助和建议,并感谢@ praks411提供包装功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM