#include <LiquidCrystal.h>
#include <Stepper.h>
h
const int STEPS_PER_REV = 200;
const int TRIG_PIN = 7;
const int ECHO_PIN = 6;
const int RS = 12, EN = 11, D4 = 5, D5 = 4, D6 = 3, D7 = 2;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
Stepper myStepper(STEPS_PER_REV, 8, 9, 10, 11);
int stepCount = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
myStepper.setSpeed(10);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration, distanceCm;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distanceCm = duration / 29.1 / 2;
if (distanceCm < 30) {
myStepper.step(1);
stepCount++;
} else {
myStepper.step(-1);
stepCount--;
}
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distanceCm);
lcd.print("cm ");
lcd.setCursor(0, 1);
lcd.print("Steps: ");
lcd.print(stepCount);
delay(50);
}