Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <sparki.h>
  2.  
  3. #define STATE_FIRST_ONE 0
  4. #define STATE_SECOND_ONE 1
  5.  
  6. // Set up some global variables with default values to be replaced during operation
  7. int current_state = 0;
  8. const int threshold = 500; // IR reading threshold to detect whether there's a black line under the sensor
  9. int cm_distance = 1000;
  10. int distance = 0;
  11. int line_left = 1000;
  12. int line_center = 1000;
  13. int line_right = 1000;
  14.  
  15.  
  16. void setup() {
  17. // put your setup code here, to run once:
  18. sparki.RGB(RGB_RED); // Turn on the red LED
  19. sparki.servo(SERVO_CENTER); // Center the ultrasonic sensor
  20. delay(1000); // Give the motor time to turn
  21. sparki.gripperOpen(); // Open the gripper
  22. delay(5000); // Give the motor time to open the griper
  23. sparki.gripperStop(); // 5 seconds should be long enough
  24. sparki.RGB(RGB_GREEN); // Change LED to green so we know the robot's setup is done!
  25. }
  26.  
  27. void readSensors() {
  28. cm_distance = sparki.ping(); // Replace with code to read the distance sensor
  29. line_left = sparki.lineLeft(); // Replace with code to read the left IR sensor
  30. line_right = sparki.lineRight(); // Replace with code to read the right IR sensor
  31. line_center = sparki.lineCenter(); // Replace with code to read the center IR sensor
  32. }
  33.  
  34. void loop() {
  35. // put your main code here, to run repeatedly:
  36. readSensors(); // Read sensors once per loop() call
  37.  
  38. sparki.clearLCD();
  39. sparki.print("STATE: ");
  40. sparki.println(current_state);
  41.  
  42. // Your state machine code goes here
  43. switch (current_state) {
  44. case 0:
  45. //rotate sparki around its center
  46. sparki.moveRight();
  47.  
  48. //when sparki detects the object is within 30cm
  49. if (cm_distance != -1) {
  50. // sparki.println(cm_distance);
  51.  
  52.  
  53. if (cm_distance <= 30) {
  54. distance = cm_distance;
  55. sparki.moveStop();
  56. //sparki.println(cm_distance);
  57. current_state = 1;
  58. }
  59. }
  60. break;
  61. case 1:
  62. sparki.moveForward(distance); //move to the object
  63. sparki.moveStop();
  64. current_state = 2;
  65. break;
  66. case 2:
  67. sparki.gripperClose(); //grab the object
  68. delay(5000);
  69. sparki.gripperStop();
  70. sparki.moveRight(180); //rotate 180 degrees
  71. sparki.moveStop();
  72. current_state = 3;
  73. break;
  74. case 3:
  75. sparki.moveForward(); //move towards the line
  76. if (line_center < threshold) { //once sparki detects the line stop
  77. sparki.moveStop();
  78. current_state = 4;
  79. }
  80. break;
  81. case 4:
  82. if ( line_left < threshold ) // if line is below left line sensor
  83. {
  84. sparki.moveLeft(); // turn left
  85. }
  86.  
  87. if ( line_right < threshold ) // if line is below right line sensor
  88. {
  89. sparki.moveRight(); // turn right
  90. }
  91.  
  92. // if the center line sensor is the only one reading a line
  93. if ( (line_center < threshold) && (line_left > threshold) && (line_right > threshold) )
  94. {
  95. sparki.moveForward(); // move forward
  96. }
  97.  
  98. if ((line_left < threshold) && (line_center < threshold) && (line_right < threshold)) {
  99. sparki.moveStop();
  100. current_state = 5;
  101. }
  102. break;
  103. case 5:
  104. sparki.beep();
  105. sparki.gripperOpen();
  106. delay(5000);
  107. sparki.gripperStop();
  108. current_state = 6;
  109. break;
  110. case 6:
  111. sparki.moveStop();
  112. }
  113.  
  114. sparki.updateLCD();
  115. delay(100); // Only run controller at 10Hz
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement