Answer the question
In order to leave comments, you need to log in
How to control two stepper motors with hc-rs04?
I have 2 stepper motors and an hc-rs04 ultrasonic sensor. And you need to make sure that when hc-rs04 receives a signal within a radius of 30 cm, the stepper motor slows down. I was able to connect only one stepper motor, and when the sound sensor picked up a signal at a distance of 30 cm, the stepper motor slowed down. But when I tried two stepper motors at once, everything stopped working.
Here is the code:
#include <Stepper.h>
const int stepsPerRevolution = 400;
const int stepsPerRevolution1 = 400;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Порты для двигателей
Stepper myStepper2(stepsPerRevolution1, 4,5,6,7);
const int trigPin = 12; // Подключение hc-rs04
const int echoPin =13;
long duration;
int distance;
long duration1;
int distance1;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance > 40) {
myStepper.step(stepsPerRevolution);
myStepper.setSpeed(25);
}
duration1 = pulseIn(echoPin, HIGH);
distance1= duration1*0.034/2;
if(distance1 > 30){
myStepper2.step(stepsPerRevolution1);
myStepper2.setSpeed(25);
}
}
Answer the question
In order to leave comments, you need to log in
Yeah. I still found a solution, and maybe all of you pseudo-programmers didn’t understand me, or maybe you wanted me to be unable to write code and leave robotics so that your workplace is not taken away. In short, who needs this code:
#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
CustomStepper stepper1(4,5,6,7);
const int trigPin = 12;
const int echoPin =13;
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
stepper.setRPM(26);
stepper.setSPR(4075.7728395);
stepper1.setRPM(26);
stepper1.setSPR(4075.7728395);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance > 20) {
stepper.setDirection(CCW);
stepper.rotate(1);
stepper.setRPM(1);
stepper.setSPR(4075.7728395);
stepper1.setDirection(CW);
stepper1.rotate(1);
stepper1.setRPM(1);
stepper1.setSPR(4075.7728395);
}
}
Why issue the pulseIn command twice . You can measure the distance once and perform the necessary actions in the condition, for example, like this:
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance > 40) {
myStepper.step(stepsPerRevolution);
myStepper.setSpeed(25);
}
else if(distance > 30){
myStepper2.step(stepsPerRevolution1);
myStepper2.setSpeed(25);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question