极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 206|回复: 4

[未解决] 步进电机

[复制链接]
发表于 2017-11-25 17:37:13 | 显示全部楼层 |阅读模式
求利用温湿度传感器控制步进电机正反转

传感器DHT11
电机42
回复

使用道具 举报

 楼主| 发表于 2017-12-5 14:39:11 | 显示全部楼层
通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。
  1. int in1=7;
  2. int in2=6;
  3. int in3=5;
  4. int in4=4;

  5. int Pin0 = 10;/*PUL*/
  6. int Pin2 = 9;/*DIR*/
  7. int Pin3 = 8;/*ENA*/
  8. boolean  dir = true;

  9. void setup()
  10. {
  11.   pinMode(Pin0,OUTPUT);
  12. //  pinMode(Pin1,OUTPUT);
  13.   pinMode(Pin2,OUTPUT);
  14.   pinMode(Pin3,OUTPUT);

  15.   pinMode(in1,OUTPUT);
  16.   pinMode(in2,OUTPUT);
  17.   pinMode(in3,OUTPUT);
  18.   pinMode(in4,OUTPUT);
  19.   //下面程序开始时让控制端都为高电平,电机保持不动。
  20.   digitalWrite(in1,HIGH);
  21.   digitalWrite(in2,HIGH);
  22.   digitalWrite(in3,HIGH);
  23.   digitalWrite(in4,HIGH);
  24. }

  25. void clockwiseRotation()  //
  26. {
  27.     digitalWrite(Pin2,LOW);
  28.     digitalWrite(Pin3,LOW);  
  29.     digitalWrite(Pin0,LOW);
  30.     delayMicroseconds(1080);
  31.     digitalWrite(Pin0,HIGH);
  32.     delayMicroseconds(1080);
  33. }

  34. void negativeRotation()
  35. {
  36.     digitalWrite(Pin2,HIGH);
  37.     digitalWrite(Pin3,LOW);
  38.     digitalWrite(Pin0,LOW);
  39.     delayMicroseconds(1080);
  40.     digitalWrite(Pin0,HIGH);
  41.     delayMicroseconds(1080);
  42. }

  43. /*
  44. Multiple_Of_180_Degrees:Stepper motor rotation multiples of180degrees;
  45. Speed:Adjust the speed of stepper motor //圈数
  46. */
  47. void stepByStepClockwiseRotation(unsigned int Multiple_Of_180_Degrees)   //+180.
  48. {
  49.   unsigned int n=0;
  50.   for(n = 0;n < 200 * Multiple_Of_180_Degrees;n++)
  51.   {
  52.     clockwiseRotation(); //The Speed value, the greater the stepper motor rotate Speed is slow
  53.   }
  54.   dir = true;
  55.   
  56. }

  57. void stepByStepNegativeRotation(unsigned int Multiple_Of_180_Degrees)   //-180.
  58. {
  59.   unsigned int n=0;
  60.   for(n = 0;n < 200 * Multiple_Of_180_Degrees ;n++)
  61.   {
  62.     negativeRotation();
  63.   }
  64.   dir = false;
  65. }

  66. void mRight(int pin1,int pin2)//电机右转,电机到底是右转还是左转取决于电机端的接线和控制脚的顺序
  67. {
  68.   digitalWrite(pin1,HIGH);
  69.   digitalWrite(pin2,LOW);
  70. }

  71. void mLeft(int pin1,int pin2)//同上
  72. {
  73.   digitalWrite(pin1,LOW);
  74.   digitalWrite(pin2,HIGH);
  75. }

  76. void mStop(int pin1,int pin2)//紧急制动,实际就是将电机两个端短接了
  77. {
  78.   digitalWrite(pin1,HIGH);
  79.   digitalWrite(pin2,HIGH);
  80. }

  81. void motor1()
  82. {
  83.   mRight(in1,in2);
  84.   mLeft(in3,in4);
  85.   delay(1000);
  86.   mStop(in1,in2);
  87.   mStop(in3,in4);
  88. }

  89. void motor2()
  90. {
  91.   mRight(in3,in4);
  92.   mLeft(in1,in2);
  93.   delay(1000);
  94.   mStop(in1,in2);
  95.   mStop(in3,in4);
  96. }


  97. void loop()
  98. {
  99.   
  100.   while(1)
  101.   {  
  102.     if(dir)
  103.     {
  104.     stepByStepNegativeRotation(4);
  105.     delay(1000);
  106.     motor1();
  107.     }
  108.     else
  109.    {
  110.     stepByStepClockwiseRotation(4);//间隔
  111.     delay(1000);
  112.     motor2();
  113.    }
  114.     delay(1000);
  115.   }
  116. }
复制代码


这是电机的
1031835478
回复 支持 反对

使用道具 举报

发表于 2017-11-28 13:05:28 | 显示全部楼层
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。
回复 支持 反对

使用道具 举报

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

const int DHpin = 2;&nbsp;&nbsp;
&nbsp;&nbsp;
byte dat[4];&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[0], DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(".");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat[1], DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.println("%");&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print("\tTemperature: ");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat[2], DEC);&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(".");&nbsp;&nbsp;
&nbsp; &nbsp; Serial.print(dat[3], 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
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-6 13:17:59 | 显示全部楼层
通幽境 发表于 2017-11-28 13:05
如果需要思路:传感器-主控板-步进驱动器-步进电机。
如果需要代做:私信我,有偿。

我发不了私信你加我qq1031835478
回复 支持 反对

使用道具 举报

高级模式  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2017-12-15 22:11 , Processed in 0.048797 second(s), 9 queries , File On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表