极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8905|回复: 3

求助控制直流电机的程序,各位大师请入,感谢~

[复制链接]
发表于 2014-6-10 15:17:06 | 显示全部楼层 |阅读模式
做了一个程序,要求电机一个方向有间隔的旋转,而且有两种旋转模式,一种每隔30秒转10秒,一种每隔50秒转10秒。

而且要求LCD上显示计时,昨儿折腾了一晚上,发现几个问题没办法解决

1,只能单线程执行,如果要中断循环要等最后一个delay延时完成,不知道怎么直接中断,求解。。。

2,计时器的问题,按下按钮开始计时,本来想做一个for或者while的循环语句增量,这个增量的循环没办法和电机的循环一起执行。。彻底没招了,求解。。。

  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>

  3. int pinA1 = 4;
  4. int pinA2 = 5;
  5. int C41_Switch = 7;       //右边按键引脚编号
  6. int D76_Switch = 8;      //左边按键引脚编号
  7. int ST_Switch = 9;      //停止按键引脚编号
  8. boolean C41onoff = LOW;
  9. boolean D76onoff = LOW;
  10. LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display



  11. void setup()
  12. {
  13.   lcd.init(); // initialize the lcd .
  14.   lcd.setCursor(0,0); //newline
  15.   lcd.print("ASH Film ");// Print a message to the LCD
  16.   lcd.setCursor(5,1);
  17.   lcd.print("V0.01");
  18.   lcd.backlight();
  19.   delay(1500); //延时显示2000
  20.   lcd.clear();
  21.   lcd.print("Select Button ");
  22.   lcd.setCursor(0,1);
  23.   lcd.print("B:C41 G:D76 ");
  24.   
  25.   

  26. pinMode(pinA1,OUTPUT);
  27. pinMode(pinA2,OUTPUT);
  28. pinMode(C41_Switch,INPUT);
  29. pinMode(D76_Switch,INPUT);
  30. pinMode(ST_Switch,INPUT);
  31. }

  32. void loop()
  33. {
  34.   if(digitalRead(C41_Switch)==HIGH)
  35.   {
  36.   delay(10);
  37.   if(digitalRead(C41_Switch)==HIGH)
  38.     {
  39.      C41();
  40.      C41onoff=(!C41onoff);
  41.      delay(10);
  42.     }
  43.   }
  44.    if(digitalRead(D76_Switch)==HIGH)
  45.   {
  46.   delay(10);
  47.   if(digitalRead(D76_Switch)==HIGH)
  48.     {
  49.      D76();
  50.      D76onoff=(!D76onoff);
  51.      delay(10);
  52.     }
  53.   }
  54. }


  55. void C41()
  56. {
  57.   int i = 0;
  58.   while (i<1000)
  59.   {
  60.     digitalWrite(pinA1,HIGH);
  61.     digitalWrite(pinA2,LOW);
  62.     delay(10000);//10s
  63.     digitalWrite(pinA1,LOW);
  64.     digitalWrite(pinA2,LOW);
  65.     delay(30000);//30s
  66.     if(digitalRead(ST_Switch)==HIGH)
  67.         {
  68.           digitalWrite(pinA1,LOW);
  69.           digitalWrite(pinA2,LOW);;
  70.           break;
  71.           STlcd();  
  72.       }
  73.         
  74.   }
  75. }

  76. void D76()
  77. {
  78.   int i = 0;
  79.   while (i<1000)
  80.   {
  81. digitalWrite(pinA1,HIGH);
  82. digitalWrite(pinA2,LOW);
  83. delay(10000);//10s
  84. digitalWrite(pinA1,LOW);
  85. digitalWrite(pinA2,LOW);
  86. delay(50000);//50s
  87. if(digitalRead(ST_Switch)==HIGH)
  88.         {
  89.           digitalWrite(pinA1,LOW);
  90.           digitalWrite(pinA2,LOW);
  91.           break;
  92.           STlcd();  
  93.         }
  94.   }
  95. }

  96. void Jtime()    //本来希望用这个计时,结果发现无法这样循环。。。
  97. {
  98. for(int k=0;k<60;k++)
  99. {
  100. delay(1000);
  101. lcd.setCursor(5,1);
  102. lcd.print(k);
  103. }
  104. }

  105. void STlcd()
  106. {
  107. lcd.clear();
  108.           lcd.print("Ok...Stop!");
  109.           delay(3000);
  110.           lcd.clear();
  111.           lcd.print("Select Button ");
  112.           lcd.setCursor(0,1);
  113.           lcd.print("B:C41 G:D76 ");
  114. }
复制代码

回复

使用道具 举报

发表于 2014-6-10 20:53:23 | 显示全部楼层
用定时器,不过你的代码要全部重写了
回复 支持 反对

使用道具 举报

发表于 2014-6-11 04:37:00 | 显示全部楼层
本帖最后由 hsr18299 于 2014-6-11 05:13 编辑

幾點建議
1.        一般boolean比較習慣用true及false
2.        如果沒有要控制電機正反轉,直接把電機pinA2接地,用pinA1控制就好,這樣程序跟接線看起來比較清爽
3.        如果要用外部中斷,只能接在第2或3腳
4.        如果delay(10)是為了消除按鈕開關的彈跳效應的話,直接並連一個電容就好了,下拉電阻還是要的

int pinA1 = 4;
int C41_Switch = 7;       //右边按键引脚编号
int D76_Switch = 8;      //左边按键引脚编号
int c41state = 0;
int d76state = 0;
//ST_Switch = 2; 停止按键引脚编号

void setup() {
pinMode(pinA1,OUTPUT);
pinMode(C41_Switch,INPUT);
pinMode(D76_Switch,INPUT);
attachInterrupt(0,st,RISING);  //RISING是上升觸發,依照你的設計去更改模式
}

void st() {    //中斷函數
digitalWrite(pinA1,LOW);
}

void c41() {   //c41運行模式函數
digitalWrite(pinA1,HIGH);
delay(10000);
digitalWrite(pinA1,LOW);
delay(30000);
}

void d76() {    //d76運行模式函數
digitalWrite(pinA1,HIGH);
delay(10000);
digitalWrite(pinA1,LOW);
delay(50000);
}

void loop() {
c41state = digitalRead(C41_Switch);
if (c41state == HIGH) {c41();}
d76state = digitalRead(D76_Switch);
if (d76state == HIGH) {d76();}
}

先試試 LCD就自己加進去吧!!
不瞭解你計時器的意義,所以沒加進去
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-6-12 23:20:39 | 显示全部楼层
hsr18299 发表于 2014-6-11 04:37
幾點建議
1.        一般boolean比較習慣用true及false
2.        如果沒有要控制電機正反轉,直接把電機pinA2接地,用pin ...

十分感谢,计时器,我想的是按下按钮就开始执行计时,计算执行的时间。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 08:59 , Processed in 0.038191 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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