在下载到开发板之前要选择好板和端口,具体参见:
代码
int Left_motor_go=8;
int Left_motor_back=9;
int Right_motor_go=10;
int Right_motor_back=11;
void setup()
{
pinMode(Left_motor_go,OUTPUT);
pinMode(Left_motor_back,OUTPUT);
pinMode(Right_motor_go,OUTPUT);
pinMode(Right_motor_back,OUTPUT);
}
void brake(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
delay(time * 100);
}
void run(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
delay(time * 100);
}
void left(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void spin_left(int time)
{
digitalWrite(Right_motor_go,HIGH);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void right(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
delay(time * 100);
}
void spin_right(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);
delay(time * 100);
}
void back(int time)
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);
digitalWrite(Left_motor_go,HIGH);
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,150);
analogWrite(Left_motor_back,0);
delay(time * 100);
}
void loop()
{
int i;
delay(2000);
run(10);
back(10);
brake(5);
for(i=0;i<5;i++)
{
run(10);
brake(1);
}
for(i=0;i<5;i++)
{
back(10);
brake(1);
}
for(i=0;i<5;i++)
{
left(10);
spin_left(5);
}
for(i=0;i<5;i++)
{
right(10);
spin_right(5);
}
for(i=0;i<10;i++)
{
right(1);
brake(1);
}
for(i=0;i<10;i++)
{
left(1);
brake(1);
}
for(i=0;i<10;i++)
{
left(3);
right(3);
}
for(i=0;i<10;i++)
{
spin_left(3);
brake(3);
}
for(i=0;i<10;i++)
{
spin_right(3);
brake(3);
}
}