【问题标题】:Why isn't my servo turning on keypress using an Arduino, Arduino IDE and Processing?为什么我的伺服没有使用 Arduino、Arduino IDE 和 Processing 打开按键?
【发布时间】:2014-06-12 17:11:08
【问题描述】:

我从这里偷了一些代码,http://www.instructables.com/id/Overview/?ALLSTEPS 并对其进行了大量修改,以尝试使用我的键盘控制伺服器的运动!目标是我将能够使用 WASD 键控制两个舵机。

任何人,我正在尝试使用 D 和 A 键来控制一个伺服的旋转。我使用以下 Arduino 代码:

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
   servoMain.attach(10); // servo on digital pin 10
   Serial.begin(9600);
}

int v = 90;

void loop()
{
   char ch = Serial.read(); // Reads serial input
   if (Serial.available()) {
     switch(ch) {
       case 'a':
         v = v + 1;
       case 'd':
         v = v - 1;
     }
   }
   servoMain.write(v);  // Turn servo to position designated by V variable.
   delay(100);          // Wait .1 second
}

所以基本上我设置了串行连接、设置伺服控制、vars 等。然后我尝试读取以下处理代码发送的 keyPresses 并尝试根据按下的键值以一种或另一种方式移动伺服.这是处理代码。

//Processing code:
import processing.serial.*;       
Serial port; // The serial port we will be using
int r,g,b;

void setup()
{
  println(Serial.list()); // List COM-ports (AUTHORS NOTE, I am not sure what this line is all about, I've never seen this pop up when I run the Processing code? Do I need it?)
  //select second com-port from the list (COM3 for my device)
  // You will want to change the [1] to select the correct device
  // Remember the list starts at [0] for the first option.
  port = new Serial(this, Serial.list()[0], 9600);
  // I need something to focus my cursor on so Processing can capture my keystrokes!
  size (600,600);
  r = 0;
  g = 0;
  b = 0;
}

void draw()
{
  background(r,g,b);
}
void keyPressed()
{
  switch (key) {
    //Send pressed key to serial conn.
    case 'a':
      port.write(key);
      break;
    case 'd':
      port.write(key);
      break;
    default:
      break;
  }
}

当我按下按键时,我的 Arduino 上的 RX LED 灯会亮起!所以一定有什么东西在某个地方发生了,但是伺服系统根本没有做任何事情。任何人都知道如何使我的脚本工作,以便我的 Arduino 将根据 A 或 D 键的按下以一种或另一种方式旋转伺服?

非常感谢!

【问题讨论】:

  • 如果你不让它等待按键,你能让伺服器做任何事情吗?你确定你的电路是对的吗?
  • 我建议采取一些步骤来测试您的问题所在:1) 取出您的伺服器,看看您是否可以点亮按键上的 LED。 2)直接写入您的伺服没有按键输入,以确保您的电路正确连接。如果你有图片甚至电路图,我想你可以上传给我们看。 3) 比上下 1 度更具戏剧性。您的问题可能是您在按键上只移动了一个度数,而不是再次移动。要从 90 变为 0,您必须按 'd' 90 次。这就是你的本意吗?
  • @eriese 我有一个简单的电路(相同的接线,相同的 PIN),我能够毫无问题地移动伺服!

标签: arduino processing


【解决方案1】:

我认为问题出在这里:

char ch = Serial.read(); // Reads serial input
if (Serial.available()) {
  switch(ch) {
    case 'a':
      v = v + 1;
    case 'd':
      v = v - 1;
}

事实上,你首先从缓冲区中读取一个字符,然后再调用函数 Serial.available()。此时您已经从缓冲区读取,因此它返回 0。您需要在读取内容之前调用 Serial.available()。当然你看到 rx pin 闪烁,但是 id 语句是 FALSE 因为缓冲区是空的。

你也忘了在 switch case 语句中加上 break

试试这个:

if (Serial.available()) {
    char ch = Serial.read(); // Reads serial input
    switch(ch) {
        case 'a':
          v = v + 1;
          break;
        case 'd':
          v = v - 1;
          break;
    }
}

这段代码对我有用(我没有伺服,抱歉):

void setup()
{
   pinMode(13, OUTPUT);
   Serial.begin(9600);
}

int v = 0;

void loop()
{
   if (Serial.available()) {
    char ch = Serial.read();
      switch(ch) {
        case 'a':
          v = 1;
          break;
        case 'd':
          v = 0;
          break;
      }
   }
   digitalWrite(13, v);
}

【讨论】:

  • 感谢您的精彩输入@GiulioG - 我今晚回家后肯定会尝试这个,再次感谢!我会回来报告的:)
  • 花了一段时间才回复您,进行了切换,它完全有效!这是非常令人兴奋的进展 :) 非常感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多