Advertisement
abdullahkahraman

Arduino Line Follower - v.0.0.6 test

Apr 17th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #define LEFT 0
  2. #define RIGHT 1
  3.  
  4. int back_motor_low = 8;
  5. int back_motor_high = 9;
  6. int back_motor_enable = 10;
  7. int front_motor_low = 13;
  8. int front_motor_high = 12;
  9. int front_motor_enable = 11;
  10.  
  11. int IR_sensor_pin = 2;
  12. int sensor_status = 0;
  13.  
  14. int timeout = 0;
  15. int toggler = 0;
  16. int car_active = 1;
  17. int on_line_time = 0;
  18. int direction = LEFT;
  19.  
  20. void front_motor_turn_left()
  21. {
  22. digitalWrite(front_motor_low, HIGH);
  23. digitalWrite(front_motor_high, LOW);
  24. digitalWrite(front_motor_enable, HIGH);
  25. direction = LEFT;
  26. }
  27.  
  28. void front_motor_turn_right()
  29. {
  30. digitalWrite(front_motor_low, LOW);
  31. digitalWrite(front_motor_high, HIGH);
  32. digitalWrite(front_motor_enable, HIGH);
  33. direction = RIGHT;
  34. }
  35.  
  36. void front_motor_turn_off()
  37. {
  38. digitalWrite(front_motor_enable, LOW);
  39. }
  40.  
  41. void back_motor_run()
  42. {
  43. digitalWrite(back_motor_enable, HIGH);
  44. }
  45.  
  46. void back_motor_stop()
  47. {
  48. digitalWrite(back_motor_enable, LOW);
  49. }
  50.  
  51. int switch_direction()
  52. {
  53. if(direction == RIGHT)
  54. front_motor_turn_left();
  55. else
  56. front_motor_turn_right();
  57. }
  58.  
  59. void setup() {
  60. pinMode(IR_sensor_pin, INPUT);
  61. pinMode(back_motor_low, OUTPUT);
  62. pinMode(back_motor_high, OUTPUT);
  63. pinMode(back_motor_enable, OUTPUT);
  64. pinMode(front_motor_low, OUTPUT);
  65. pinMode(front_motor_high, OUTPUT);
  66. pinMode(front_motor_enable, OUTPUT);
  67.  
  68. digitalWrite(back_motor_low, HIGH);
  69. digitalWrite(back_motor_high, LOW);
  70. digitalWrite(back_motor_enable, LOW);
  71. digitalWrite(front_motor_low, HIGH);
  72. digitalWrite(front_motor_high, LOW);
  73. digitalWrite(front_motor_enable, LOW);
  74. }
  75.  
  76. void loop() {
  77. sensor_status = digitalRead(IR_sensor_pin);
  78.  
  79. if(toggler == 0)
  80. {
  81. toggler = 1;
  82. if (car_active == 1)
  83. {
  84. back_motor_run();
  85. }
  86. }
  87. else
  88. {
  89. back_motor_stop();
  90. toggler = 0;
  91. }
  92.  
  93. if (sensor_status == HIGH)
  94. {
  95. switch direction();
  96. timeout++;
  97. if(timeout>50)
  98. switch_direction();
  99. }
  100. else
  101. {
  102. switch_direction();
  103. on_line_time++;
  104. timeout = 0;
  105. if (on_line_time > 250)
  106. {
  107. on_line_time = 0;
  108. car_active = 1;
  109. }
  110. }
  111.  
  112. delay(10);
  113.  
  114. if (timeout > 300)
  115. {
  116. car_active = 0;
  117. timeout = 0;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement