纸鸢arduino 发表于 2017-11-25 17:37:13

步进电机

求利用温湿度传感器控制步进电机正反转

传感器DHT11
电机42

纸鸢arduino 发表于 2017-12-5 14:39:11

通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。

int in1=7;
int in2=6;
int in3=5;
int in4=4;

int Pin0 = 10;/*PUL*/
int Pin2 = 9;/*DIR*/
int Pin3 = 8;/*ENA*/
booleandir = 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

通幽境 发表于 2017-11-28 13:05:28

如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。:)

纸鸢arduino 发表于 2017-12-5 13:28:26

通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。

const int DHpin = 2;&nbsp;&nbsp;
&nbsp;&nbsp;
byte dat;&nbsp;&nbsp;
void setup()&nbsp;&nbsp;
{&nbsp;&nbsp;
&nbsp; &nbsp; pinMode(2, OUTPUT);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.begin(9600);&nbsp;&nbsp;//设置串行每秒传输数据的速率(9600)
}&nbsp;&nbsp;
&nbsp;&nbsp;
void loop()&nbsp;&nbsp;
{&nbsp;&nbsp;
&nbsp; &nbsp; DHT11_test();&nbsp;&nbsp;
&nbsp; &nbsp; show_data();&nbsp;&nbsp;
&nbsp; &nbsp; delay(500);&nbsp;&nbsp;
}&nbsp;&nbsp;
&nbsp;&nbsp;
void show_data(){&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//从 Arduino 串口监视器观察数据&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; Serial.print("Humdity: ");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat, DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(".");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat, DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.println("%");&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print("\tTemperature: ");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat, DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(".");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat, DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.println(" C");&nbsp;&nbsp;
&nbsp;&nbsp;
}&nbsp;&nbsp;
&nbsp;&nbsp;
byte DHT11_read(){&nbsp; &nbsp;&nbsp; &nbsp;//串口通讯,发送8位数据
&nbsp; &nbsp; byte data = 0;&nbsp;&nbsp;
&nbsp; &nbsp; for(int i=0; i<8; i++){&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if( digitalRead(DHpin)==LOW ){&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while( digitalRead(DHpin)==LOW );&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;delayMicroseconds(30);&nbsp;&nbsp;//高电平>30us为“1”,高电平<30us为“0”&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if( digitalRead(DHpin)==HIGH )&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; data |= 1<<(7-i);&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while( digitalRead(DHpin)==HIGH );&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;
&nbsp; &nbsp; }&nbsp;&nbsp;
&nbsp; &nbsp; return data;&nbsp;&nbsp;
}&nbsp;&nbsp;
&nbsp;&nbsp;
void DHT11_test(){&nbsp;&nbsp;
&nbsp; &nbsp; digitalWrite(DHpin, LOW); &nbsp;&nbsp;//拉低总线,发开始信号&nbsp;&nbsp;
&nbsp; &nbsp; delay(30);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;//【延时要大于18ms,以便 DHT11 检测开始信号】
&nbsp; &nbsp; digitalWrite(DHpin, HIGH);&nbsp; &nbsp;
&nbsp; &nbsp; delayMicroseconds(40);&nbsp; &nbsp;&nbsp;&nbsp; //拉高总线,等待 DHT11 响应&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pinMode(DHpin, INPUT); //从 DHT11 中读取温湿度
&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while( digitalRead(DHpin)==HIGH );&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delayMicroseconds(80);&nbsp;&nbsp;// DHT11 发出响应,拉低总线80us
&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;while( digitalRead(DHpin)==LOW );&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delayMicroseconds(80);&nbsp;&nbsp;// DHT11 拉高总线80us后开始发送数据&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;for(int i=0; i<4; i++)&nbsp;&nbsp; //接受温湿度数据【前湿度,后温度】&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;dat = DHT11_read();&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pinMode(DHpin, OUTPUT);&nbsp; &nbsp;
&nbsp; &nbsp; digitalWrite(DHpin, HIGH);&nbsp;&nbsp;//发送完后释放总线,等待下一次开始信号&nbsp;&nbsp;
}&nbsp;
给这个步进电机加个温湿度控制
1031835478

纸鸢arduino 发表于 2017-12-6 13:17:59

通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。

我发不了私信你加我qq1031835478
页: [1]
查看完整版本: 步进电机