Guest User

Untitled

a guest
Jun 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #define ENA 5
  2. #define IN1 4
  3. #define IN2 3
  4. #define IN3 8
  5. #define IN4 9
  6. #define ENB 10
  7. #define potPin 0
  8. int Mspeed;
  9.  
  10. void setup() {
  11. Serial.begin(9600);
  12. // initialize digital pin LED_BUILTIN as an output.
  13. pinMode(IN1, OUTPUT);
  14. pinMode(IN2, OUTPUT);
  15. pinMode(IN3, OUTPUT);
  16. pinMode(IN4, OUTPUT);
  17. pinMode(ENA,OUTPUT);
  18. pinMode(ENB, OUTPUT);
  19. pinMode(potPin,INPUT);
  20. }
  21.  
  22. void loop()
  23. {
  24. Mspeed= analogRead(potPin)/4;
  25. Serial.println(Mspeed);
  26. delay(100);
  27. control_Robot(IN1,IN2,IN3,IN4,100);
  28.  
  29. }
  30. void RobotMove(byte inR1, byte inR2, byte inL1, byte inL2,byte action)
  31. {
  32. /*
  33. * inR1 và inR2 là 2 chân tín hiệu điều khiển động cơ bên phải
  34. * inL1 và inL2 là 2 chân tín hiệu điều khiển động cơ bên trái
  35. * action 0 : đứng yên
  36. * action 1 : đi thẳng
  37. * action 2 : lệch phải
  38. * action 3 : lệch trái
  39. */
  40. switch (action)
  41. {
  42. case 0: // đứng yên
  43. controlMotor_direction(inR1, inR2, 0);
  44. controlMotor_direction(inL1, inL2, 0);
  45. break;
  46. case 1: // di thang
  47. controlMotor_direction(inR1, inR2, 1);
  48. controlMotor_direction(inL1, inL2, 1);
  49. break;
  50. case 2: // lech trai
  51. controlMotor_direction(inR1, inR2, 2);
  52. controlMotor_direction(inL1, inL2, 1);
  53. break;
  54. case 3: // lech phai
  55. controlMotor_direction(inR1, inR2, 1);
  56. controlMotor_direction(inL1, inL2, 2);
  57. break;
  58. default:
  59. action=0;
  60. }
  61. }
  62. void controlMotor_direction(byte in1, byte in2, byte direction)
  63. {
  64. // in1,in2 là tín hiệu điều khiển hướng motor
  65. //0 : stop
  66. //1 : di cùng chiều
  67. //2 : đi ngược chiều
  68.  
  69. switch (direction)
  70. {
  71. case 0: // đứng im
  72. digitalWrite (in1, LOW);
  73. digitalWrite (in2, LOW);
  74. break;
  75. case 1: // quay cùng chiều
  76. digitalWrite (in1, HIGH);
  77. digitalWrite (in2, LOW);
  78. break;
  79. case 2: // quay ngược chiều
  80. digitalWrite (in1, LOW);
  81. digitalWrite (in2, HIGH);
  82. break;
  83. }
  84. }
  85. void control_Robot(byte inR1, byte inR2, byte inL1, byte inL2, byte center_distance)
  86. {
  87. // RobotMove(inR1,inR2,inL1,inL2,1);
  88. // Serial.println("Tiến");
  89. int distance = Mspeed;
  90. if(distance = center_distance)
  91. {
  92. RobotMove(inR1,inR2,inL1,inL2,1);
  93. analogWrite(ENA,Mspeed);
  94. analogWrite(ENB,Mspeed);
  95. Serial.println("đi thẳng");
  96. delay(1000);
  97. }
  98. if (distance > center_distance)
  99. {
  100. RobotMove(inR1,inR2,inL1,inL2,3);
  101. analogWrite(ENA,Mspeed);
  102. analogWrite(ENB,Mspeed);
  103. Serial.println("lệch phải");
  104. delay(1000);
  105. }
  106.  
  107. if (distance < center_distance)
  108. {
  109. RobotMove(inR1,inR2,inL1,inL2,2);
  110. analogWrite(ENA,Mspeed);
  111. analogWrite(ENB,Mspeed);
  112. Serial.println("lệch trái");
  113. delay(1000);
  114. }
  115. }
Add Comment
Please, Sign In to add comment