I want stepper motor and ultrasonic sensors to work at the same time, but I couldn't find a solution. I tried to shorten the time of the stepper motor, but it always stays the same and this time is too long. here are the codes
The stepper lib you are using blocks while the stepper is moving.
You could try my MobaTools library to drive the stepper. This lib creates the stepping in timer interrupts independent from loop - so the stepper runs in the background independent from your sensors. In loop it is only necessary to set speed and target position ( which can be changed at any time ).
Can you explain what this sketch should do?
Why use an analog input, when you don't use that value? Only 0 or not 0 can also be be done by a digital input. And when do you set the speed to a reasonable value again after it has been set to 0?
I just want it to go 180 degrees + and - direction. When the value of the potentiometer is 0, the stepper motor needs to stop. If you just write the code that rotates 180 degrees in + and - directions, I can do the rest. like this
The library works fine in the background, but I couldn't do a single thing. It works when the value of the potentiometer is not 0 and stops when it is 0, but when I turn the potentiometer back on, it does not start to rotate. what is the solution to this? (These are part of the codes.)
You must not use myStepper.setSpeedSteps( 0 ); to stop the stepper ( speed=0 is invalid and will be set to the minimum value internally ). MoToStepper is a positioning system. The motor runs up to the target position and stops there automatically. But you can change the target position at any time - no matter if the motor has reached the previous target position or not. To stop the motor while it is running, you must set the target position to the actual position of the motor. There are several methods to accomplish this.
myStepper.rotate(0) - this sets a new target postion one ramplen beyond of the actual position. The motor decelerates immediatly and stops at the end of the ramp.
myStepper.doSteps(0) - this sets the target position to the actual position. The motor decelerates until stop, and than moves back to the position where it was at the moment of issuing the command.
myStepper.stop() - this is an emergency stop. The motor stops immediatly without decelerating. This may lead to step errors, if the motor mechanically cannot stop that fast.
If no accelerating/decelerating is defined ( rampLen = 0 ), all three methods behave the same.
Speed and ramplen are remembered and need not to be set again, when setting a new Target position to start the motor again.
A new target can be set absolutely with mystepper.write(angle); or mystepper.writeSteps(steps); measured from the reference point (zeropoint). Or it can be set in steps relative to the actual position with myStepper.doSteps(relativeSteps);
Continuing the discussion from I want stepper motor and ultrasonic sensors to work at the same time:
Hello Hasan.
I'm working on a radar project using an ultrasonic and a stepper motor. Once the ultrasonic detects an object, it will start beeping using a buzzer. The stepper motor is to move the ultrasonic sensor. I have very little experience in coding and I would greatly appreciate it if you can help me with my code. Thats what I have so far, I don't know if thats correct, I've been trying to teach myself via youtube: #include <Stepper.h> #define STEPS 255 // the number of steps in one revolution of your motor (28BYJ-48)
const int stepsPerRevolution = 2038;
const int trigPin = 7;
const int echoPin = 6;
const int buzzer = 12;
const int ledPin = 13;
long duration;
int distance;
int roundAngle;
int Angle;
Stepper stepper(STEPS, 8, 10, 9, 11);
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
stepper.setSpeed(60);
stepper.step(1300);
stepper.step(-1300);
}