本帖最后由 wb9999cn 于 2014-12-2 20:25 编辑
/***************坦克运行部分控制模型*********************
******************车体运行命令列表***********************
命令 前进 后退 左弯 右弯 行进档 转向档 左电机 右电机
1 接通 一档 30% 30%
2 接通 二档 70% 70%
3 接通 三档 100% 100%
4 接通 一档 -30% -30%
5 接通 二档 -70% -70%
6 接通 三档 -100% -100%
7 接通 一档 0% 50%
8 接通 二档 0% 100%
9 接通 三档 -50% 50%
10 接通 一档 50% 0%
11 接通 二档 100% 0%
12 接通 三档 50% -50%
13 接通 接通 一档 一档 20% 50%
14 接通 接通 二档 一档 35% 65%
15 接通 接通 三档 一档 50% 80%
16 接通 接通 一档 二档 20% 60%
17 接通 接通 二档 二档 35% 75%
18 接通 接通 三档 二档 50% 90%
19 接通 接通 一档 三档 20% 70%
20 接通 接通 二档 三档 35% 85%
21 接通 接通 三档 三档 50% 100%
22 接通 接通 一档 一档 50% 20%
23 接通 接通 二档 一档 65% 35%
24 接通 接通 三档 一档 80% 50%
25 接通 接通 一档 二档 60% 20%
26 接通 接通 二档 二档 75% 35%
27 接通 接通 三档 二档 90% 50%
28 接通 接通 一档 三档 70% 20%
29 接通 接通 二档 三档 85% 35%
30 接通 接通 三档 三档 100% 50%
31 接通 接通 一档 一档 -20% -50%
32 接通 接通 二档 一档 -35% -65%
33 接通 接通 三档 一档 -50% -80%
34 接通 接通 一档 二档 -20% -60%
35 接通 接通 二档 二档 -35% -75%
36 接通 接通 三档 二档 -50% -90%
37 接通 接通 一档 三档 -20% -70%
38 接通 接通 二档 三档 -35% -85%
39 接通 接通 三档 三档 -50% -100%
40 接通 接通 一档 一档 -50% -20%
41 接通 接通 二档 一档 -65% -35%
42 接通 接通 三档 一档 -80% -50%
43 接通 接通 一档 二档 -60% -20%
44 接通 接通 二档 二档 -75% -35%
45 接通 接通 三档 二档 -90% -50%
46 接通 接通 一档 三档 -70% -20%
47 接通 接通 二档 三档 -85% -35%
48 接通 接通 三档 三档 -100% -50%
49 0% 0% 停车
******************炮塔运行命令列表***********************
命令 左转 右转 抬高 降低 转速档 方向机 高低机
51 接通 一档 40%
52 接通 二档 70%
53 接通 三档 100%
54 接通 一档 -40%
55 接通 二档 -70%
56 接通 三档 -100%
57 接通 一档 40%
58 接通 二档 70%
59 接通 三档 100%
60 接通 一档 -40%
61 接通 二档 -70%
62 接通 三档 -100%
63 0 0
*****************火炮运行命令列表***********************
命令 开火档 火炮机
91 一档 0%
92 二档 50%
93 三档 100%
******************变量声明****************************/
String comdata = ""; //定义字符串
int mark = 0; //定义接收数据的标识
int numdata = 0; //定义串口收到的数值
int E1 = 3; //左驱动电机模拟口
int M1 = 4; //左驱动电机数字口
int E2 = 5; //右驱动电机模拟口
int M2 = 7; //右驱动电机数字口
int E3 = 6; //炮塔方向电机模拟口
int M3 = 8; //炮塔方向电机数字口
int E4 = 9; //炮管高低电机模拟口
int M4 = 12; //炮管高低电机数字口
int E5 = 10; //火炮驱动电机模拟口
int M5 = 13; //火炮驱动电机数字口
int command; //定义命令
int drive; //定义驾驶状态要求
int oldDrive; //定义行驶状态
int turret; //定义炮塔状态要求
int oldTurret; //定义炮塔状态
int cannon; //定义射击状态要求
int oldCannon; //定义射击状态
/****************程序初始化***********************/
void setup()
{
pinMode(E1,OUTPUT); //设置电机驱动接口状态
pinMode(M1,OUTPUT); //设置电机驱动接口状态
pinMode(E2,OUTPUT); //设置电机驱动接口状态
pinMode(M2,OUTPUT); //设置电机驱动接口状态
pinMode(E3,OUTPUT); //设置电机驱动接口状态
pinMode(M3,OUTPUT); //设置电机驱动接口状态
pinMode(E4,OUTPUT); //设置电机驱动接口状态
pinMode(M4,OUTPUT); //设置电机驱动接口状态
pinMode(E5,OUTPUT); //设置电机驱动接口状态
pinMode(M5,OUTPUT); //设置电机驱动接口状态
Serial.begin(9600); //打开串口
}
/****************主程序***********************/
void loop()
{
/********不断循环检测串口缓存,一个个读入字符串*********/
while (Serial.available() > 0) //如果有串口数据
{
comdata += char(Serial.read()); //读入之后将字符串,串接到comdata上面。
delay(2); //延时
mark = 1; //标记串口读过数据
}
/*****************将字符串转化为数值和命令**********************/
if(mark == 1) //如果接收到数据
{
for(int i = 0; i < comdata.length() ; i++) //以串口读取字符串长度循环,
{
numdata = numdata * 10 + (comdata[i] - '0'); //将字符串转化为数值
}
command = numdata; //读取命令
Serial.print("command="); //输出命令
Serial.println(command); //输出命令内容
comdata = String(""); //清空comdata
numdata = 0; //给numdata赋值0
mark = 0; //给mark赋值0
}
/***************将命令转化为动作*****************/
switch(command)
{
/***************车体动作*****************/
case 1: //命令1
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 63); //30%
analogWrite(E2, 63); //30%
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 2:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 3:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 254); //PWM调速
analogWrite(E2, 254); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 4:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 63); //PWM调速
analogWrite(E2, 63); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 5:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 6:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 254); //PWM调速
analogWrite(E2, 254); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 7:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 0); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 8:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 0); //PWM调速
analogWrite(E2, 254); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 9:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 10:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 0); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 11:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 254); //PWM调速
analogWrite(E2, 0); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 12:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 13:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 14:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 165); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 15:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 204); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 16:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 152); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 17:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 192); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 18:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 229); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 19:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 178); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 20:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 216); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 21:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 255); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 22:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 23:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 165); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 24:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 204); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 25:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 152); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 26:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 192); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 27:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 229); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 28:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 178); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 29:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 216); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 30:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 255); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 31:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 32:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 165); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 33:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 204); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 34:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 152); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 35:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 192); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 36:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 229); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 37:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 50); //PWM调速
analogWrite(E2, 178); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 38:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 89); //PWM调速
analogWrite(E2, 216); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 39:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 255); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 40:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 127); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 41:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 165); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 42:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 204); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 43:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 152); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 44:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 192); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 45:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 229); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 46:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 178); //PWM调速
analogWrite(E2, 50); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 47:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 216); //PWM调速
analogWrite(E2, 89); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
case 48:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,HIGH); //
digitalWrite(M2,HIGH); //
analogWrite(E1, 255); //PWM调速
analogWrite(E2, 127); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
/*******??????????????????????????????????***********/
case 49:
drive = command;
if(drive != oldDrive)
{
digitalWrite(M1,LOW); //
digitalWrite(M2,LOW); //
analogWrite(E1, 0); //PWM调速
analogWrite(E2, 0); //PWM调速
oldDrive = drive; //刷新行驶状态
Serial.print("drive=");
Serial.println(drive);
}
break;
/***************炮塔动作*******************/
case 51:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,LOW); //
analogWrite(E3, 63); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 52:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,LOW); //
analogWrite(E3, 127); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 53:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,LOW); //
analogWrite(E3, 254); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 54:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,HIGH); //
analogWrite(E3, 63); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 55:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,HIGH); //
analogWrite(E3, 127); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 56:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,HIGH); //
analogWrite(E3, 254); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 57:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,LOW); //
analogWrite(E4, 63); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 58:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,LOW); //
analogWrite(E4, 127); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 59:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,LOW); //
analogWrite(E4, 254); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 60:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,HIGH); //
analogWrite(E4, 63); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 61:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,HIGH); //
analogWrite(E4, 127); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 62:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M4,HIGH); //
analogWrite(E4, 254); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
case 63:
turret = command;
if(turret != oldTurret)
{
digitalWrite(M3,LOW); //
digitalWrite(M4,LOW); //
analogWrite(E3, 0); //PWM调速
analogWrite(E4, 0); //PWM调速
oldTurret = turret; //刷新行驶状态
Serial.print("turret=");
Serial.println(turret);
}
break;
/***************射击动作*******************/
case 91:
cannon = command;
if(cannon != oldCannon)
{
digitalWrite(M5,LOW); //
analogWrite(E5, 0); //PWM调速
oldCannon = cannon; //刷新行驶状态
Serial.print("cannon=");
Serial.println(cannon);
}
break;
case 92:
cannon = command;
if(cannon != oldCannon)
{
digitalWrite(M5,LOW); //
analogWrite(E5, 127); //PWM调速
oldCannon = cannon; //刷新行驶状态
Serial.print("cannon=");
Serial.println(cannon);
}
break;
case 93:
cannon = command;
if(cannon != oldCannon)
{
digitalWrite(M5,LOW); //
analogWrite(E5, 254); //PWM调速
oldCannon = cannon; //刷新行驶状态
Serial.print("cannon=");
Serial.println(cannon);
}
break;
}
}
复制代码
车体行动部分的代码基本上写完了,不过,由于第一次写Arduino的程序,因此感觉非常累赘,估计有很多技巧和函数可以大幅度精简代码。先不管了,还是发上来供大家批判,如果有大神能指点一下精简代码的思路,不胜感激。