本帖最后由 tmcy 于 2014-6-10 15:17 编辑
做了一个程序,要求电机一个方向有间隔的旋转,而且有两种旋转模式,一种每隔30秒转10秒,一种每隔50秒转10秒。
而且要求LCD上显示计时,昨儿折腾了一晚上,发现几个问题没办法解决
1,只能单线程执行,如果要中断循环要等最后一个delay延时完成,不知道怎么直接中断,求解。。。
2,计时器的问题,按下按钮开始计时,本来想做一个for或者while的循环语句增量,这个增量的循环没办法和电机的循环一起执行。。彻底没招了,求解。。。
[mw_shl_code=c,true]#include <LiquidCrystal_I2C.h>
#include <Wire.h>
int pinA1 = 4;
int pinA2 = 5;
int C41_Switch = 7; //右边按键引脚编号
int D76_Switch = 8; //左边按键引脚编号
int ST_Switch = 9; //停止按键引脚编号
boolean C41onoff = LOW;
boolean D76onoff = LOW;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd .
lcd.setCursor(0,0); //newline
lcd.print("ASH Film ");// Print a message to the LCD
lcd.setCursor(5,1);
lcd.print("V0.01");
lcd.backlight();
delay(1500); //延时显示2000
lcd.clear();
lcd.print("Select Button ");
lcd.setCursor(0,1);
lcd.print("B:C41 G76 ");
pinMode(pinA1,OUTPUT);
pinMode(pinA2,OUTPUT);
pinMode(C41_Switch,INPUT);
pinMode(D76_Switch,INPUT);
pinMode(ST_Switch,INPUT);
}
void loop()
{
if(digitalRead(C41_Switch)==HIGH)
{
delay(10);
if(digitalRead(C41_Switch)==HIGH)
{
C41();
C41onoff=(!C41onoff);
delay(10);
}
}
if(digitalRead(D76_Switch)==HIGH)
{
delay(10);
if(digitalRead(D76_Switch)==HIGH)
{
D76();
D76onoff=(!D76onoff);
delay(10);
}
}
}
void C41()
{
int i = 0;
while (i<1000)
{
digitalWrite(pinA1,HIGH);
digitalWrite(pinA2,LOW);
delay(10000);//10s
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,LOW);
delay(30000);//30s
if(digitalRead(ST_Switch)==HIGH)
{
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,LOW);;
break;
STlcd();
}
}
}
void D76()
{
int i = 0;
while (i<1000)
{
digitalWrite(pinA1,HIGH);
digitalWrite(pinA2,LOW);
delay(10000);//10s
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,LOW);
delay(50000);//50s
if(digitalRead(ST_Switch)==HIGH)
{
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,LOW);
break;
STlcd();
}
}
}
void Jtime() //本来希望用这个计时,结果发现无法这样循环。。。
{
for(int k=0;k<60;k++)
{
delay(1000);
lcd.setCursor(5,1);
lcd.print(k);
}
}
void STlcd()
{
lcd.clear();
lcd.print("Ok...Stop!");
delay(3000);
lcd.clear();
lcd.print("Select Button ");
lcd.setCursor(0,1);
lcd.print("B:C41 G76 ");
}[/mw_shl_code]
|