|
楼主 |
发表于 2017-12-5 14:39:11
|
显示全部楼层
- int in1=7;
- int in2=6;
- int in3=5;
- int in4=4;
- int Pin0 = 10;/*PUL*/
- int Pin2 = 9;/*DIR*/
- int Pin3 = 8;/*ENA*/
- boolean dir = true;
- void setup()
- {
- pinMode(Pin0,OUTPUT);
- // pinMode(Pin1,OUTPUT);
- pinMode(Pin2,OUTPUT);
- pinMode(Pin3,OUTPUT);
- pinMode(in1,OUTPUT);
- pinMode(in2,OUTPUT);
- pinMode(in3,OUTPUT);
- pinMode(in4,OUTPUT);
- //下面程序开始时让控制端都为高电平,电机保持不动。
- digitalWrite(in1,HIGH);
- digitalWrite(in2,HIGH);
- digitalWrite(in3,HIGH);
- digitalWrite(in4,HIGH);
- }
- void clockwiseRotation() //
- {
- digitalWrite(Pin2,LOW);
- digitalWrite(Pin3,LOW);
- digitalWrite(Pin0,LOW);
- delayMicroseconds(1080);
- digitalWrite(Pin0,HIGH);
- delayMicroseconds(1080);
- }
- void negativeRotation()
- {
- digitalWrite(Pin2,HIGH);
- digitalWrite(Pin3,LOW);
- digitalWrite(Pin0,LOW);
- delayMicroseconds(1080);
- digitalWrite(Pin0,HIGH);
- delayMicroseconds(1080);
- }
- /*
- Multiple_Of_180_Degrees:Stepper motor rotation multiples of180degrees;
- Speed:Adjust the speed of stepper motor //圈数
- */
- void stepByStepClockwiseRotation(unsigned int Multiple_Of_180_Degrees) //+180.
- {
- unsigned int n=0;
- for(n = 0;n < 200 * Multiple_Of_180_Degrees;n++)
- {
- clockwiseRotation(); //The Speed value, the greater the stepper motor rotate Speed is slow
- }
- dir = true;
-
- }
- void stepByStepNegativeRotation(unsigned int Multiple_Of_180_Degrees) //-180.
- {
- unsigned int n=0;
- for(n = 0;n < 200 * Multiple_Of_180_Degrees ;n++)
- {
- negativeRotation();
- }
- dir = false;
- }
- void mRight(int pin1,int pin2)//电机右转,电机到底是右转还是左转取决于电机端的接线和控制脚的顺序
- {
- digitalWrite(pin1,HIGH);
- digitalWrite(pin2,LOW);
- }
- void mLeft(int pin1,int pin2)//同上
- {
- digitalWrite(pin1,LOW);
- digitalWrite(pin2,HIGH);
- }
- void mStop(int pin1,int pin2)//紧急制动,实际就是将电机两个端短接了
- {
- digitalWrite(pin1,HIGH);
- digitalWrite(pin2,HIGH);
- }
- void motor1()
- {
- mRight(in1,in2);
- mLeft(in3,in4);
- delay(1000);
- mStop(in1,in2);
- mStop(in3,in4);
- }
- void motor2()
- {
- mRight(in3,in4);
- mLeft(in1,in2);
- delay(1000);
- mStop(in1,in2);
- mStop(in3,in4);
- }
- void loop()
- {
-
- while(1)
- {
- if(dir)
- {
- stepByStepNegativeRotation(4);
- delay(1000);
- motor1();
- }
- else
- {
- stepByStepClockwiseRotation(4);//间隔
- delay(1000);
- motor2();
- }
- delay(1000);
- }
- }
复制代码
这是电机的
1031835478 |
|