Advertisement
Bosssu

Project 9

Sep 16th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <atx.h>
  2. #define BLACK 200
  3. #define LEFT_SENSOR 5
  4. #define RIGHT_SENSOR 4
  5. int state = 1 ;
  6. void setup()
  7. {
  8. lcd("pressSW1");
  9. sw1_press();
  10. beep();
  11. }
  12. void loop()
  13. {
  14. go_forward(100);
  15. delay(400);
  16. spin_right(100);
  17. delay(470);
  18. go_forward(100);
  19. delay(370);
  20. motor_stop(ALL);
  21. delay(200);
  22. go_forward(100);
  23. delay(800);
  24. motor_stop(ALL);
  25. delay(200);
  26. follow_to_intersection();
  27. motor_stop(ALL);
  28. delay(200);
  29. spin_left(100);
  30. delay(330);
  31. timed_follow2(16000);
  32. motor_stop(ALL);
  33. delay(200);
  34. go_forward(100);
  35. delay(400);
  36. motor_stop(ALL);
  37. delay(200);
  38. timed_follow2(9000);
  39. go_forward(100);
  40. delay(300);
  41. motor_stop(ALL);
  42. sw1_press();
  43. }
  44.  
  45. void spin_left(int t_power)
  46. {
  47. motor(0,t_power);
  48. motor(1,t_power);
  49. motor(2,-t_power);
  50. motor(3,-t_power);
  51. }
  52. void turn_left(int t_power)
  53. {
  54. motor(0,t_power);
  55. motor(1,t_power);
  56. motor(2,0);
  57. motor(3,0);
  58. }
  59. void spin_right(int t_power)
  60. {
  61. motor(0,-t_power);
  62. motor(1,-t_power);
  63. motor(2,t_power);
  64. motor(3,t_power);
  65. }
  66. void go_forward(int f_power)
  67. {
  68. motor(0,f_power);
  69. motor(1,f_power);
  70. motor(2,f_power);
  71. motor(3,f_power);
  72. }
  73.  
  74. void timed_follow( long ms )
  75. {
  76. long int start = millis(); // page 23
  77. while( millis() - start < ms )
  78. {
  79. int left = analogRead(LEFT_SENSOR);
  80. int right = analogRead(RIGHT_SENSOR);
  81. if(left < 200 && right < 200)
  82. {
  83. spin_left(100);
  84. delay(300);
  85. }
  86. else if( left < 200 )
  87. {
  88. spin_left(50);
  89. }
  90. else if( right < 200 )
  91. {
  92. spin_right(50);
  93. }
  94. else
  95. {
  96. go_forward(20);
  97. }
  98. }
  99. }
  100. void timed_follow2( long ms )
  101. {
  102. long int start = millis(); // page 23
  103. while( millis() - start < ms )
  104. {
  105. int left = analogRead(LEFT_SENSOR);
  106. int right = analogRead(RIGHT_SENSOR);
  107. if( left < 200 )
  108. {
  109. spin_left(70);
  110. }
  111. else if( right < 200 )
  112. {
  113. spin_right(70);
  114. }
  115. else
  116. {
  117. go_forward(20);
  118. }
  119. }
  120. }
  121.  
  122. void follow_to_intersection()
  123. {
  124. int left = analogRead(5);
  125. int right = analogRead(4);
  126.  
  127. do {
  128. int left = analogRead(5);
  129. int right = analogRead(4);
  130. if( left < 150 && right < 150 )
  131. {
  132. break;
  133. }
  134. else if( left < 200 )
  135. {
  136. spin_left(50);
  137. }
  138. else if( right < 200 )
  139. {
  140. spin_right(50);
  141. }
  142. else
  143. {
  144. go_forward(20);
  145. }
  146. }
  147. while( 1 );
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement