M
M
Maly2021-06-20 14:06:03
Arduino
Maly, 2021-06-20 14:06:03

How to control stepper motors with hc-rs04?

I wanted to make it so that when the ultrasonic sensor receives a signal at a distance of 20 cm, the code for the stop stepper motor works. hc-rs04 .

I tried like this:

#include <CustomStepper.h>        
#include <Ultrasonic.h>
CustomStepper stepper1(4,5,6,7); 
CustomStepper stepper2(8,9,10,11);
int example1 = 1; 
int example2 = 1;

Ultrasonic ultrasonic(12, 13);
int distance;
void setup()
{ 
   Serial.begin(9600);
 stepper1.setRPM(16);   //Для первого двигателя это stepper1              
  stepper1.setSPR(4075.7728395);  
   stepper2.setRPM(16);          //И для второго это stepper2      
  stepper2.setSPR(4075.7728395);

}

void loop()
{
 if (stepper1.isDone() and example1 == 1)  
  {
    stepper1.setDirection(CCW);        
    stepper1.rotate(1);                
  }
  stepper1.run(); 

       
 if (stepper2.isDone() and example2 == 1)  
  {
    stepper2.setDirection(CW);        
    stepper2.rotate(1);                
  }
  
  stepper2.run();      
  distance = ultrasonic.read();
  
  Serial.print("Distance in CM: ");
  Serial.println(distance);
  delay(1000);
if(Distance == 20){
stepper=0;
}else if(Distance!==20){
stepper.run();
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Skusnov, 2021-06-21
@AlexSku

Learn the controller language "State Graph", draw a program (there are parallel structures).
An example of Stateflow on MatLab (in Matlab there are examples of porting code to Arduino).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question