Need feedback on this code because I am trying to work a Stepper Motor 28BYJ-48 with this code

Yes and this question is wayyy to unspecific !
So two very simple question for you which is very easy to answer with a simple
"Yes" or a simple "no"

did you read what I have written about how stepper-motors work?

did you watch the video that I have posted?

I answer with the same generalised unspecific way what you have to modify
This is just to demonstrate that your generalised questions demand too much.
You have ask specific questions to reduce te work of your potential helpers to that part that is really nescessary.

Nobody here will write ready to use code for you
nor will write a 10 pages tutorial to explain all possible details which you might already know

So here is a generalised answer:
You have to use a stepper-motor-library which is either able to create the step--pulses in the backround or you have to modify the code to work non-blocking to be able to read in the sensor-data and creating the stepper-pulses at the same time

I'm very sure that this generalised answer does not help
As mentioned above this generalised answer is just to demonstrate that you have to be more specific in your questions

And if you didn't realise yet: converting this code from DC-motor to stepper-motor requires more than just changing 10 lines of code

best regards Stefan

Please post a schematic of the stepper circuit. 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. Clear photos of the wiring are often useful, too.

What stepper motor driver?

+1 for the MobaTools stepper library.

have you read Control 28BYJ-48 Stepper Motor with ULN2003 Driver & Arduino

Here is a simple example that may help. It uses the MobaTools stepper library. It is written for 28BYJ steppers (2048 steps per rev) with a ULN2003 diver.

#include <MobaTools.h>

MoToStepper rightStepper(2048, FULLSTEP );  
MoToStepper leftStepper(2048, FULLSTEP);

void setup()
{
   Serial.begin(115200);
   rightStepper.attach( 8, 9, 10, 11 );
   leftStepper.attach( 4, 5, 6, 7 );
   rightStepper.setSpeed( 200 );  // desired RPM x 100.
   rightStepper.setRampLen(100);
   rightStepper.setZero();
   leftStepper.setSpeed( 200 );  // desired RPM x 100.
   leftStepper.setRampLen(100);
   leftStepper.setZero();
}

void loop()
{
   checkSerial();
}

void reverse()
{
   Serial.println(F(" going reverse"));
   rightStepper.setSpeed( 200 );
   leftStepper.setSpeed( 200 );
   rightStepper.rotate(-1);
   leftStepper.rotate(-1);
}

void forward()
{
   Serial.println(F(" going forward"));
   rightStepper.setSpeed( 200 );
   leftStepper.setSpeed( 200 );
   rightStepper.rotate(1);
   leftStepper.rotate(1);
}

void right()
{
   Serial.println(F("right turn"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(0); // change to -1 for spin turn
   leftStepper.rotate(1);
}

void left()
{
   Serial.println(F("left turn"));
   rightStepper.setSpeed( 100 );
   leftStepper.setSpeed( 100 );
   rightStepper.rotate(1);
   leftStepper.rotate(0); // change to -1 for spin turn
}

void stopCar()
{
   Serial.println(F("stop the car"));
   rightStepper.rotate(0);
   leftStepper.rotate(0);
}

void checkSerial()
{
   if (Serial.available())   // Witing for data incoming from the other XBee module
   {
      char tiltDirection = Serial.read();
      Serial.print(F("new direction  "));
      Serial.println(tiltDirection);
      if (tiltDirection == 'B')
      {
         Serial.println(" reverse");
         reverse();
      }
      else if (tiltDirection == 'F')
      {
         Serial.println("Forward");
         forward();
      }
      else if (tiltDirection == 'R')
      {
         Serial.println("Left");
         left();
      }
      else if (tiltDirection == 'L')
      {
         Serial.println("Right");
         right();
      }
      else if (tiltDirection == 'S')
      {
         Serial.println("Stop");
         stopCar();
      }
   }
}

HC-05 Secondary (1).pdf (899.6 KB)
HC-05 Primary (1).pdf (883.0 KB)

Stepper Motor is a 28BYJ-48 ULN2003

I have uploaded that sample code that you sent to me and I am still unavailable to get stepper motor 28byj-48 response.

I tested the code on real hardware so I know that the code works.

Did you change the code to use the right serial port? Baud rate?
Did you change the code to use the right pins for the stepper drivers?


the problem I see is that you have right(8 9 10 100) and left (4 5 6 7) for the stepper motor but the problem is technically I only have one stepper motor so I cant put pin for right(8 9 10 11) and left (4 5 6 7 ). Thank you for your help so far though.

That was a guess because I had not seen a schematic. I thought that there were 2 DC motors being driven with a driver like a L29x. So I wrote the code to replace 2 motors with 2 motors. A bad guess on my part. My mistake for jumping the gun and assuming.

Though I don't know how you get forward, back, right and left with one motor.

I would hope that you get something useful from the code.

Any advice on how to modify the code you sent me so it can work for my stepper motor 28byj-48?

Will you please answer questions?

If you only want one motor, delete everything in the code that refers to the leftStepper. Then you will be left with the code for the rightStepper and it is wired to pins 8, 9, 10 and 11.

Does the motor do anything? Like buzz or vibrate?

Do the LEDs on the driver turn on or flash?

How sure are you that the motor is coils are connected to the driver properly?

I am still unable to get motor response from your code and I got it wired correctly

what does "motor response" mean
You have been asked for several details

you are on the edge of loosing support if you go on ignoring questions that you have been asked

The result of further ignoring questions you have been asked might be that your questions were ignored.

There are enough other users that are more cooperative and do answer questions in detail that they are asked.

So last time asking:
can you

explicitly answer the questions

If you uploaded the code do the LEDs on the stepper-motor-board blink or light up
"yes or "no" ??

Does the stepper-motor buzz or hum
"yes" or "no"??

Does the stepper-motor vibrate?
"Yes" or "no"??

No I can not for both of your questions

What does this mean
"No I can not for both of your questions"???

Does it mean
you can not answer the questions
or
does it mean the answer IS "no" to all THREE questions?

When I upload your code I was unable to get the motor to do anything. It did not buzz or vibrate and no LEDs turn on or flash on the driver.

That means that the driver is not getting signals and/or power. Is there a jumper on the board next to where the power connects? Is the jumper in place?