Guest User

Untitled

a guest
Oct 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. #define servo_pin 9
  4.  
  5. #define sensor_id_number 2
  6.  
  7. #define led_status_pin 13
  8. #define sensor_trig_pin 3
  9. #define sensor_echo_pin 2
  10. #define led_green_pin 7
  11. #define led_red_pin 8
  12.  
  13. Servo servo;
  14.  
  15. void setup() {
  16. pinMode(sensor_trig_pin, OUTPUT);
  17. pinMode(sensor_echo_pin, INPUT);
  18. pinMode(led_green_pin, OUTPUT);
  19. pinMode(led_red_pin, OUTPUT);
  20. pinMode(led_status_pin, OUTPUT);
  21.  
  22. servo.attach(servo_pin);
  23.  
  24. Serial.begin (9600);
  25. }
  26.  
  27. void loop() {
  28. long duration, distance;
  29.  
  30. digitalWrite(led_status_pin, HIGH);
  31. //servo.write(1);
  32.  
  33. digitalWrite(sensor_trig_pin, LOW);
  34. delayMicroseconds(2);
  35. digitalWrite(sensor_trig_pin, HIGH);
  36. delayMicroseconds(10);
  37. digitalWrite(sensor_trig_pin, LOW);
  38. duration = pulseIn(sensor_echo_pin, HIGH);
  39. distance = (duration/2) / 29.1;
  40.  
  41. check_distance(distance);
  42.  
  43. Serial.print("Sensor ");
  44. Serial.print(distance);
  45. Serial.println(" cm");
  46.  
  47. digitalWrite(led_status_pin, LOW);
  48. //servo.write(90);
  49. delay(1000);
  50. }
  51.  
  52. long ls_distances[3] = {};
  53. int ls_distances_p = 0;
  54. int ls_distances_max = 3;
  55.  
  56. void check_distance(long distance) {
  57.  
  58. if (ls_distances_p >= ls_distances_max)
  59. ls_distances_p = 0;
  60. ls_distances[ls_distances_p++] = distance;
  61.  
  62. long avg = 0;
  63. for(int x = 0; x < ls_distances_max; x++) {
  64. avg += ls_distances[x];
  65. }
  66. avg /= ls_distances_max;
  67.  
  68. Serial.print("Avg ");
  69. Serial.print(avg);
  70. Serial.println(" cm");
  71.  
  72.  
  73. if (avg == 0) {
  74. set_status(0);
  75. } else if (avg >= 50) {
  76. set_status(2);
  77. } else {
  78. set_status(1);
  79. }
  80.  
  81. }
  82.  
  83. int last_status = 0;
  84. int current_status = 0;
  85. // status == 1 => OPEN
  86. // status == 2 => CLOSE
  87.  
  88. void set_status(int sta)
  89. {
  90. if (sta == 1) {
  91. digitalWrite(led_green_pin, HIGH);
  92. digitalWrite(led_red_pin, LOW);
  93.  
  94. current_status = 1;
  95.  
  96. } else if (sta == 2) {
  97. digitalWrite(led_green_pin, LOW);
  98. digitalWrite(led_red_pin, HIGH);
  99.  
  100. current_status = 2;
  101.  
  102. } else {
  103. digitalWrite(led_green_pin, LOW);
  104. digitalWrite(led_red_pin, LOW);
  105.  
  106. current_status = 1;
  107. }
  108.  
  109.  
  110. if (last_status != current_status) {
  111.  
  112. Serial.print("Status ");
  113. Serial.print(current_status);
  114. Serial.println("");
  115.  
  116.  
  117. if (current_status == 2) {
  118. servo.write(60);
  119. delay(3000);
  120.  
  121. } else if (current_status == 1) {
  122. servo.write(160);
  123. delay(3000);
  124. }
  125.  
  126. last_status = current_status;
  127.  
  128. }
  129.  
  130.  
  131. }
Add Comment
Please, Sign In to add comment