Ultrasonic with stepper motor arduino

hi
i'm new using Arduino
trying to control stepper motor with ultrasonic
my goal is if the distance is more than 15 cm the stepper motor rotate left until it reach 15 cm then stop
and if the distance is less than 15 rotates right until it reach 15 cm thenn stop rotating
how it is done i'm trying to program it for days

@ayoubmohamed
Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section.

Therefore I have moved your post here. Please be more careful where you post in future.

You may want to read this before you proceed:-
how to get the best out of this forum

Stepping motors can only rotate. So what is the gearing that translates rotary movement to linear movement. In other words how many steps are there in 1 cm?

It unlikely that anyone will write code for you, but we are glad to help you write the code.

What have you tried? Post your best attempt. Tell us what the code actually does and how that differs form what you want.

Post a wiring diagram of the project. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Post a diagram showing the mechanical setup so we can know how it will work.

this is my attempt and also my system

it is balancing rod i use the help of ultrasonic to determine the of the rod when it is goes up or down so the distance will change more than 15 or less than 15

than the motor will move the mass to distance when the rod reach 15 cm high which is the equilibrium case and the mass will move in belt that move it left and right

Images of code are nearly worthless. I cannot copy the code to a text editor or the IDE for examination or verification. Please read the forum guidelines to see how to properly post code and some information on making a good post.

Post the complete verifiable code.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

So where is the diagram showing that system that you were requested to show in post #3?
Your words are almost impossible to understand.

sketch_nov4c.ino (‏1.9 ‏ك.ب)

this is the system i just want to control the stepper motor with the ultrasonic sensor in certin distance the stepper motor start working

Why is it so hard? The forum guideline tells how to post code. I, for one, will not download code.

Posting the same photo again does not clear up our questions.

sorry for not clarified my question but it is my first time using this website

That's Great
But first kindly read the forum use and guidelines and then use it because it will helpful for us as well as you to get your problem solution

hi i'm new using Arduino Uno
I have question about my project if u can help me i will be thankful
I'm using (28BYJ-48) stepper motor with uln2003 and Ultrasonic Sensor (HC-SR04)
my plan is to control the stepper motor using the ultrasonic sensor.
if the distance is more than 15 cm, it should moves to the left
if the distance is less than 15 cm, it should moves to the right
this is to move belt in my project
the ultrasonic works perfectly but the motor vibration without moving

could you guys kindly check my program and help me with this issue?
the code

//declare variables for the motor pins
const float MicrosecondsPerCM = 29.15452;
const float MicrosecondsPerRoundTripCM = MicrosecondsPerCM * 2;  // ~58.3

const byte EchoPin = 7;
const byte TriggerPin = 8;
int motorPin1 = 9;   // Blue   - 28BYJ48 pin 1
int motorPin2 = 10;  // Pink   - 28BYJ48 pin 2
int motorPin3 = 11;  // Yellow - 28BYJ48 pin 3
int motorPin4 = 12;  // Orange - 28BYJ48 pin 4
                     // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made


unsigned long PreviousStepMillis = 0;
unsigned long MillisBetweenSteps = 25;  // milliseconds

void setup() {
  Serial.begin(115200);
  pinMode(8, OUTPUT);
  pinMode(7, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
}
void loop() {
  unsigned long currentMillis = millis();

  boolean hitRightLimit = digitalRead(motorPin3) == LOW;
  boolean hitLeftLimit = digitalRead(motorPin4) == LOW;

  // Do nothing if either limit switch is pressed
  if (hitRightLimit || hitLeftLimit)
    return;

  digitalWrite(TriggerPin, LOW);
  delayMicroseconds(200);
  // Sets the TriggerPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(TriggerPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(TriggerPin, LOW);
  // Reads the EchoPin, returns the sound wave travel time in microseconds
  unsigned long duration = pulseIn(EchoPin, HIGH, 30000ul);
  // Calculating the distance
  int distanceCM = duration / MicrosecondsPerRoundTripCM;


  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distanceCM);
  Serial.println(" cm");

  // Set the direction pin to move toward 20cm.
  digitalWrite(motorPin1, (distanceCM > 15) ? HIGH : LOW);

  // Step periodically
  if (currentMillis - PreviousStepMillis >= MillisBetweenSteps) {
    PreviousStepMillis = currentMillis;
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin2, HIGH);
  }
}

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Can you write (or find) some simpler code to verify that you can actually step the motor?

the only working in my code is the Ultrasonic the motor just vibration without moving i dont know why

Because you don't step it correctly. You have four motor pins, but you step only one of them.
Why not using a library? It's much more easy - especially with this stepper. This is not a step/dir driver/stepper.

.

?it can be done without driver/stepper control circuit

I don't understand what you mean. You always need a driver for your stepper. Arduino cannot control a stepper motor without a driver.

yes i use uln2003