c++ - 从我自己的库调用时,伺服器没有 move 。阿杜诺

标签 c++ header arduino hardware move

我制作了一个名为“rudder.cpp”的类及其“rudder.h” 我也有 Arduino 主代码。

1)Arduino主代码:调用方向 Helm 类, move Helm 机。

2) 方向 Helm 类:将 Helm 机从 0 move 到 180。(从扫描代码复制粘贴 http://arduino.cc/en/Tutorial/Sweep )

3) Helm 头:这包含 rudder.cpp 中的所有定义

我的问题是从 Rudder 类调用 move 方法时,它只是从扫描中复制粘贴代码,我已经确定 它在使用之前工作正常,

我注意到正在发生不良行为。

1) Helm 机发热 2) Helm 机抖动,不像扫地那样顺畅 move

代码截图:

Arduino 主文件

#include <Servo.h>
Rudder rudder = Rudder();
void setup() 
{ 

}

void loop() 
{ 
    rudder.moveRudder();
}

Helm 机.cpp文件:

#include "Rudder.h"
#include <Rudder.h>
#include <Servo.h>

Servo myServo;

Rudder::Rudder()
{
       myServo.attach(9);
}

// Sweep code!
//
void Rudder::moveRudder()
{
    for(pos = 0; pos < 180; 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(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

方向 Helm .h

#ifndef Rudder_h
#define Rudder_h

#include "Arduino.h"
#include <Servo.h>

class Rudder
{
   Public:
   Rudder();
   moveRudder();
   private:
   Servo myServo;

};

#endif

最佳答案

问题解决了!它是有线的!我必须附加 myServo.attach(9);每次调用 moveRudder() 时

所以新的代码是:

void Rudder::moveRudder()
{
   myServo.attach(9);             //Here the solution


    for(pos = 0; pos < 180; 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(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

关于c++ - 从我自己的库调用时,伺服器没有 move 。阿杜诺,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160980/

相关文章:

在 Win7 上的 Cygwin 64 上找不到 BZIP2 库(64)

gps - 有谁知道如何使用 Arduino 和 SIM900 进行三角定位?

php - 如何阻止 PHP 在文件开头添加 LF 前缀

gcc - 如何让 Makefile 自动重建包含修改后的头文件的源文件? (在 C/C++ 中)

c - 从 5v 到 3.3v 的电压电平转换

Android-Arduino蓝牙通讯: Data is not received properly in Android app

c++ - 对 std::array 的模板化引用未在 MSVC 中调用 SFINAE* 。漏洞?

c++ - 我需要帮助分离 C++ LNK2005 和 LNK1169 中的类代码

c++ - 从 MFC UI 打开新进程

c++ - 需要修复 C++ 脚本(不是我的)