// عمرو رأفت السيد محمد فتح الله
// سكشن 6
// 200458
#define echo 2
#define trigger 3
#define step 22
#define dir 21
#define enable 27
#define led 5
const int steps_per_rev = 1500;
int distance ;
long duration ;
bool opened = false ;
bool closed = false;
void setup() {
  Serial1.begin(115200);
  Serial1.println("Hello, Raspberry Pi Pico!");
  pinMode(trigger , OUTPUT);
  pinMode(echo , INPUT);
  pinMode(step , OUTPUT);
  pinMode(dir , OUTPUT);
  pinMode(enable , OUTPUT);
  pinMode(led , OUTPUT);
  digitalWrite(enable, HIGH);
}

void loop() {

  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2;
  Serial1.print("Distance: ");
  Serial1.println(distance);
  if(distance >= 200){
    if(opened == false){
    with();
    }
  }else{
    if(closed == false){
  counter();
    }
  }
  delay(2000);
}
void with(){
  digitalWrite(dir, HIGH);
  digitalWrite(enable, LOW);
  digitalWrite(led, HIGH);
  for(int i = 0 ; i <= steps_per_rev; i++){
    digitalWrite(step, HIGH);
    delayMicroseconds(1000);
    digitalWrite(step, LOW);
    delayMicroseconds(1000);
  }
  digitalWrite(led, LOW);
 opened = true;
 closed = false;
}
void counter(){
  digitalWrite(dir, LOW);
  digitalWrite(enable, LOW);
  digitalWrite(led, HIGH);
  for(int i = 0 ; i <= steps_per_rev; i++){
    digitalWrite(step, HIGH);
    delayMicroseconds(1000);
    digitalWrite(step, LOW);
    delayMicroseconds(1000);
  }
  digitalWrite(led, HIGH);
 closed = true;
 opened = false;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
A4988