Advertisement
Guest User

Untitled

a guest
Nov 11th, 2012
1,691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <Servo.h> //This line is needed to use the servo library
  2. Servo servo1; //This line create a servo named "servo1"
  3. int buffer; //two more integer variables we will use as buffers
  4. const int pingPin = 3, led = 4; //This is an variable we are setting to 3 and 4 to make it easier to read/write from those pins
  5.  
  6. void setup() {
  7. servo1.attach(2); //set the servo to pin 2
  8. pinMode(led, OUTPUT); //set the led pin as an output
  9. Serial.begin(9600); // initialize serial communication, this will let us read/write data back and forth (this is useful for debugging)
  10. }
  11.  
  12. void loop()
  13. {
  14. long duration, inches; // establish variables for duration of the ping, and the distance result in inches
  15.  
  16. // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  17. pinMode(pingPin, OUTPUT);
  18. digitalWrite(pingPin, LOW);
  19. delayMicroseconds(2);
  20. digitalWrite(pingPin, HIGH);
  21. delayMicroseconds(5);
  22. digitalWrite(pingPin, LOW);
  23.  
  24. pinMode(pingPin, INPUT); //Set the pingPin to an input to read from
  25. inches = pulseIn(pingPin, HIGH)/ 74 / 2; //read from the pin, divide by 74 to account for the speed of sound, then by 2 to account for the return trip)
  26.  
  27. if (inches<20) flusher(); //if the reading is less than 20 start the flush function
  28. else //if the reading is not less than 20
  29. {
  30. buffer--; //decrease the buffer
  31. if (buffer<0) buffer=0; //make sure the buffer cant be less than 0
  32. servo1.write(360); //put the servo in the far right position
  33. digitalWrite(led, LOW); //turn off the LED
  34. }
  35. delay(100); //wait a 10th of a second before checking again
  36. }
  37.  
  38. void flusher()
  39. {
  40. buffer++; //increase the buffer counter
  41. if (buffer>15) //if the buffer counter is over 15 (which would be 1500 or 1.5 seconds)
  42. {
  43. servo1.write(41); //put the servo in the far left position
  44. digitalWrite(led, HIGH); //turn the led on
  45. delay(1000); //wait one second
  46. servo1.write(360); //put the servo in the far right position
  47. delay(3000); //wait 3 second
  48. buffer=0; //reset the buffer
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement