Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Define the pins you want to use as trigger and echo.
- */
- #define ECHOPIN 2 // Pin to receive echo pulse
- #define TRIGPIN 3 // Pin to send trigger pulse
- /*
- * setup function
- * Initialize the serial line (D0 & D1) at 115200.
- * Then set the pin defined to receive echo in INPUT
- * and the pin to trigger to OUTPUT.
- */
- void setup()
- {
- Serial.begin(115200);
- pinMode(ECHOPIN, INPUT);
- pinMode(TRIGPIN, OUTPUT);
- pinMode(11, OUTPUT);
- }
- /*
- * loop function.
- *
- */
- void loop()
- {
- // Start Ranging
- digitalWrite(TRIGPIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGPIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGPIN, LOW);
- // Compute distance
- float distance = pulseIn(ECHOPIN, HIGH);
- distance= distance/58;
- Serial.println(distance);//print distance to Serial
- Serial.println("cm");
- if(distance <= 90.00){// if the distance is more than 90cm
- digitalWrite(11, HIGH); //pin 11 0n
- delay(1000);//wait
- }
- if(distance >= 90.00){//if less than 90
- digitalWrite(11, LOW); //pin 11 off
- }
- delay(200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement