|
这是五分钟倒计时的代码,用了millis()函数来计时,显示在1602上; - #include<Wire.h>
- #include<LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27,16,2);
- unsigned long i,j,v;
- int t=59,x=4,k,l;
- void setup()
- { lcd.init();
- i=millis();
- lcd.setCursor(0,0);
- lcd.print("www.geeetech.com");
- lcd.setCursor(0,1);
- lcd.print("05:00");
- lcd.backlight();
- }
- void loop()
- {
- j=millis();
- v=(j-i)%1000;
- if(v==0)
- { k=t/10;
- l=t%10;
- lcd.setCursor(3,1);
- lcd.print(k);
- lcd.setCursor(4,1);
- lcd.print(l);
-
- if(t==59)
- {
- lcd.setCursor(1,1);
- lcd.print(x);
- x--;
- if(x<0) x=4;
- }
- t--;
- if(t<0) t=59;
- }
- }
复制代码
这是在上面代码的基础上添加了中断函数,来检测外接按钮的按键次数,每按一次,score加5;在1602上显示出来;但是加了中断函数之后,那个计时就完全不准了,求解 - #include<Wire.h>
- #include<LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27,16,2);
- unsigned long i,j,v;
- int t=59,x=4,k,l;
- int score=0;
- int button=2;//设置按钮的数字IO引脚也是中断的接口
- void setup()
- {
- i=millis();
- pinMode(button,INPUT);
- attachInterrupt(0,count,FALLING);
- lcd.init();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("www.geeetech.com");
- lcd.setCursor(0,1);
- lcd.print("05:00");
- lcd.print("score:");
- lcd.setCursor(11,1);
- lcd.print("00X10");
- lcd.backlight();
- }
- void loop()
- {
- j=millis();
- v=(j-i)%1000;
- if(v==0)
- { k=t/10;
- l=t%10;
- lcd.setCursor(3,1);
- lcd.print(k);
- lcd.setCursor(4,1);
- lcd.print(l);
- if(t==59)
- {
- lcd.setCursor(1,1);
- lcd.print(x);
- x--;
- if(x<0) x=4;
- }
- t--;
- if(t<0) t=59;
- }
- lcd.setCursor(11,1);
- lcd.print(score/10);
- lcd.setCursor(12,1);
- lcd.print(score%10);
- void count()
- {
- if(digitalRead(button)==LOW)
- {
- delay(1);
- if(digitalRead(button)==LOW)
- {score+=5;}
- }
- }
- }
复制代码 |
|