Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #define trigPin 11
  2. #define echoPin 10
  3. #define led LED_BUILTIN
  4. #define movePin 8
  5. #define ledPin 9
  6.  
  7. void setup() {
  8. Serial.begin (9600);
  9. pinMode(trigPin, OUTPUT);
  10. pinMode(echoPin, INPUT);
  11. pinMode(movePin, INPUT);
  12. pinMode(led, OUTPUT);
  13. pinMode(ledPin, OUTPUT);
  14. }
  15.  
  16. void loop() {
  17. long duration, distance;
  18. digitalWrite(trigPin, LOW); // Added this line
  19. delayMicroseconds(2); // Added this line
  20. digitalWrite(trigPin, HIGH);
  21. // delayMicroseconds(1000); - Removed this line
  22. delayMicroseconds(10); // Added this line
  23. digitalWrite(trigPin, LOW);
  24. duration = pulseIn(echoPin, HIGH);
  25. distance = (duration/2) / 29.1;
  26. if (distance < 4) { // This is where the LED On/Off happens
  27. // ruch
  28. int val = digitalRead(movePin); // read input value
  29. if (val == HIGH) {
  30. Serial.println("OTWIERANIE!!xd");
  31. digitalWrite(ledPin, HIGH);
  32. }
  33.  
  34. digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  35. }
  36. else {
  37. digitalWrite(led,LOW);
  38. }
  39. if (distance >= 200 || distance <= 0){
  40. Serial.println("Out of range");
  41. }
  42. else {
  43. Serial.print(distance);
  44. Serial.println(" cm");
  45. }
  46. delay(500);
  47. digitalWrite(ledPin, LOW);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement