Guest User

Untitled

a guest
May 27th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. int outpin=11;// initialize pin 11
  2. int inpin=4;// initialize pin 4
  3. int val;// define val
  4.  
  5.  
  6.  
  7. #define echoPin 7 // Echo Pin
  8. #define trigPin 8 // Trigger Pin
  9. #define LEDPin 13 // Onboard LED
  10. int maximumRange = 200; // Maximum range needed
  11. int minimumRange = 0; // Minimum range needed
  12. long duration, distance; // Duration used to calculate distance
  13.  
  14. void setup() {
  15. Serial.begin (9600);
  16.  
  17. pinMode(outpin,OUTPUT);// set LED pin as “output”
  18. pinMode(inpin,INPUT);// set button pin as “input”
  19.  
  20.  
  21. pinMode(trigPin, OUTPUT);
  22. pinMode(echoPin, INPUT);
  23. pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
  24. }
  25.  
  26. void loop() {
  27.  
  28.  
  29.  
  30. val=digitalRead(inpin);// read the level value of pin 4 and assign if to val
  31. Serial.println(val);
  32. if (val==1) // check if the button is pressed, if yes, turn on the LED
  33. { digitalWrite(outpin,LOW);
  34.  
  35. int m = millis();
  36. /* The following trigPin/echoPin cycle is used to determine the
  37. distance of the nearest object by bouncing soundwaves off of it. */
  38. digitalWrite(trigPin, LOW);
  39. delayMicroseconds(2);
  40.  
  41. digitalWrite(trigPin, HIGH);
  42. delayMicroseconds(10);
  43. digitalWrite(trigPin, LOW);
  44. duration = pulseIn(echoPin, HIGH);
  45.  
  46. //Calculate the distance (in cm) based on the speed of sound.
  47. distance = duration/58.2;
  48.  
  49. if (distance >= maximumRange || distance <= minimumRange){
  50. /* Send a negative number to computer and Turn LED ON
  51. to indicate "out of range" */
  52. Serial.println("-1");
  53. digitalWrite(LEDPin, HIGH);
  54. }
  55. else {
  56. /* Send the distance to the computer using Serial protocol, and
  57. turn LED OFF to indicate successful reading. */
  58.  
  59. Serial.println(distance);
  60. if (distance < 60){
  61. Serial.println(m);}
  62. digitalWrite(LEDPin, LOW);
  63. }
  64.  
  65. //Delay 50ms before next reading.
  66. delay(50);}
  67.  
  68.  
  69. else
  70. { digitalWrite(outpin,HIGH);}
  71. }
Add Comment
Please, Sign In to add comment