aytdev

sonar code 2

Apr 23rd, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. #define DW digitalWrite
  2. #define PM pinMode
  3. #define H HIGH
  4. #define L LOW
  5. #define OP OUTPUT
  6. #define SB Serial.begin
  7. int pins[3] = {A1,A2,A3};
  8. float duration, distance;
  9. void setup() {
  10. PM(pins[0],OP);
  11. PM(pins[1],INPUT);
  12. PM(pins[2], OP);
  13. SB(9600);
  14. }
  15.  
  16. void loop() {
  17. DW(pins[0],L);
  18. delay(2);
  19. DW(pins[0],H);
  20. delayMicroseconds(10);
  21. DW(pins[0],L);
  22. duration = pulseIn(pins[1],H);
  23. distance = (duration * 0.034 * 0.5);
  24. if(distance<=100){
  25.  
  26. DW(pins[2],H);
  27. }else{
  28. DW(pins[2],L);
  29. }
  30. delay(500);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment