int echo_pin = 7;
int trig_pin = 8;
int green_led = 22;
int red_led = 21;
int dir = 26;
int step = 27;
float duration , distance;
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
pinMode(echo_pin , INPUT);
pinMode(trig_pin , OUTPUT);
pinMode(step , OUTPUT);
pinMode(dir , OUTPUT);
pinMode(green_led , OUTPUT);
pinMode(red_led , OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig_pin , LOW);
delayMicroseconds(2);
digitalWrite(trig_pin , HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin , LOW);
duration = pulseIn(echo_pin , HIGH);
distance = duration * 0.0343 / 2;
if(distance <= 250){
digitalWrite(dir,HIGH);
for(int x = 0; x < 200; x++) {
digitalWrite(step ,HIGH);
delayMicroseconds(5000);
digitalWrite(step ,LOW);
delayMicroseconds(5000);
if( x >= 100 ){
digitalWrite(green_led , HIGH);
digitalWrite(red_led , LOW);
}else{
digitalWrite(red_led, HIGH);
digitalWrite(green_led, LOW);
}
}
delay(1000);
digitalWrite(dir , LOW);
}
}