寻迹小车-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 358|回复: 0

[未解决] 寻迹小车

[复制链接]
发表于 2022-6-5 10:11 | 显示全部楼层 |阅读模式
为什么加了和寻迹相关的代码之后,小车的速度会变慢呢?有什么解决方案吗?

  1. int GraysensorRPin = 18;
  2. int GraysensorLPin = 14;
  3. int motor_01 = 10;
  4. int motor_02 = 6;
  5. int motor_03 = 5;
  6. int motor_04 = 9;
  7. int start_stop_flag = 0;
  8. int stop_num = 0;
  9. int stop_clear_num = 0;
  10. void right();
  11. void left();
  12. void forwards();
  13. void stop();

  14. void setup() {
  15.   pinMode(GraysensorRPin,INPUT);
  16.   pinMode(GraysensorLPin,INPUT);
  17.   pinMode(motor_01,OUTPUT);
  18.   pinMode(motor_02,OUTPUT);
  19.   pinMode(motor_03,OUTPUT);
  20.   pinMode(motor_04,OUTPUT);
  21. }

  22. void loop() {
  23.   if(!(digitalRead(GraysensorRPin)) &&digitalRead(GraysensorLPin))
  24.   {
  25.     right();
  26.   }
  27.     if(digitalRead(GraysensorRPin) && !(digitalRead(GraysensorLPin)))
  28.   {
  29.     left();
  30.   }
  31.     if( (!digitalRead(GraysensorRPin))&& (!digitalRead(GraysensorLPin)))
  32.   {
  33.     forwards();
  34.   }
  35.     if(digitalRead(GraysensorRPin) &&digitalRead(GraysensorLPin))
  36.   {
  37.     stop();
  38.   }
  39.   if(stop_clear_num == 2)
  40.   {
  41.     start_stop_flag = 1;
  42.   }
  43.   
  44. }
  45. void stop()
  46. {
  47.    if(start_stop_flag)
  48.   {
  49.     analogWrite(motor_03, 0);
  50.     analogWrite(motor_02, 0);
  51.     analogWrite(motor_04, 0);
  52.     analogWrite(motor_01, 0);
  53.     start_stop_flag = 0;
  54.     forwards();
  55.   }else
  56.   {
  57.     stop_num++;
  58.   }
  59. }

  60. void right()
  61. {
  62.   analogWrite(motor_03, 175);
  63.   analogWrite(motor_02, 0);
  64.   analogWrite(motor_04, 0);
  65.   analogWrite(motor_01, 175);
  66. }

  67. void forwards()
  68. {
  69.   analogWrite(motor_03, 175);
  70.   analogWrite(motor_02, 0);
  71.   analogWrite(motor_04, 175);
  72.   analogWrite(motor_01, 0);
  73.    if(stop_num > 10)
  74.   {
  75.     stop_num = 0;
  76.     stop_clear_num++;
  77.   }
  78. }

  79. void left()
  80. {
  81.   analogWrite(motor_03, 0);
  82.   analogWrite(motor_02, 175);
  83.   analogWrite(motor_04, 175);
  84.   analogWrite(motor_01, 0);
  85. }
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2025-1-1 20:29 , Processed in 0.094462 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表