#define In1pin 7 #define In2pin 8 #define Speedpin 9 #define asingle A6 #define Step A1 #define Dir A0 #define Enable 12 #define setstep 400 // 1/2步模式 一圈 步长400 #define nw_step setstep * 1.7 // 400 x 1.7 = 680步 #define delayM 600 // 延时微秒 int x; void setup() { pinMode(Enable,OUTPUT); pinMode(Step,OUTPUT); pinMode(Dir,OUTPUT); digitalWrite(Enable,LOW); Serial.begin(9600); pinMode(In1pin, OUTPUT); pinMode(In2pin, OUTPUT); pinMode(Speedpin, OUTPUT); pinMode(asingle, INPUT); digitalWrite(In1pin,HIGH); digitalWrite(In2pin,LOW); analogWrite(Speedpin,200); } void(* resetFunc) (void) = 0; //制造重启命令 void DCMOTOR_RUN(){ digitalWrite(In1pin,HIGH); digitalWrite(In2pin,LOW); analogWrite(Speedpin,200); } void DCMOTOR_STOP(){ digitalWrite(In1pin,LOW); digitalWrite(In2pin,LOW); analogWrite(Speedpin,0); } void STEP_F(){ digitalWrite(Dir,HIGH); for(x = 0; x < setstep; x++) { digitalWrite(Step,HIGH); delayMicroseconds(delayM); digitalWrite(Step,LOW); delayMicroseconds(delayM); if(!(10000<=millis() && millis()<=20000)){break;} }} void STEP_B(){ digitalWrite(Dir,LOW); for(x = 0; x < setstep; x++) { digitalWrite(Step,HIGH); delayMicroseconds(delayM); digitalWrite(Step,LOW); delayMicroseconds(delayM); if(!(25000<=millis() && millis()<=35000)){break;} }} void STEP_CONTROL(){ } void loop() { DCMOTOR_RUN(); // 直流电机转动 int val = analogRead(asingle); val = map(val,0,1023,0,255); Serial.print("A6类比信号为:");Serial.println(val); Serial.print("millis()为:");Serial.println(millis()); // 内部计数器 while(1){ if(10000<=millis() && millis()<=20000) { DCMOTOR_STOP(); STEP_F(); } else if(25000<=millis() && millis()<=35000) { DCMOTOR_STOP(); STEP_B(); } else if(10000<=millis()&& millis()<=35000) { DCMOTOR_STOP(); } // 直流电机停止 else{break;} } if (millis() >= 40000) { // 复位函数 重置程序 resetFunc(); } //STEP_CONTROL(); }