全部代码----made by 邓翔
void rig(int mod,int b,int rud1,int offset1) //右边舵机
{
rud1-=offset1;
int rud2;
rud2=3000-rud1;
if(mod==1)
{
digitalWrite(b,HIGH);
delayMicroseconds(rud1);
digitalWrite(b,LOW);
delay(20);
}
if(mod==-1)
{
digitalWrite(b,HIGH);
delayMicroseconds(rud2);
digitalWrite(b,LOW);
delay(20);
}
if(mod==0)
{
digitalWrite(b,HIGH);
delayMicroseconds(1500);
digitalWrite(b,LOW);
delay(20);
}
}
void lef(int mod,int b,int rud1,int offset1) //左边舵机
{
rud1-=offset1;
int rud2;
rud2=3000-rud1-offset1;
if(mod==1)
{
digitalWrite(b,HIGH);
delayMicroseconds(rud1);
digitalWrite(b,LOW);
delay(20);
}
if(mod==-1)
{
digitalWrite(b,HIGH);
delayMicroseconds(rud2);
digitalWrite(b,LOW);
delay(20);
}
if(mod==0)
{
digitalWrite(b,HIGH);
delayMicroseconds(1500);
digitalWrite(b,LOW);
delay(20);
}
}
void run(int mod,int le,int ri,int rud1,int offset1,int offset2) //左右舵机
{
switch(mod)
{
case 8:
lef(1,le,rud1,offset1);
rig(-1,ri,rud1,offset2);
break;
case 2:
lef(-1,le,rud1,offset1);
rig(1,ri,rud1,offset2);
break;
case 4:
lef(-1,le,rud1,offset1);
rig(-1,ri,rud1,offset2);
break;
case 6:
lef(1,le,rud1,offset1);
rig(1,ri,rud1,offset2);
break;
case 5:
lef(0,le,rud1,offset1);
rig(0,ri,rud1,offset2);
break;
case 7:
lef(0,le,rud1,offset1);
rig(-1,ri,rud1,offset2);
break;
case 9:
lef(1,le,rud1,offset1);
rig(0,ri,rud1,offset2);
break;
case 1:
lef(0,le,rud1,offset1);
rig(1,ri,rud1,offset2);
break;
case 3:
lef(-1,le,rud1,offset1);
rig(0,ri,rud1,offset2);
break;
}
}
void turn(int mod,int dur) //不跑直线
{
if(mod==8)
{
for(;dur>0;dur--)
{
run(8,11,4,1700,0,34);
}
}
else
{
for(;dur>0;dur--)
{
run(mod,11,4,1700,0,0);
}
}
}
// 蜂鸣器
int fre1[7]={262,294,330,349,392,440,494};
int fre2[7]={523,587,659,698,784,880,988};
int fre3[7]={1046,1175,1318,1397,1568,1760,1967};
int magic=400;int magic2=800;
void toe(int pin,int mod,int ft,int dura)
{
ft--;
if(mod==1)
{
tone(pin,fre1[ft]);
}
if(mod==2)
{
tone(pin,fre2[ft]);
}
if(mod==3)
{
tone(pin,fre3[ft]);
}
delay(dura);
noTone(3);
}
//夹子
void cat(int pin,int nu) //900闭合 张开1300
{
int ac;
for(ac=0;ac<15;ac++)
{
digitalWrite(pin,HIGH);
delayMicroseconds(nu);
digitalWrite(pin,LOW);
delay(20);
}
}
void runcat(int mod)
{
switch(mod)
{
case 0:
cat(2,850);
break;
case 1:
cat(2,1400);
break;
}
}
int counter =0,sum,sign=0;
int i=0; //循环参数
int right=0;int left=0; //9
int rerig,relef;
void setup() {
pinMode(6,INPUT);
pinMode(9,INPUT);
pinMode(11,OUTPUT);
pinMode(4,OUTPUT);
pinMode(12,OUTPUT);
pinMode(2,OUTPUT);
Serial.begin(9600);
toe(13,2,2,200);
noTone(13);
}
int ddad=0;
void loop()
{
rerig=right;
relef=left;
right=digitalRead(9);
left=digitalRead(6);/*
Serial.println("right");
Serial.println(right);
Serial.println("left");
Serial.println(left);*/
sum=left+right;
switch(sum) //2.0版本运动
{
case 0:
turn(8,1);
break;
case 1:
if(right == 1)
run(9,11,4,1700,0,0);
else if(left == 1)
run(7,11,4,1700,0,0);
break;
case 2:
counter++;
toe(13,2,2,200);
noTone(13);
switch(counter)
{
case 1:
runcat(1);
turn(8,5);
turn(9,11);
turn(8,10);
break;
case 2:
turn(8,6);
do
{
right=digitalRead(9);
turn(4,1);
}while(right==0);
do
{
right=digitalRead(9);
turn(4,1);
}while(right==1);
turn(4,4);
turn(8,5);
break;
case 3:
runcat(0);
turn(8,5);
break;
case 4:
turn(8,11);
left=digitalRead(6);
while(left==0)
{
turn(6,1);
left=digitalRead(6);
}
do{
turn(6,1);
left=digitalRead(6);
}while(left==1);
turn(6,2);
turn(8,6);
break;
case 5:
turn(8,5);
break;
case 6:
turn(2,3);
delay(500);
runcat(1);
turn(2,4);
turn(4,26);
break;
case 7:
turn(8,5);
break;
case 8:
turn(8,8);
turn(4,18);
turn(8,4);
break;
case 9:
runcat(0);
delay(500);
turn(8,5);
turn(4,26);
break;
case 10:
turn(8,9);
left=digitalRead(6);
while(left==0)
{
turn(6,1);
left=digitalRead(6);
}
while(left==1)
{
turn(6,1);
left=digitalRead(6);
}
while(left==0)
{
turn(6,1);
left=digitalRead(6);
}
turn(4,4);
turn(8,5);
break;
case 11:
turn(8,5);
break;
case 12:
turn(2,4);
runcat(1);
delay(1000);
turn(2,5);
turn(4,25);
break;
case 13:
turn(8,4);
break;
case 14:
turn(8,11);
right=digitalRead(9);
while(right==0)
{
turn(4,1);
right=digitalRead(9);
}
do{
turn(4,1);
right=digitalRead(9);
}while(right==1);
turn(4,2);
turn(8,3);
break;
case 15:
runcat(0);
delay(500);
turn(8,6);
break;
case 16:
turn(8,11);
right=digitalRead(9);
while(right==0)
{
turn(4,1);
right=digitalRead(9);
}
break;
case 17:
turn(2,5);
delay(1000);
runcat(1);
turn(2,4);
right=digitalRead(9);
while(right==0)
{
turn(6,1);
right=digitalRead(9);
}
while(right==1)
{
turn(6,1);
right=digitalRead(9);
}
break;
case 18:
turn(9,10);
turn(8,5);
break;
case 19:
turn(7,11);
turn(8,5);
turn(8,10);
turn(4,25);
toe(13,2,3,200);
toe(13,2,2,200);
toe(13,2,3,200);
delay(100000);
}
}
/*1.0运动
if(right== 0 && left ==0)
{
run(8,11,4,1700,0,10);
}
else if(right ==1 && left == 0)
{
run(9,11,4,1600,0,0);
}
else if(right==0 && left == 1)
{
run(7,11,4,1600,0,0);
}
else if(right ==1 && left == 1)
{
if(relef== 0 || rerig ==0)
{
counter++;
}
if(counter==2)
{
while(i<25)
{
run(4,11,4,1600,0,0);
i++;
}
}
else if(counter==3)
{
while(i>0)
{
delay(1);
}
}
}
/* 舵机检查
if(right==1)
{
run(8,11,4,1700,0,10);
}
else
{
run(5,11,4,1700,0,0);
}
/*
for(i=100;i>0;i--)
{
run(8,11,6,1700,0,10);
}
for(i=100;i>0;i--)
{
run(2,11,6,1700,0,70);
}
for(i=100;i>0;i--)
{
run(4,11,6,1700,0,0);
}
for(i=100;i>0;i--)
{
run(6,11,6,1700,0,0);
}
for(i=100;i>0;i--)
{
run(7,11,6,1700,0,0);
}
for(i=100;i>0;i--)
{
run(9,11,6,1700,0,0);
}
for(i=100;i>0;i--)
{
run(1,11,6,1700,0,0);
}
for(i=100;i>0;i--)
{
run(3,11,6,1700,0,0);
}
*//*两只老虎
toe(11,3,1,magic);
toe(11,3,2,magic);
toe(11,3,3,magic);
toe(11,3,1,magic);
toe(11,3,1,magic);
toe(11,3,2,magic);
toe(11,3,3,magic);
toe(11,3,1,magic);
toe(11,3,3,magic);
toe(11,3,4,magic);
toe(11,3,5,magic2);
toe(11,3,3,magic);
toe(11,3,4,magic);
toe(11,3,5,magic2);
// delay(magic);
toe(11,3,5,magic);
toe(11,3,6,magic);
toe(11,3,5,magic);
toe(11,3,4,magic);
toe(11,3,3,magic2);
toe(11,3,1,magic2);
delay(magic);
toe(11,3,5,magic);
toe(11,3,6,magic);
toe(11,3,5,magic);
toe(11,3,4,magic);
toe(11,3,3,magic2);
toe(11,3,1,magic2);
delay(magic);
toe(11,3,2,magic2);
toe(11,2,5,magic2);
toe(11,3,1,magic2);
delay(magic);
toe(11,3,2,magic2);
toe(11,2,5,magic2);
toe(11,3,1,magic2);*/
}
邓翔&徐谨行小组