I am a beginner in MultiWii writing. After my quad be able to fly, I want to add some function(not controlling a camera) during its ANGLE_MODE by controlling a servo on it through pin A2.
I choose to control it by the following code:
Code: Select all
#include <Servo.h>
Servo SwitchServo;
void setup() {
........
SwitchServo.attach(A2);
}
void loop() {
......
if (f.ANGLE_MODE)
SwitchServo.write(90);
else
SwitchServo.write(30);
}
The problem is that after using this code, 2 motors of the quad seems to lose its power and constantly make beep sound, while the servo rotates and sqeaks all along.
When I delete those codes, it all become to normal state.
I also tried another way but it doesn't work at all. The motors are all right but the servo doesn't move a bit.
Code: Select all
void setup() {
........
pinMode(A2, OUTPUT);
}
void loop() {
......
if (f.ANGLE_MODE)
analogWrite(A2, 127);
else
analogWrite(A2, 42);
}
While I tested the pin A2 to make it sweep by the following code and it works well.
Code: Select all
#include <Servo.h>
Servo myservo;
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(A2); // attaches the servo on pin A2 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{
myservo.write(pos);
delay(15);
}
delay(1000);
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos);
delay(15);
}
delay(1000);
}
I do not quite understand the reason so could you guys give any advise or point out my fault in this?
Thanks!