|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
include <MsTimer2.h>
int i;
int servopin=8;
int count_p1;
int myangle;//定义角度变量
int pulsewidth;//定义脉宽变量
void setup()
{
pinMode(servopin,OUTPUT);
MsTimer2::set(10,Servo_around);
MsTimer2::start();
}
void loop()
{
}
void servpulse(int servopin,int myangle)
{
for(int i=0;i<=5;i++)
{
pulsewidth=(myangle*11)+500;
digitalWrite(servopin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
delay(20-pulsewidth/1000);
}
}
void D_1()
{
count_p1++;
if(count_p1>=120)
{
count_p1=120;
i++;
}
}
void D_2()
{
count_p1--;
if(count_p1<=60)
{
count_p1=60;
i=0;
}
}
void Servo_around()
{
switch(i)
{
case 0:servpulse(servopin,count_p1);D_1();break;
case 1:servpulse(servopin,count_p1);D_2();break;
default:break;
}
} |
|