[英]Servo is not moving while calling from my own library. Arduino
I made a class called "rudder.cpp" and its "rudder.h" I also have the Arduino main code. 我创建了一个名为“ rudder.cpp”的类,其名为“ rudder.h”的类也有Arduino主代码。
1) Arduino main code: calls the rudder class and move the servo. 1)Arduino主要代码:调用舵类并移动舵机。
2) rudder class: moves the servo from 0 to 180. (copy paste from Sweep code http://arduino.cc/en/Tutorial/Sweep ) 2)舵类:将舵机从0移到180。(从Sweep代码http://arduino.cc/en/Tutorial/Sweep复制粘贴)
3) rudder header: this hold all the definitions in rudder.cpp 3)rudder标头:包含rudder.cpp中的所有定义
My problem is when calling the move method from Rudder class , which is just a copy paste code from the sweep and I have made sure that it is working correctly before using it, 我的问题是从Rudder类调用move方法时,这只是扫描中的复制粘贴代码,在使用它之前,我已确保其正常工作,
I noticed there is a bad behavior is going on. 我注意到有一种不良行为正在发生。
1) The servo is getting hot 2) Servo is shaking and not moving smoothly like sweep 1)伺服器变热2)伺服器晃动且不像扫动一样平稳移动
Snapshot of the code: 代码快照:
Arduino main file Arduino主文件
#include <Servo.h>
Rudder rudder = Rudder();
void setup()
{
}
void loop()
{
rudder.moveRudder();
}
Rudder.cpp file: Rudder.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
}
}
Rudder.h 舵
#ifndef Rudder_h
#define Rudder_h
#include "Arduino.h"
#include <Servo.h>
class Rudder
{
Public:
Rudder();
moveRudder();
private:
Servo myServo;
};
#endif
Problem solved!. 问题解决了!。 It is wired!.
它是有线的! I have to attach myServo.attach(9);
我必须附加myServo.attach(9); every time when calling moveRudder()
每次调用moveRudder()时
So the new code is: 因此,新代码为:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.