#define speeds 50 //宏定义speedsbyte GLevel[22]={0}; //灰度储存short Condition = 0; //定义前进时为Condition = 0;//---------------------------------------------------目前没有改变Condition的语句,调试时只能改变这里的值----------------------////-1~4依次为停止-1,前进0,后退1,左2,右3,原地旋转4const short GrayPin[22] = {40, 41, 42, 43, 44, 45,46, 47, 48, 49, 50, 51, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39};//40-45为前接口,46-51为后接口,30-34为左接口,35-39为右接口。;//---------------------------------灰度部分------------------------------//void Gray_Init() //灰度初始化{ for (short i = 0; i <22; i++){ pinMode(GrayPin[i], INPUT);}}void GrayGet_value(byte value[]){ int j = 0;for (j = 0; j < 22; j++)value[j] = digitalRead(GrayPin[j]);}//--------------------------------电机部分------------------------------////23左前,45右前,67左后,89右后void _M_Init() { //电机初始化for (int i = 2; i <= 9; i++){pinMode(i, OUTPUT);analogWrite(i, 0);}}void _M_Front(int pin1, int pin2, int _speed) {analogWrite(pin1, _speed);analogWrite(pin2, 0);}void _M_Back(int pin1, int pin2, int _speed) {analogWrite(pin2, _speed);analogWrite(pin1, 0);}/*void _M_Stop() {for (int i = 2; i <= 9; i++)analogWrite(i, 255);Condition = -1;}*/void _M_Stop(int pin1,int pin2) {analogWrite(pin1,255);analogWrite(pin2,255);}//-----------------------------前进---------电机全部正转-----------------------//void _M_GoFront(int _speed) { //直行时四个轮子全部正转_M_Front(7, 6, _speed + 0);//左前_M_Front(4, 5, _speed +0 );//右前_M_Front(8, 9, _speed + 0);//左后_M_Front(2, 3, _speed + 0);//右后}//-----------------------------后退---------电机全部反转------------------------//void _M_GoBack(int _speed) {_M_Back(7, 6, _speed + 0);_M_Back(4, 5, _speed + 0);_M_Back(8, 9, _speed + 0);_M_Back(2, 3, _speed + 0);}//--------------------------------------左横行-------------------------------//void _M_GoLeft(int _speed){_M_Back(7, 6, _speed +0 );_M_Front(4, 5,_speed +0);_M_Front(8, 9,_speed +0);_M_Back(2, 3,_speed +0);}//--------------------------------------右横行-------------------------------//void _M_GoRight(int _speed){_M_Front(7, 6, _speed +0);_M_Back(4, 5, _speed +0);_M_Back(8, 9, _speed +0);_M_Front(2, 3, _speed +0);}//--------------------------------------右前走------------------------------//void _MGo_youqian(int _speed){_M_Front(7, 6, _speed +0);_M_Stop(4,5);_M_Front(8, 9, _speed +0);_M_Stop(2,3);}//---------------------------------------左前走-----------------------------//void _MGo_zuoqian(int _speed){_M_Stop(7,6);_M_Front(4, 5, _speed +0);_M_Stop(8,9);_M_Front(2, 3, _speed +0);}//---------------------------------------左后走----------------------------//void _MGo_zuohou(int _speed){_M_Back(7, 6, _speed +0);_M_Stop(4,5);_M_Back(8, 9, 255);_M_Stop(2,3);}//--------------------------------------右后走----------------------------//void _MGo_youhou(int _speed){_M_Stop(7,6);_M_Back(4, 5,_speed +0 );_M_Back(8, 9,_speed +0);_M_Stop(2,3);}void setup() {Gray_Init();}void loop(){GrayGet_value(GLevel);//0 1 2 3 4 5switch(Condition){case 0://前进{unsigned a;a=(GLevel[0]<< 5)|(GLevel[1] << 4) | (GLevel[2] << 3) | (GLevel[3] << 2) | (GLevel[4]<<1) | (GLevel[5]);switch( a ){case 0x0C://直行不偏转 //001100_M_GoFront(speeds);break;//--------------------------------前---小偏--------------//case 0x08://右小偏 //001000_MGo_zuoqian(speeds) ;break;case 0x04://左小偏 //000100_MGo_youqian(speeds);break;//------------------------------前-----中偏---------------//case 0x18://右中偏 //011000_MGo_zuoqian(speeds) ;break;case 0x06://左中偏 //000110_MGo_youqian(speeds);break;//----------------------------前-------大偏---------------//case 0x30://右大偏 //110000_MGo_zuoqian(speeds) ;break;case 0x03://左大偏 //000011_MGo_youqian(speeds);break;default:_M_GoFront(speeds);break;}}case 2://车左横行时{unsigned b=0;b=(GLevel[16]<< 4)|(GLevel[15] << 3) | (GLevel[14] << 2) | (GLevel[13] << 1) | (GLevel[12]);switch(b){case 0x04://直行//00100_M_GoLeft(speeds);break;//-------------------------------左---小偏--------------//case 0x06://小左后偏//调用左前走函数//00110_MGo_zuoqian(speeds+0);break;case 0x0C://小左前偏//调用左后走函数01100_MGo_zuohou(speeds+0);break;//------------------------------左---中偏----------------//case 0x02://中左后偏//调用左前走函数00010_MGo_zuoqian(speeds-1);break;case 0x08://中左前偏//调用左后走函数01000_MGo_zuohou(speeds-1);break;//------------------------------左---大偏----------------//case 0x03://大左后偏//调用左前走函数00011_MGo_zuoqian(speeds-2);break;case 0x18://大左前偏//调用左后走函数11000_MGo_zuohou(speeds-2);break;default:_M_GoLeft(speeds);break;}}case 3://车右横行时{unsigned c=0;c=(GLevel[17]<< 4)|(GLevel[18] << 3) | (GLevel[19] << 2) | (GLevel[20] << 1) | (GLevel[21]) ;switch(c){case 0x04://直行//00100_M_GoRight(speeds);break;//-------------------------------左---小偏--------------//case 0x06://小右前偏//00110_MGo_youhou(speeds+0);break;case 0x0C://小右后偏01100_MGo_youqian(speeds+0);break;//------------------------------左---中偏----------------//case 0x02://中右前偏00010_MGo_youhou(speeds-1);break;case 0x08://中右后偏01000_MGo_youqian(speeds-1);break;//------------------------------左---大偏----------------//case 0x03://大右前偏00011_MGo_youhou(speeds-2);break;case 0x18://大右后偏11000_MGo_youqian(speeds-2);break;default:_M_GoRight(speeds);break;}}}delay(2000);}
在最后delay(2000)的原因:
Mega2560性能不够,时间太短无法调整轮子的转动方向和速度;
void loop(){
switch(){
case 0:
···
case 1:
···
···
}
delay(2000);
}
//原理:等待2000ms再进行下一次判断~
