Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ECHOPIN 6// Pin to receive echo pulse
- #define TRIGPIN A0// Pin to send trigger pulse
- #define ENPIN 5
- void setup(){
- Serial.begin(57600);
- pinMode(ECHOPIN, INPUT_PULLUP);
- pinMode(TRIGPIN, OUTPUT);
- pinMode(ENPIN, OUTPUT);
- }
- void loop(){
- digitalWrite(ENPIN, HIGH);
- delay(250);
- digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
- delayMicroseconds(2);
- digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
- delayMicroseconds(10);
- digitalWrite(TRIGPIN, LOW); // Send pin low again
- int distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
- distance= distance/58L;
- Serial.print(distance);
- Serial.println(" cm");
- digitalWrite(ENPIN, LOW);
- delay(500);// Wait 50mS before next ranging
- }
Advertisement
Add Comment
Please, Sign In to add comment