Controlling Stepper Motor using Ultrasonic proximitySensor

Hello guys, i am very new to arduino, actually i have never done anything on arduino yet, i'm just trying to do my project. We have 28 BYJ-48 Stepper motor and ULN2003A driver. We are expected to design a garage door, and we are using proximity sensor that will control stepper motor and that motor needs to rotate the garage door 90 degrees. But we couldn't manage to do it. Our stepper rotates 90 degrees + and then -. It doesn't stop and also we don't know how to make sensor and motor work together. It is kinda urgent we couldn't make it by ourselves i need help. Thanks

It's urgent and you want us to help you but you aren't going to post your code so we can work on it. Can you explain this thinking or are you thinking at all? Being a coder requires that you be the type of person who thinks.

HINT : There is nothing wrong with the code that you have not posted, nor with the circuit diagram that you have not posted.

#include <Stepper.h>

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

int motorSpeed = 1000; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 128; // number of steps per 1/4 revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {

pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}

void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

#include <Stepper.h>

// the number of steps on your motor
#define STEPS 4096

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// Variables for ultrasonic sensor
// defines pins numbers
const int trigPin = 5;
const int echoPin = 6;

// defines variables
long duration;
float distance;

void setup() {
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);

//setup for the sensor
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication
}

void readSensor() {
//reads the value of ultrasonic sensor and prints it

// 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);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

void loop() {

//clockwise rotation
for(int i = 0; i < 30; i++)
{
// move one step
stepper.step(1);
readSensor();
}
//anticlockwise rotation
for(int i = 0; i < 30; i++)
{
// move one step in reverse
stepper.step(-1);
readSensor();
}

}

And we have this code, but when we put our hand in front of the sensor it stops and when we pull it back it rotates but it rotates 360. We need the opposite when we put the object it will rotate 90 degree when we pull it back it will rotate 90 degree opposite side.

You need to read the sticky thread at the top of any of the forums titled, "How to use this forum - please read". You said your issue was urgent. So that tells me that you want help fast. The biggest thing you can do to get help fast is to not make it harder than necessary for people to help you. Take a few minutes to read the forum rules over there, especially the parts about how to properly post code so someone can easily copy and paste it into an editor and work with it without it being mangled by html code on the forum. There's a section on what sorts of details you would want to include to get the best help.

In this hobby, what separates the wheat from the chaff is the tendency to read the directions first. When writing code with a new API, the first large chunk of it involves just reading and writing little test programs to make sure you understand how it works. That's how you really learn it, not by biting off more than you can chew and expecting the nice people on an internet forum to give you an answer you can turn in.

Follow along here. Answer all the questions. IF you don't know the answer then go figure out the answer. Once you get to the end, you should be able to write this code or at least ask some specific question other than, "I don't know how so please explain it all to me"

Do you understand at all what the code in either post does? The first one is a little too complicated of a way to try to drive a stepper. It includes Stepper.h but never uses it at all. I wouldn't use that at all. The second one just moves a stepper 30 steps one way and 30 steps the other reading the sensor between every step. Can you spot the line in the loop function that reads the sensor? Can you find the definition of that function? What does it do? Can you spot the lines of code that make the stepper step? How many times would you need it to step to move it 90 degrees? Do you know how a for loop works? If not why haven't you looked at the reference section on this site (under Learning at the top of this page)? Do you know how an if statement works? If you called that function to read the sensors, where would the distance be stored? Could you write an if statement to test that distance is greater or less than some number? Could you write code inside that if statement to move your stepper the number of steps you need?

xmperez was banned as he requested.

Any more requests like that ?

:slight_smile: :slight_smile: :slight_smile:

Bob.

do you still have the circuit diagram where the pins connections are.can you send it to me