步进电机
求利用温湿度传感器控制步进电机正反转传感器DHT11
电机42 通幽境 发表于 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
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。
const int DHpin = 2;
byte dat;
void setup()
{
pinMode(2, OUTPUT);
Serial.begin(9600); //设置串行每秒传输数据的速率(9600)
}
void loop()
{
DHT11_test();
show_data();
delay(500);
}
void show_data(){ //从 Arduino 串口监视器观察数据
Serial.print("Humdity: ");
Serial.print(dat, DEC);
Serial.print(".");
Serial.print(dat, DEC);
Serial.println("%");
Serial.print("\tTemperature: ");
Serial.print(dat, DEC);
Serial.print(".");
Serial.print(dat, DEC);
Serial.println(" C");
}
byte DHT11_read(){ //串口通讯,发送8位数据
byte data = 0;
for(int i=0; i<8; i++){
if( digitalRead(DHpin)==LOW ){
while( digitalRead(DHpin)==LOW );
delayMicroseconds(30); //高电平>30us为“1”,高电平<30us为“0”
if( digitalRead(DHpin)==HIGH )
data |= 1<<(7-i);
while( digitalRead(DHpin)==HIGH );
}
}
return data;
}
void DHT11_test(){
digitalWrite(DHpin, LOW); //拉低总线,发开始信号
delay(30); //【延时要大于18ms,以便 DHT11 检测开始信号】
digitalWrite(DHpin, HIGH);
delayMicroseconds(40); //拉高总线,等待 DHT11 响应
pinMode(DHpin, INPUT); //从 DHT11 中读取温湿度
while( digitalRead(DHpin)==HIGH );
delayMicroseconds(80); // DHT11 发出响应,拉低总线80us
while( digitalRead(DHpin)==LOW );
delayMicroseconds(80); // DHT11 拉高总线80us后开始发送数据
for(int i=0; i<4; i++) //接受温湿度数据【前湿度,后温度】
dat = DHT11_read();
pinMode(DHpin, OUTPUT);
digitalWrite(DHpin, HIGH); //发送完后释放总线,等待下一次开始信号
}
给这个步进电机加个温湿度控制
1031835478 通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。
我发不了私信你加我qq1031835478
页:
[1]