当前位置:网站首页>Arduino+a4988 control stepper motor

Arduino+a4988 control stepper motor

2022-07-05 08:28:00 Sharing master

* foot 6(-en) Low level refers to starting motor (enable), It seems that you can not answer , Tried to run the same . But if you want to control the starting and closing of the motor, you still need to use

* foot 4(-dir) Control the direction with high and low levels .

* foot 5(-step) Drive the motor to rotate with high and low levels . Note the microsecond value of the waiting interval , If it is too fast, it will cause the motor to make a sound and do not rotate .

const int stepPin = 12; //D6
const int dirPin = 14; //D5
const int enable = 16; //D0 
const int key1 = 5; //D1   Forward 0.5
const int key2 = 4; //D2   back off 0.5
const int key3 = 0; //D3   Back to 0
int ButtonState = HIGH;
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enable,OUTPUT); 
  pinMode(key1,INPUT_PULLUP); 
  pinMode(key2,INPUT_PULLUP);
  pinMode(key3,INPUT_PULLUP); 
  Serial.begin(9600);
  Serial.println("Sensor Test");
  Serial.println("");
}

void step(boolean dir, int steps) 
{
digitalWrite(enable, LOW);//enable
digitalWrite(dirPin, dir); //dir=1
delay(50);
for (int i = 0; i < steps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepPin, LOW);
delayMicroseconds(800);
}
digitalWrite(enable, HIGH);//disable
}

void addone() // Forward 0.5
{
int reading = digitalRead(key1); 
  if (reading != ButtonState) {
   delayMicroseconds(500);
   reading = digitalRead(key1); 
    if (reading == LOW) {
        step(true, 200); delay(500); 
        Serial.println("---------------- Forward --------------------");
      }
  }
}

void minone() // back off 0.5
{
 int reading = digitalRead(key2); 
  if (reading != ButtonState) {
   delayMicroseconds(500);
   reading = digitalRead(key2); 
    if (reading == LOW) {
        step(false, 200); delay(500); 
        Serial.println("---------------- back off --------------------");
      }
  } 
}

void zreoone() // Back to 0
{
 int reading = digitalRead(key3); 
  if (reading != ButtonState) {
   delayMicroseconds(500);
   reading = digitalRead(key3); 
    if (reading == LOW) {
         for (int i = 0; i < 2; i++) {
                                           step(false, 500); delay(500); 
                                           delayMicroseconds(800);
                                           Serial.println("----------------KEY3--------------------");
                                      }
                          } 
 
  }

void estkey()  // Key judgment
{
Serial.println("----------------OK--------------------");
}

void loop() 
{
addone();
minone();
zreoone();
}

 

Reference article Arduino+A4988+ Stepper motor      Press the key to shake off

原网站

版权声明
本文为[Sharing master]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140544214049.html