|
求大佬帮忙改一下程序让电机可以通过温湿度传感器采集的数据实现正反转- int in1=7;
- int in2=6;
- int in3=5;
- int in4=4;
- int potpin=0;//指定模拟端口A0
- int ledpin=13;//指定LED端口 13
- int val=0;//声明临时变量
- 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);
- {pinMode(ledpin,OUTPUT);//设置端口13为输出模式
- Serial.begin(9600);//设置串口波特率为9600
- }
- }
- 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_45_Degrees:Stepper motor rotation multiples of 45 degrees;
- 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(500);
- mStop(in1,in2);
- mStop(in3,in4);
- }
- void motor2()
- {
- mRight(in3,in4);
- mLeft(in1,in2);
- delay(500);
- 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);
- {
- digitalWrite(ledpin,HIGH);//拉高端口13,LED点亮
- delay(50);//延时0.05秒
- digitalWrite(ledpin,LOW);//拉低端口13,关闭LED
- delay(50);//延时0.05 秒
- val=analogRead(potpin);//读取A0口的电压值并赋值到val
- Serial.println(val);//串口发送val值
- }
- if(val>500);
- {
- digitalWrite(Pin2,LOW);
- digitalWrite(Pin3,LOW);
- digitalWrite(Pin0,LOW);
- delayMicroseconds(1080);
- digitalWrite(Pin0,HIGH);
- delayMicroseconds(1080);
- }
- if(val<500)
- {
- digitalWrite(Pin2,HIGH);
- digitalWrite(Pin3,LOW);
- digitalWrite(Pin0,LOW);
- delayMicroseconds(1080);
- digitalWrite(Pin0,HIGH);
- delayMicroseconds(1080);
- }
- }
- }
复制代码
加了温湿度的程序但步进电机的转动不收采集的数据控制
传感器型号DHT11
|
|