程序设计
1 //控制板引脚定义
2 int motor_L_ENA = 6; //左电机控制端连接于Arduino板的6,10,9引脚
3 int motor_L_IN1 = 10;
4 int motor_L_IN2 = 9;
5 int motor_R_ENB = 5; //右电机控制端连接于Arduino板的5,8,7引脚
6 int motor_R_IN3 = 8;
7 int motor_R_IN4 = 7;
8
9 int infrared_LL = A0; //左左侧红外传感器连接于Arduino板的A0引脚
10 int infrared_L = A1; //左侧红外传感器连接于Arduino板的A1引脚
11 int infrared_R = A2; //右侧红外传感器连接于Arduino板的A2引脚
12 int infrared_RR = A3; //右右侧红外传感器连接于Arduino板的A3引脚
13
14 int sensor_LL = 0; //左左侧红外传感器的检测值,默认值为1,在黑线外
15 int sensor_L = 1; //左侧红外传感器的检测值,默认值为1,在黑线上
16 int sensor_R = 1; //右侧红外传感器的检测值,默认值为1,在黑线上
17 int sensor_RR = 0; //右右侧红外传感器的检测值,默认值为0,在黑线外
18 int low_speed = 100; //左转右转时的高速低速设定,可调整
19 int high_speed = 200;
20
21 //子函数定义
22 void go_forward_high_speed() //小车前进
23 {
24 analogWrite(motor_L_ENA, high_speed); //左电机正转
25 digitalWrite(motor_L_IN1, 0);
26 digitalWrite(motor_L_IN2, 1);
27
28 analogWrite(motor_R_ENB,high_speed); //右电机正转
29 digitalWrite(motor_R_IN3, 0);
30 digitalWrite(motor_R_IN4, 1);
31 }
32
33 void stop_with_brake() //小车制动
34 {
35 digitalWrite(motor_L_ENA, 1); //左电机制动
36 digitalWrite(motor_L_IN1, 1);
37 digitalWrite(motor_L_IN2, 1) ;
38
39 digitalWrite(motor_R_ENB, 1); //右电机制动
40 digitalWrite(motor_R_IN3, 1);
41 digitalWrite(motor_R_IN4, 1) ;
42 }
43
44 void go_forward_left() //小车左转
45 {
46 digitalWrite(motor_L_IN1, 0);
47 digitalWrite(motor_L_IN2, 1);
48 analogWrite(motor_L_ENA, low_speed); //左轮低速
49
50 digitalWrite(motor_R_IN3, 0);
51 digitalWrite(motor_R_IN4, 1);
52 analogWrite(motor_R_ENB, high_speed); //右轮高速
53 }
54
55 void go_forward_right() //小车右转
56 {
57 digitalWrite(motor_L_IN1, 0);
58 digitalWrite(motor_L_IN2, 1);
59 analogWrite(motor_L_ENA, high_speed); //左轮高速
60
61 digitalWrite(motor_R_IN3, 0);
62 digitalWrite(motor_R_IN4, 1);
63 analogWrite(motor_R_ENB, low_speed); //右轮低速
64 }
65
66
67 void setup()
68 {
69 pinMode(motor_L_ENA, OUTPUT); //左电机使能和PWM调速口
70 pinMode(motor_L_IN1, OUTPUT); //左电机控制口1
71 pinMode(motor_L_IN2, OUTPUT); //左电机控制口2
72
73 pinMode(motor_R_ENB, OUTPUT); //右电机使能和PWM调速口
74 pinMode(motor_R_IN3, OUTPUT); //右电机控制口3
75 pinMode(motor_R_IN4, OUTPUT); //左电机控制口4
76
77 pinMode(infrared_L, INPUT); //左侧红外传感器
78 pinMode(infrared_LL, INPUT); //左左侧红外传感器
79
80 pinMode(infrared_R, INPUT); //右侧红外传感器
81 pinMode(infrared_RR, INPUT); //右右侧红外传感器
82 }
83
84 void loop()
85 {
86 sensor_LL=digitalRead(infrared_LL);
87 sensor_L=digitalRead(infrared_L);
88 sensor_R=digitalRead(infrared_R);
89 sensor_RR=digitalRead(infrared_RR);
90
91 if(sensor_LL==0 & sensor_L==1 & sensor_R==1 & sensor_RR==0) //小车在黑线上
92 {
93 go_forward_high_speed(); //前进
94 }
95
96 if(sensor_L==1 & sensor_R==0) //小车右偏
97 {
98 go_forward_left(); //小车左转100毫秒,时间可调整
99 delay(100);
100 }
101
102 if(sensor_L==0 & sensor_R==1) //小车左偏
103 {
104 go_forward_right(); //小车右转100毫秒,时间可调整
105 delay(100);
106 }
107
108 if(sensor_LL==1 & sensor_L==1 & sensor_R==1 & sensor_RR==1) //小车在停止线上
109 {
110 stop_with_brake(); //制动2秒
111 delay(2000);
112 }
113 }
程序设计
1 //控制板引脚定义
2 int motor_L_ENA = 6; //左电机控制端连接于Arduino板的6,10,9引脚
3 int motor_L_IN1 = 10;
4 int motor_L_IN2 = 9;
5 int motor_R_ENB = 5; //右电机控制端连接于Arduino板的5,8,7引脚
6 int motor_R_IN3 = 8;
7 int motor_R_IN4 = 7;
8
9 int infrared_LL = A0; //左左侧红外传感器连接于Arduino板的A0引脚
10 int infrared_L = A1; //左侧红外传感器连接于Arduino板的A1引脚
11 int infrared_R = A2; //右侧红外传感器连接于Arduino板的A2引脚
12 int infrared_RR = A3; //右右侧红外传感器连接于Arduino板的A3引脚
13
14 int sensor_LL = 0; //左左侧红外传感器的检测值,默认值为1,在黑线外
15 int sensor_L = 1; //左侧红外传感器的检测值,默认值为1,在黑线上
16 int sensor_R = 1; //右侧红外传感器的检测值,默认值为1,在黑线上
17 int sensor_RR = 0; //右右侧红外传感器的检测值,默认值为0,在黑线外
18 int low_speed = 100; //左转右转时的高速低速设定,可调整
19 int high_speed = 200;
20
21 //子函数定义
22 void go_forward_high_speed() //小车前进
23 {
24 analogWrite(motor_L_ENA, high_speed); //左电机正转
25 digitalWrite(motor_L_IN1, 0);
26 digitalWrite(motor_L_IN2, 1);
27
28 analogWrite(motor_R_ENB,high_speed); //右电机正转
29 digitalWrite(motor_R_IN3, 0);
30 digitalWrite(motor_R_IN4, 1);
31 }
32
33 void stop_with_brake() //小车制动
34 {
35 digitalWrite(motor_L_ENA, 1); //左电机制动
36 digitalWrite(motor_L_IN1, 1);
37 digitalWrite(motor_L_IN2, 1) ;
38
39 digitalWrite(motor_R_ENB, 1); //右电机制动
40 digitalWrite(motor_R_IN3, 1);
41 digitalWrite(motor_R_IN4, 1) ;
42 }
43
44 void go_forward_left() //小车左转
45 {
46 digitalWrite(motor_L_IN1, 0);
47 digitalWrite(motor_L_IN2, 1);
48 analogWrite(motor_L_ENA, low_speed); //左轮低速
49
50 digitalWrite(motor_R_IN3, 0);
51 digitalWrite(motor_R_IN4, 1);
52 analogWrite(motor_R_ENB, high_speed); //右轮高速
53 }
54
55 void go_forward_right() //小车右转
56 {
57 digitalWrite(motor_L_IN1, 0);
58 digitalWrite(motor_L_IN2, 1);
59 analogWrite(motor_L_ENA, high_speed); //左轮高速
60
61 digitalWrite(motor_R_IN3, 0);
62 digitalWrite(motor_R_IN4, 1);
63 analogWrite(motor_R_ENB, low_speed); //右轮低速
64 }
65
66
67 void setup()
68 {
69 pinMode(motor_L_ENA, OUTPUT); //左电机使能和PWM调速口
70 pinMode(motor_L_IN1, OUTPUT); //左电机控制口1
71 pinMode(motor_L_IN2, OUTPUT); //左电机控制口2
72
73 pinMode(motor_R_ENB, OUTPUT); //右电机使能和PWM调速口
74 pinMode(motor_R_IN3, OUTPUT); //右电机控制口3
75 pinMode(motor_R_IN4, OUTPUT); //左电机控制口4
76
77 pinMode(infrared_L, INPUT); //左侧红外传感器
78 pinMode(infrared_LL, INPUT); //左左侧红外传感器
79
80 pinMode(infrared_R, INPUT); //右侧红外传感器
81 pinMode(infrared_RR, INPUT); //右右侧红外传感器
82 }
83
84 void loop()
85 {
86 sensor_LL=digitalRead(infrared_LL);
87 sensor_L=digitalRead(infrared_L);
88 sensor_R=digitalRead(infrared_R);
89 sensor_RR=digitalRead(infrared_RR);
90
91 if(sensor_LL==0 & sensor_L==1 & sensor_R==1 & sensor_RR==0) //小车在黑线上
92 {
93 go_forward_high_speed(); //前进
94 }
95
96 if(sensor_L==1 & sensor_R==0) //小车右偏
97 {
98 go_forward_left(); //小车左转100毫秒,时间可调整
99 delay(100);
100 }
101
102 if(sensor_L==0 & sensor_R==1) //小车左偏
103 {
104 go_forward_right(); //小车右转100毫秒,时间可调整
105 delay(100);
106 }
107
108 if(sensor_LL==1 & sensor_L==1 & sensor_R==1 & sensor_RR==1) //小车在停止线上
109 {
110 stop_with_brake(); //制动2秒
111 delay(2000);
112 }
113 }
举报