1、/初始化电机驱动 IO为输出方式void setup()pinMode(Left_motor_go,OUTPUT); / PIN 8 (PWM)pinMode(Left_motor_back,OUTPUT); / PIN 9 (PWM)pinMode(Right_motor_go,OUTPUT);/ PIN 10 (PWM) pinMode(Right_motor_back,OUTPUT);/ PIN 11 (PWM)pinMode(13, OUTPUT);/端口模式,输出Serial.begin(9600); /波特率 9600irrecv.enableIRIn(); / Start the
2、 receiver/电机控制:前进+刹车+左转+右转+后退/void run() / 前进digitalWrite(Right_motor_go,HIGH); / 右电机前进digitalWrite(Right_motor_back,LOW); /analogWrite(Right_motor_go,200);/PWM比例 0255调速,左右轮差异略增减/analogWrite(Right_motor_back,0);digitalWrite(Left_motor_go,HIGH); / 左电机前进digitalWrite(Left_motor_back,LOW);/analogWrite(L
3、eft_motor_go,200);/PWM比例 0255调速,左右轮差异略增减/analogWrite(Left_motor_back,0);/delay(time * 100); /执行时间,可以调整 void brake() /刹车,停车digitalWrite(Right_motor_go,LOW);digitalWrite(Right_motor_back,LOW);digitalWrite(Left_motor_go,LOW);digitalWrite(Left_motor_back,LOW);/delay(time * 100);/执行时间,可以调整 void left() /左
4、转(左轮不动,右轮前进)digitalWrite(Right_motor_go,HIGH); / 右电机前进digitalWrite(Right_motor_back,LOW);/analogWrite(Right_motor_go,200); /analogWrite(Right_motor_back,0);/PWM比例 0255调速digitalWrite(Left_motor_go,LOW); /左轮不动digitalWrite(Left_motor_back,LOW);/analogWrite(Left_motor_go,0); /analogWrite(Left_motor_back
5、,0);/PWM比例 0255调速/delay(time * 100); /执行时间,可以调整 void spin_left() /左转(左轮后退,右轮前进)digitalWrite(Right_motor_go,HIGH); / 右电机前进digitalWrite(Right_motor_back,LOW);/analogWrite(Right_motor_go,200); /analogWrite(Right_motor_back,0);/PWM比例 0255调速digitalWrite(Left_motor_go,LOW); /左轮后退digitalWrite(Left_motor_ba
6、ck,HIGH);/analogWrite(Left_motor_go,0); /analogWrite(Left_motor_back,200);/PWM比例 0255调速/delay(time * 100); /执行时间,可以调整 void right() /右转(右轮不动,左轮前进)digitalWrite(Right_motor_go,LOW); /右电机不动digitalWrite(Right_motor_back,LOW);/analogWrite(Right_motor_go,0); /analogWrite(Right_motor_back,0);/PWM比例 0255调速di
7、gitalWrite(Left_motor_go,HIGH);/左电机前进digitalWrite(Left_motor_back,LOW);/analogWrite(Left_motor_go,200); /analogWrite(Left_motor_back,0);/PWM比例 0255调速/delay(time * 100); /执行时间,可以调整 void spin_right() /右转(右轮后退,左轮前进)digitalWrite(Right_motor_go,LOW); /右电机后退digitalWrite(Right_motor_back,HIGH);/analogWrite
8、(Right_motor_go,0); /analogWrite(Right_motor_back,200);/PWM比例 0255调速digitalWrite(Left_motor_go,HIGH);/左电机前进digitalWrite(Left_motor_back,LOW);/analogWrite(Left_motor_go,200); /analogWrite(Left_motor_back,0);/PWM比例 0255调速/delay(time * 100); /执行时间,可以调整 void back() /后退digitalWrite(Right_motor_go,LOW); /
9、右轮后退digitalWrite(Right_motor_back,HIGH);/analogWrite(Right_motor_go,0);/analogWrite(Right_motor_back,150);/PWM比例 0255调速digitalWrite(Left_motor_go,LOW); /左轮后退digitalWrite(Left_motor_back,HIGH);/analogWrite(Left_motor_go,0);/analogWrite(Left_motor_back,150);/PWM比例 0255调速/delay(time * 100); /执行时间,可以调整
10、/红外控制模块/void loop()if (irrecv.decode(/标志位置反digitalWrite(13, on ? HIGH : LOW);/板子上接收到信号闪烁一下 leddump(/解码红外信号if (results.value = run_car )/按键 2run();/前进if (results.value = back_car )/按键 8back();/后退if (results.value = left_car )/按键 4left();/左转if (results.value = right_car )/按键 6right();/右转if (results.va
11、lue = stop_car )/按键 5brake();/停车if (results.value = left_turn )/按键 1spin_left();/左旋转if (results.value = right_turn )/按键 3spin_right();/右旋转last = millis(); irrecv.resume(); / Receive the next value/超声波模块/float Distance_test() / 量出前方距离 digitalWrite(Trig, LOW); / 给触发脚低电平 2sdelayMicroseconds(2);digitalW
12、rite(Trig, HIGH); / 给触发脚高电平 10s,这里至少是 10sdelayMicroseconds(10);digitalWrite(Trig, LOW); / 持续给触发脚低电float Fdistance = pulseIn(Echo, HIGH); / 读取高电平时间(单位:微秒)Fdistance= Fdistance/58; /为什么除以 58等于厘米, Y 米=(X 秒*344)/2/ X秒=( 2*Y 米)/344 =X 秒=0.0058*Y 米 =厘米=微秒/58/Serial.print(“Distance:“); /输出距离(单位:厘米)/Serial.p
13、rintln(Fdistance); /显示距离/Distance = Fdistance;return Fdistance; /超声波显示/void Distance_display(int Distance)/显示距离if(2Distance) /把光标移回左上角,即从头开始输出 lcd.print(“ Distance: “); /显示lcd.setCursor(6,2); /把光标定位在第 2行,第 6列lcd.print(Distance); /显示距离lcd.print(“cm“); /显示elselcd.home(); /把光标移回左上角,即从头开始输出 lcd.print(“! Out of range“); /显示delay(250);lcd.clear();