用键盘或其他硬件按钮控制伺服? [英] Control servo with keyboard or other hardware buttons?

查看:31
本文介绍了用键盘或其他硬件按钮控制伺服?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Arduino,几乎不知道更高级的东西.这看起来很简单.现在我是一个通常喜欢将两个设备集成在一起的人,所以我想知道我是否可以使用计算机键盘或连接到 Arduino 板的两个硬件按钮来控制伺服.

I have just gotten started with Arduino and barely have any idea about more of the advanced stuff. It seems pretty straightforward. Now I'm one who usually likes to integrate two devices together, so i was wondering if i could control a servo with the computer's keyboard or two hardware push buttons attached to the Arduino board.

如果有帮助,我使用的是 Arduino Uno 板.这是我现在用来扫描伺服的示例代码

In case it helps, I'm using an Arduino Uno board. Here is the example code i am using to sweep the servo for now

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
            // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(11);  // attaches the servo on pin 9 to the servo object 
} 


void loop() 
{ 
  for(pos = 0; pos < 45; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 45; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
}

  1. 现在,假设我想通过按下我电脑键盘上的左/右箭头键.我怎么去关于这样做?

  1. Now, let's say I wanted to change the servo's angle via pressing the left/right arrow keys on my computer's keyboard. How would i go about doing that?

或者,如果我将两个按钮连接到 Arduino,并按下一个将向左或向右移动伺服,具体取决于在按钮上.我会将按钮插入哪些端口?任何代码示例或图表会大有帮助!

Alternatively, what if i attached two push buttons to the Arduino, and pressing one would move the servo either left or right depending on the the button. Which ports would i plug the buttons into? Any code samples or diagrams would greatly help!

推荐答案

要移动连接到计算机的 arduino 上的伺服器,您需要两个组件.

To move a servo attached to an arduino attached to a computer you will need two components.

您需要在计算机上安装软件来接受键盘命令并通过串行端口向 arduino 发送命令.我会推荐像 python 或 java 这样的语言来做到这一点,因为一个简单的应用程序可以很容易地编写.

You will need software on your computer to accept keyboard commands and send commands to the arduino via the serial port. I would recommend a language like python or java to do that as a simple app can written quite easily.

查看此 playground 链接,了解使用 Java 的示例.对于 Python 中的示例,请查看 这个项目.

Check this playground link for an example of using Java. And for an example in python check out this project.

arduino 中内置了一个错误/功能,当您继续操作时,它会让您感到悲伤.arduino 设计为在通过 USB 进行串行连接时自动重置.此页面详细描述了该问题并列举了几种处理方法.

There is a bug/feature built into the arduino that will give you grief as you go on here. The arduino is designed to auto reset when a serial connection is made to it via usb. This page has a detailed description of the issue and cites several ways to deal with it.

您需要修改 arduino 上的草图以侦听串行端口并根据从计算机收到的命令调整伺服的位置.查看上面的python链接.这是一个完整的(硬件、PC 软件和 arduino 草图)项目,旨在完成与您正在尝试做的非常相似的事情.

You will need to modify the sketch on the arduino to listen to the serial port and adjust the servo's position based on the commands received from your computer. Check out the python link above. It is an complete (hardware, pc software and arduino sketch) project designed to do something very similar to what you are trying to do.

我建议您从任一组件开始,然后尝试让它继续运行.当您遇到问题时,请发布您的代码,有人会很乐意提供进一步帮助.

I recommend you start with either component and try to get it going. As you run into problems, post your code and someone will be glad to help further.

至于第二个问题,在arduino中添加按钮相当简单.您将它们连接到数字输入.网络上有数百个示例.搜索向 arduino 添加按钮",看看你会得到什么.(大声笑... 130 万次点击).再次尝试并发布详细信息以获取更多帮助.

As for the second question, adding buttons to the arduino is fairly simple. You will connect them to digital inputs. There are hundreds of examples on the web. Search for "add button to arduino" and see what you get. (lol... 1.3 million hits). Here again, try it and post specifics for more help.

这篇关于用键盘或其他硬件按钮控制伺服?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆