Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h> //This line is needed to use the servo library
- Servo servo1; //This line create a servo named "servo1"
- int buffer; //two more integer variables we will use as buffers
- 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
- void setup() {
- servo1.attach(2); //set the servo to pin 2
- pinMode(led, OUTPUT); //set the led pin as an output
- Serial.begin(9600); // initialize serial communication, this will let us read/write data back and forth (this is useful for debugging)
- }
- void loop()
- {
- long duration, inches; // establish variables for duration of the ping, and the distance result in inches
- // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
- pinMode(pingPin, OUTPUT);
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(pingPin, LOW);
- pinMode(pingPin, INPUT); //Set the pingPin to an input to read from
- 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)
- if (inches<20) flusher(); //if the reading is less than 20 start the flush function
- else //if the reading is not less than 20
- {
- buffer--; //decrease the buffer
- if (buffer<0) buffer=0; //make sure the buffer cant be less than 0
- servo1.write(360); //put the servo in the far right position
- digitalWrite(led, LOW); //turn off the LED
- }
- delay(100); //wait a 10th of a second before checking again
- }
- void flusher()
- {
- buffer++; //increase the buffer counter
- if (buffer>15) //if the buffer counter is over 15 (which would be 1500 or 1.5 seconds)
- {
- servo1.write(41); //put the servo in the far left position
- digitalWrite(led, HIGH); //turn the led on
- delay(1000); //wait one second
- servo1.write(360); //put the servo in the far right position
- delay(3000); //wait 3 second
- buffer=0; //reset the buffer
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement