极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13055|回复: 1

利用温湿度传感器控制步进电机

[复制链接]
发表于 2017-12-5 15:14:06 | 显示全部楼层 |阅读模式
求大佬帮忙改一下程序让电机可以通过温湿度传感器采集的数据实现正反转
  1. int in1=7;
  2. int in2=6;
  3. int in3=5;
  4. int in4=4;
  5. int potpin=0;//指定模拟端口A0
  6. int ledpin=13;//指定LED端口 13
  7. int val=0;//声明临时变量

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

  12. void setup()
  13. {
  14.   pinMode(Pin0,OUTPUT);
  15. //  pinMode(Pin1,OUTPUT);
  16.   pinMode(Pin2,OUTPUT);
  17.   pinMode(Pin3,OUTPUT);

  18.   pinMode(in1,OUTPUT);
  19.   pinMode(in2,OUTPUT);
  20.   pinMode(in3,OUTPUT);
  21.   pinMode(in4,OUTPUT);
  22.   //下面程序开始时让控制端都为高电平,电机保持不动。
  23.   digitalWrite(in1,HIGH);
  24.   digitalWrite(in2,HIGH);
  25.   digitalWrite(in3,HIGH);
  26.   digitalWrite(in4,HIGH);
  27.   {pinMode(ledpin,OUTPUT);//设置端口13为输出模式
  28.   Serial.begin(9600);//设置串口波特率为9600
  29.    }
  30. }

  31. void clockwiseRotation()  //
  32. {
  33.     digitalWrite(Pin2,LOW);
  34.     digitalWrite(Pin3,LOW);  
  35.     digitalWrite(Pin0,LOW);
  36.     delayMicroseconds(1080);
  37.     digitalWrite(Pin0,HIGH);
  38.     delayMicroseconds(1080);
  39. }

  40. void negativeRotation()
  41. {
  42.     digitalWrite(Pin2,HIGH);
  43.     digitalWrite(Pin3,LOW);
  44.     digitalWrite(Pin0,LOW);
  45.     delayMicroseconds(1080);
  46.     digitalWrite(Pin0,HIGH);
  47.     delayMicroseconds(1080);
  48. }

  49. /*
  50. Multiple_Of_45_Degrees:Stepper motor rotation multiples of 45 degrees;
  51. Speed:Adjust the speed of stepper motor
  52. */
  53. void stepByStepClockwiseRotation(unsigned int Multiple_Of_180_Degrees)   //+180.
  54. {
  55.   unsigned int n=0;
  56.   for(n = 0;n < 200 * Multiple_Of_180_Degrees;n++)
  57.   {
  58.     clockwiseRotation(); //The Speed value, the greater the stepper motor rotate Speed is slow
  59.   }
  60.   dir = true;
  61.   
  62. }

  63. void stepByStepNegativeRotation(unsigned int Multiple_Of_180_Degrees)   //-180.
  64. {
  65.   unsigned int n=0;
  66.   for(n = 0;n < 200 * Multiple_Of_180_Degrees ;n++)
  67.   {
  68.     negativeRotation();
  69.   }
  70.   dir = false;
  71. }

  72. void mRight(int pin1,int pin2)//电机右转,电机到底是右转还是左转取决于电机端的接线和控制脚的顺序
  73. {
  74.   digitalWrite(pin1,HIGH);
  75.   digitalWrite(pin2,LOW);
  76. }

  77. void mLeft(int pin1,int pin2)//同上
  78. {
  79.   digitalWrite(pin1,LOW);
  80.   digitalWrite(pin2,HIGH);
  81. }

  82. void mStop(int pin1,int pin2)//紧急制动,实际就是将电机两个端短接了
  83. {
  84.   digitalWrite(pin1,HIGH);
  85.   digitalWrite(pin2,HIGH);
  86. }

  87. void motor1()
  88. {
  89.   mRight(in1,in2);
  90.   mLeft(in3,in4);
  91.   delay(500);
  92.   mStop(in1,in2);
  93.   mStop(in3,in4);
  94. }

  95. void motor2()
  96. {
  97.   mRight(in3,in4);
  98.   mLeft(in1,in2);
  99.   delay(500);
  100.   mStop(in1,in2);
  101.   mStop(in3,in4);
  102. }


  103. void loop()
  104. {
  105.   
  106.   while(1)
  107.   {  
  108.     if(dir)
  109.     {
  110.     stepByStepNegativeRotation(4);
  111.     delay(1000);
  112.     motor1();
  113.     }
  114.     else
  115.    {
  116.     stepByStepClockwiseRotation(4);
  117.     delay(1000);
  118.     motor2();
  119.    }
  120.     delay(1000);
  121.     {
  122.   digitalWrite(ledpin,HIGH);//拉高端口13,LED点亮
  123.   delay(50);//延时0.05秒
  124.   digitalWrite(ledpin,LOW);//拉低端口13,关闭LED
  125.   delay(50);//延时0.05 秒
  126.   val=analogRead(potpin);//读取A0口的电压值并赋值到val
  127.   Serial.println(val);//串口发送val值
  128.     }
  129.     if(val>500);
  130.     {
  131.       digitalWrite(Pin2,LOW);
  132.     digitalWrite(Pin3,LOW);  
  133.     digitalWrite(Pin0,LOW);
  134.     delayMicroseconds(1080);
  135.     digitalWrite(Pin0,HIGH);
  136.     delayMicroseconds(1080);
  137.     }
  138.      if(val<500)
  139.     {
  140.       digitalWrite(Pin2,HIGH);
  141.     digitalWrite(Pin3,LOW);
  142.     digitalWrite(Pin0,LOW);
  143.     delayMicroseconds(1080);
  144.     digitalWrite(Pin0,HIGH);
  145.     delayMicroseconds(1080);
  146. }
  147.   }
  148. }
复制代码

加了温湿度的程序但步进电机的转动不收采集的数据控制

传感器型号DHT11
回复

使用道具 举报

发表于 2017-12-10 14:10:04 | 显示全部楼层
现在没时间
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 07:03 , Processed in 0.043041 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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