为什么小车在加入判断语句循迹的时候,小车速度会变慢,求解
- int GraysensorRPin = 18;
- int GraysensorLPin = 14;
- int motor_01 = 10;
- int motor_02 = 6;
- int motor_03 = 5;
- int motor_04 = 9;
- int start_stop_flag = 0;
- int stop_num = 0;
- int stop_clear_num = 0;
- void right();
- void left();
- void forwards();
- void stop();
- void setup() {
- pinMode(GraysensorRPin,INPUT);
- pinMode(GraysensorLPin,INPUT);
- pinMode(motor_01,OUTPUT);
- pinMode(motor_02,OUTPUT);
- pinMode(motor_03,OUTPUT);
- pinMode(motor_04,OUTPUT);
- }
- void loop() {
- if(!(digitalRead(GraysensorRPin)) &&digitalRead(GraysensorLPin))
- {
- right();
- }
- if(digitalRead(GraysensorRPin) && !(digitalRead(GraysensorLPin)))
- {
- left();
- }
- if( (!digitalRead(GraysensorRPin))&& (!digitalRead(GraysensorLPin)))
- {
- forwards();
- }
- if(digitalRead(GraysensorRPin) &&digitalRead(GraysensorLPin))
- {
- stop();
- }
- if(stop_clear_num == 2)
- {
- start_stop_flag = 1;
- }
-
- }
- void stop()
- {
- if(start_stop_flag)
- {
- analogWrite(motor_03, 0);
- analogWrite(motor_02, 0);
- analogWrite(motor_04, 0);
- analogWrite(motor_01, 0);
- start_stop_flag = 0;
- forwards();
- }else
- {
- stop_num++;
- }
- }
- void right()
- {
- analogWrite(motor_03, 175);
- analogWrite(motor_02, 0);
- analogWrite(motor_04, 0);
- analogWrite(motor_01, 175);
- }
- void forwards()
- {
- analogWrite(motor_03, 175);
- analogWrite(motor_02, 0);
- analogWrite(motor_04, 175);
- analogWrite(motor_01, 0);
- if(stop_num > 10)
- {
- stop_num = 0;
- stop_clear_num++;
- }
- }
- void left()
- {
- analogWrite(motor_03, 0);
- analogWrite(motor_02, 175);
- analogWrite(motor_04, 175);
- analogWrite(motor_01, 0);
- }
复制代码
|