一直研究apm。前几天买了一个,只可惜手头没遥控器T^T,只能做一个模拟的遥控了……
遥控输出pwm信号,用arduino感觉还好模拟,只是精度不是太高,不过也可以应付应付了= =
代码如此:
int pinsv=11;
int pinsh=10;
int pinv=A0;
int pinh=A1;
int pulse;
void servor(int pin_,int angle)
{
pulse=map(angle,0,180,500,2500);
digitalWrite(pin_,HIGH);
delayMicroseconds(pulse);
digitalWrite(pin_,LOW);
delayMicroseconds(20000-pulse);
}
void setup()
{
Serial.begin(9600);
pinMode(pinsv,OUTPUT);
pinMode(pinsh,OUTPUT);
pinMode(pinv,INPUT);
pinMode(pinh,INPUT);
Serial.println("Servor angle:");
}
void loop()
{
int v,h;
v=analogRead(pinv);
v=map(v,0,1023,5,175);
servor(pinsv,v);
Serial.print("The 'v' is:");
Serial.print(v,DEC);
delayMicroseconds(100);
h=analogRead(pinh);
h=map(h,0,1023,5,175);
servor(pinsh,h);
Serial.print(";The 'h' is:");
Serial.println(h,DEC);
delay(20);
}
|