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

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

问题描述

我刚刚得到开始与Arduino的,几乎有更多的先进的东西的想法。看来pretty简单。现在我一个谁平时最喜欢的两个设备集成在一起,所以我想知道如果我能控制与计算机的键盘或两个硬件按钮伺服连接到Arduino板。

在情况下,它可以帮助,我使用一个Arduino板乌诺。这里是例如code我使用扫伺服现在

  //扫描
//由巴拉甘< HTTP://barraganstudio.com>
//这个例子code是在公共领域。
#包括LT&;&Servo.h GT;伺服myservo; //创建伺服对象来控制伺服
            //最多8个伺服对象可以创建INT POS = 0; //变量来存储伺服位置无效设置()
{
  myservo.attach(11); //附加引脚9伺服伺服对象
}
无效循环()
{
  为(位置= 0; POS及下; 45;位置pos + = 1)//从0度变到180度
  {//在1度的步骤
    myservo.write(POS) //告诉伺服去位置变量POS
    延迟(10); //等待15ms的伺服到达位置
  }
  对于(POS = 45; POS> = 1; POS-= 1)//从180度到0度
  {
    myservo.write(POS) //告诉伺服去位置变量POS
    延迟(10); //等待15ms的伺服到达位置
  }
}


  1. 现在,让我们说,我想通过pressing的改变伺服的角度
    我的电脑键盘上的左/右箭头键。我将如何去
    这样做呢?


  2. 另外,如果我连着两个按钮到Arduino是什么,
    和pressing人会动伺服或左或右视
    上的按钮。哪些端口,我会堵塞按钮进入?任何
    code样品或图表将极大地帮助!



解决方案

要移动连接到连接到您需要两个组件的计算机一个Arduino的伺服。

您将需要您的计算机上的软件来接受键盘命令,并通过串口发送命令到Arduino。我建议像Python或Java等语言做一个简单的应用程序可以很容易地编写。

勾选此操场链接使用Java的一个例子。而对于一个例子,蟒蛇退房这个项目

有就是你解忧,你这里走的建成,这将使Arduino的一个bug /功能。 Arduino的设计为自动复位时,串行连接到它通过USB进行。 此页面有问题的详细说明,并列举几种方法来对付它。

您将需要修改的Arduino的草图根据您的计算机接收的命令听串口和调整伺服的位置。退房蟒蛇以上链接。这是一个完整的(硬件,电脑软件以及Arduino的草图)项目,旨在做的非常相似,你正在尝试做一些事情。

我建议你开始与任一组件,设法得到它去。当你遇到问题,发表您的code,有人会很乐意提供进一步的帮助。

至于第二个问题,将按钮添加到Arduino是相当简单。您将它们连接到数字输入。有数以百计的网站上的例子。搜索按钮添加到Arduino的,看看你会得到什么。 (笑...... 130万次点击)。在这里,尝试和更多的帮助张贴细节。

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.

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. 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?

  2. 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!

解决方案

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

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.

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

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.

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.

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天全站免登陆