Stepper Motor Activated when pass through Ultrasonic Sensor

Hey y'all, I'm trying to make the coding for my project which when the object passes the ultrasonic sensor the stepper will continuously run. I have tried on my own but could never make it work. Below was my approach. Please help me! Thank you in advance!

const int trigPin = 9;
const int echoPin = 8;
int step_number = 0;
long distance;
long duration;
#define STEPPER_PIN_1 10
#define STEPPER_PIN_2 11
#define STEPPER_PIN_3 12
#define STEPPER_PIN_4 13

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
Serial.begin(9600); 
}
void loop() {
  digitalWrite(trigPin, LOW); // Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); 
  distance= duration*0.034/2; // Calculating the distance
}
void OneStep(bool dir){
    if(dir && distance <=9){
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
 
  
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
  }
}

Hi,
You have a function:
" void OneStep(bool dir) {" which is never called in the loop function.

If you want help, you need to state what doesn't work and what does work. Did you do any debugging of your code? Did you test each step as you wrote the code? When did the code go bad?

Does the stepper motor actually run for you? Does the sensor return any values when you test it?

The code runs fine without any issues but nothing worked, the stepper motor doesn't run nor the sensor shows anything. I didn't test each step as I wrote the code.

to run the stepmotor, you need to run the function
" void OneStep(bool dir) {", but it is never called, so the step will never run.

This is contradictory. If nothing worked nothing is fine.
If the code runs fine without any issues things would work.

This is simply a too short description of what really is.
You need to write down more words and simply describe in detail what does work
what does not work

Without describing in detail what works and what works not; I interpret your postings as a hidden attempt trying to make other users writing your code without own effort.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.