Advertisement
stephenvdavis

obstacle avoidance with states

Oct 17th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. //Obstacle avoidance with states program
  2.  
  3. #include <ComponentObject.h>
  4. #include <RangeSensor.h>
  5. #include <SparkFun_VL53L1X.h>
  6. #include <vl53l1x_class.h>
  7. #include <vl53l1_error_codes.h>
  8.  
  9. #include <Wire.h>
  10. #include "SparkFun_VL53L1X.h" //everything above is brought in from distance sensor
  11. //Optional interrupt and shutdown pins.
  12. #define SHUTDOWN_PIN 2
  13. #define INTERRUPT_PIN 3
  14.  
  15. int distance;
  16. SFEVL53L1X distanceSensor;
  17.  
  18. unsigned long startMillis; //used for taking time for reference later
  19. unsigned long currentMillis; //used for exiting play state
  20. unsigned long currentMillis2; //used for vibrating motor every 3 minutes
  21. const unsigned long period = 600000; //the value is a number of milliseconds (10 minutes) timer for play mode
  22. const unsigned long period2 = 180000; //this is 3 minutes. timer for vibration
  23.  
  24. #define sensingRangeMax 250 //in mm, 250mm max distance
  25. #define sensingRangeMin 5 //in mm 5mm min distance
  26.  
  27.  
  28. int Left_Distance = 0; //variable for left distance
  29. int Right_Distance = 0; //variable for right distance
  30. int state = 0; //declared for possible states
  31. const int LeftMotorForward = 6;
  32. const int LeftMotorBackward = 8; //these are PWM pins for motor control
  33. const int RightMotorForward = 9;
  34. const int RightMotorBackward = 12;
  35.  
  36. void setup() {
  37. // put your setup code here, to run once:
  38. pinMode(RightMotorForward, OUTPUT);
  39. pinMode(LeftMotorForward, OUTPUT);
  40. pinMode(LeftMotorBackward, OUTPUT);
  41. pinMode(RightMotorBackward, OUTPUT);
  42. Wire.begin();
  43. Serial.begin(9600);
  44. startMillis = millis(); //write start time in varible for comparisons later
  45. }
  46.  
  47. void loop() {
  48. // put your main code here, to run repeatedly:
  49. Left_Distance = Left_Distance_test(); //takes distance from sensor on left side
  50. Serial.println("Your left distance is ");
  51. Serial.println(Left_Distance);
  52. Right_Distance = Right_Distance_test(); / / takes distance from sensor on right side
  53. Serial.println("Your right distance is ");
  54. Serial.println(Right_Distance);
  55.  
  56. switch (state) {
  57.  
  58.  
  59. case 0: //search mode (slow)
  60.  
  61. currentMillis2 = millis(); //will vibrate motor every 3 minutes
  62. if (currentMillis2 - startMillis >= period2) {
  63. entice(); //function that turns on vibrating motor
  64. startMillis = currentMillis; //restarts count to do again in 3 minutes
  65. }
  66.  
  67. else if (Left_Distance > Right_Distance) { //if there is no object detected in left side, angle left
  68. //Angle_left();
  69. }
  70. else if (Right_Distance > Left_Distance) { //if there is no object detected in right side, angle right
  71. //Angle_right();
  72. }
  73. else if (Right_Distance == 0 && Left_Distance == 0) { //if both sensors 0 or "read out of range" then move forward because its clear
  74. //mForward();
  75. }
  76. else if (Right_Distance < 40 || Left_Distance < 40) { //if object is detected closer than 40 mm, turn hard
  77. //hardTurn();
  78. }
  79. break;
  80.  
  81. case 1: //play mode (fast)
  82. currentMillis = millis(); //only is in this state for 10 minutes then goes to search mode
  83. if (currentMillis - startMillis >= period) {
  84. state = 0; //goes back to search mode time expires
  85. startMillis = currentMillis;
  86. }
  87. else if (Left_Distance > Right_Distance) { //if there is no object detected in left side, angle left
  88. //Angle_left();
  89. }
  90. else if (Right_Distance > Left_Distance) { //if there is no object detected in right side, angle right
  91. //Angle_right();
  92. }
  93.  
  94. else if (Right_Distance == 0 && Left_Distance == 0) { //if both sensors read out of range then move forward
  95. //mForward_fast();
  96. }
  97. else if (Right_Distance < 40 || Left_Distance < 40) { //if closer than 40 mm, turn hard
  98. //hardTurn();
  99. }
  100.  
  101. break;
  102.  
  103. case 2: //this will stop motors if z is negative for 5 seconds (upside down)
  104. // mStop();
  105. break;
  106. }
  107. }
  108.  
  109. int Left_Distance_test() {
  110. distanceSensor.startRanging(); //reads distance
  111. int distance = distanceSensor.getDistance();
  112. distanceSensor.stopRanging();
  113.  
  114. if (distance >= sensingRangeMin && distance <= sensingRangeMax)
  115. {
  116. return distance; //send back the distance only if within range
  117. } else {
  118. return 0; //else return zero
  119. }
  120. }
  121.  
  122. int Right_Distance_test() {
  123. /*
  124. distanceSensor.startRanging(); //reads distance
  125. int distance = distanceSensor.getDistance();
  126. distanceSensor.stopRanging();
  127.  
  128. if (distance >= sensingRangeMin && distance <= sensingRangeMax)
  129. {
  130. return distance; //send back the distance only if within range
  131. } else {
  132. return 0; //else return zero
  133. }
  134. */
  135. }
  136. void entice() {
  137. digitalWrite(5, HIGH); //this is the vibrating motor
  138. delay(1000);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement