#include const int trigPin = 4; // Connected to PA1 const int echoPin = 3; // Connected to PA0 #define PLAY 2 const int stepsPerRevolution = 200; Stepper myStepper(stepsPerRevolution, 7, 8, 9, 10); // defines variables that are used in this code !! long duration; int distance; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT ! pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT ! pinMode(PLAY, OUTPUT);// set pin as OUTPUT for PLAY digitalWrite(PLAY, HIGH);//turn OFF the pin: PLAY myStepper.setSpeed(60); } void loop() { // this to clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // this to calculate the distance !! distance= duration*0.034/2; // Here is the condition that if the distance is less or equal to 5 the LED will be on !! if (distance >= 23 ){ myStepper.step(200); delay(5000); digitalWrite(PLAY,LOW);// start the command delay(4000);// give it a 400ms time to execute digitalWrite(PLAY,HIGH);// stop the command delay(4000);// give it a 400ms time to stop delay(5000); } digitalWrite(PLAY, HIGH);//turn OFF the pin: PLAY }