arduino计时器程序

这是个arduino计时器程序,这个程序可以调整计时器进行倒计时,可以设置倒计时时间。

一共两个按钮,两个电位器,三个led灯。

两个按钮其中一个控制计时开始和计时结束,另一个按钮循环移位时分秒设置,按下之后可以设置当前的计时,时分秒依次设置。

两个电位器,一个调节1602屏幕亮度,另一个设置计时器时间。

3个led灯,一个2秒闪一次,一个一分钟闪两次,一个一小时闪两次。

代码:

//include file
#include 

//define a pin to control beep
#define buzzer_pin 6

//define a pin to detect key signal
//when you press the key which controled by key1,the timer start work
#define key1 7
//when you press the key which controled by key1,you could set the timer
#define key2 13

//define the pin to control led
//second
#define led_s 8
//minite
#define led_m 9
//hour
#define led_h 10
//led HIHG time
#define lit 100



//define a lcd object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//the int to save timer goal
//second goal
unsigned int seco_g=30;
//minite goal
unsigned int mini_g=1;
//hour goal
unsigned int hour_g=0;

unsigned long seco_sum=0;

//define the now time
//second
unsigned int seco=0;
//minite
unsigned int mini=0;
//hour
unsigned int hour=0;

unsigned long seco_sum_pre=0;


unsigned long seco_pre=0;
unsigned long mini_pre=0;
unsigned long hour_pre=0;

bool fi_s=false;
//set bit
//0 for second 1 for minite 2 for hour
unsigned int swit=0;
//set timer mode,0,1
bool swit_s=false;

//the timer status
bool js_status=false;


//detect key1
bool anjian1(){
  if(digitalRead(key1)==HIGH)
  {
    delay(10);
    if(digitalRead(key1)==HIGH)
    return true;
    else
    return false;
  }
  return false;
};

//detect key2
bool anjian2(){
  if(digitalRead(key2)==HIGH)
  {
    delay(10);
    if(digitalRead(key2)==HIGH)
    return true;
    else
    return false;
  }
  return false;
};

void display_t()
{
    hour=seco_sum/3600;
    mini=(seco_sum-hour*3600)/60;
    seco=seco_sum-hour*3600-mini*60;
  
  lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Arduino Timer");
        lcd.setCursor(0, 1);
        lcd.print(hour);
        lcd.setCursor(2, 1);
        lcd.print(":");
        lcd.setCursor(3, 1);
        lcd.print(mini);
        lcd.setCursor(5, 1);
        lcd.print(":");
        lcd.setCursor(6, 1);
        lcd.print(seco);
}
void setup()
{
  //初始化引脚
  for(int i=2;i<=6;i++)
  {
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  pinMode(7,INPUT);
  for(int i=8;i<=12;i++)
  {
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  pinMode(13,INPUT);
  //初始化串口
  Serial.begin(115200);
  
  lcd.begin(16, 2); // 启动与LCD键盘的通信
  lcd.setCursor(0,0);
  lcd.print("Arduino-");
  lcd.setCursor(0, 1);
  lcd.print(" Timer");
  delay(3000);
  Serial.println("Init suss!");
  seco_sum=hour*3600+mini*60+seco;
  display_t();
}

void loop(){
  if((anjian1()==true)&&(js_status==false))
  {
    seco=seco_g;
    mini=mini_g;
    hour=hour_g;
    js_status=true;
    seco_sum_pre=millis();
    seco_pre=millis();
    mini_pre=millis();
    hour_pre=millis();
  }
  else if((anjian1()==true)&&(js_status==true))
    js_status=false;
  
  //if the timer not work ,you can set the timer'goal
  if(js_status==false)
  {
    if(anjian2()==true)
    {
      if((swit_s==false)||(swit==2))
        swit=0;
      else
        swit++;
      swit_s=true;
    }
    if(swit_s==true)
    {
                  //set the timer's goal
                  if(swit==0)
                  {
                    if(hour_g!=map(analogRead(A0),0,1023,0,24))
                    lcd.clear();
                    hour_g=map(analogRead(A0),0,1023,0,24);
                    
                  }
                  else if(swit==1)
                  {
                    if(mini_g!=map(analogRead(A0),0,1023,0,59))
                    lcd.clear();
                    mini_g=map(analogRead(A0),0,1023,0,59);
                  }
                  else if(swit==2)
                  {
                    if(seco_g!=map(analogRead(A0),0,1023,0,59))
                    lcd.clear();
                    seco_g=map(analogRead(A0),0,1023,0,59);
                  }
                  //lcd.clear();
                    //set mode
                  lcd.setCursor(0,0);
                  lcd.print("Set mode-");
                  lcd.setCursor(0, 1);
                  lcd.print(hour_g);
                  lcd.setCursor(2, 1);
                  lcd.print(":");
                  lcd.setCursor(3, 1);
                  lcd.print(mini_g);
                  lcd.setCursor(5, 1);
                  lcd.print(":");
                  lcd.setCursor(6, 1);
                  lcd.print(seco_g);
    }
  }

  if(js_status==true)
  {
      seco_sum=hour*3600+mini*60+seco;
      if(seco_sum>0)
      {
        if((millis()-seco_sum_pre)>=1000)
        {
          seco_sum--;
          display_t();
          seco_sum_pre=millis();
        }
        if((millis()-seco_pre)>=2000)
        {
          seco_pre=millis();
          digitalWrite(led_s,HIGH);
          delay(100);
          digitalWrite(led_s,LOW);
        }
        if((millis()-mini_pre)>=60000)
        {
          mini_pre=millis();
          digitalWrite(led_m,HIGH);
          delay(100);
          digitalWrite(led_m,LOW);
          delay(100);
          digitalWrite(led_m,HIGH);
          delay(100);
          digitalWrite(led_m,LOW);
        }
        if((millis()-hour_pre)>=3600000)
        {
          hour_pre=millis();
          digitalWrite(led_h,HIGH);
          delay(100);
          digitalWrite(led_h,LOW);
          delay(100);
          digitalWrite(led_h,HIGH);
          delay(100);
          digitalWrite(led_h,LOW);
        }
      }
      else
      {
        if(fi_s==false)
        {
          lcd.clear();
          fi_s=true;
        }
        else;
        lcd.setCursor(0,0);
        digitalWrite(buzzer_pin,HIGH);
        lcd.print("Timer finished!");
      }
      
  }
}

 

你可能感兴趣的:(arduino,arduino计时器,arduino,计时器)