You are on page 1of 2

#include <Servo.

h>
Servo servo1;
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
servo1.attach(7);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);// put your setup code here, to run once:
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();
}

void loop()
{
ultra();
servo1.write(0);
lcd.setCursor(2,0);
lcd.print("SWIPE SENSOR");
lcd.setCursor(5,1);
lcd.print("PLEASE");
if(distance <= 20){
servo1.write(80);
lcd.setCursor(0,0);
lcd.print("FOR MOTHER EARTH");
lcd.setCursor(3,1);
lcd.print("THANK YOU!");
delay(3000);
lcd.clear();}
}

void ultra(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}

You might also like