2020-11-29

#include <LiquidCrystal.h>
#include <Keypad.h>

long int set1;
long int set2;
long int set3;
long int set4;
long int j;
int t1, t2, t3, t4, t5, t6;
int r1, r2, r3;
char key;
String r[8];
String hours;
String minutes;
String seconds;

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = { 6, 7, 8, 9 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins
byte colPins[COLS] = { 10, 11, 12, 13 };// Connect keypad COL0, COL1 and COL2 to t

LiquidCrystal lcd(A0, A1, 5, 4, 3, 2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setFeedingTime()
{
bool feed = true;
int i=0;

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Set feeding Time”);
lcd.clear();
lcd.print(“HH:MM:SS”);
lcd.setCursor(0,1);

while(1){
key = kpd.getKey();
char j;

if(key!=NO_KEY){
lcd.setCursor(j,1);
lcd.print(key);
r[i] = key-48;
i++;
j++;

if (j==2 || j == 5)
{
  lcd.print(":"); j++;
}
delay(500);

}

if (key == ‘D’)
{key=0; break; }
}
lcd.clear();
}

void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A3, INPUT);
pinMode(A4, OUTPUT);
}

void loop()
{
lcd.setCursor(0,0);
lcd.print(“Arduino TImer”);
if(digitalRead((A3) == 1))
{
lcd.clear();
setFeedingTime();
for(int i = 0;i < 6;i++)
{
Serial.print(r[i]);
Serial.println();
}
hours = String(r[0]) +String(r[1]);
minutes = String(r[2] + r[3]);
seconds = String(r[4] + r[5]);

set1 = (hours.toInt()*3600);
set2 = (minutes.toInt()*60);
set3 = seconds.toInt();
set4 = (hours.toInt() * 3600) + (minutes.toInt() * 60) + seconds.toInt();
Serial.print("set4");
Serial.print(set4);
Serial.println();

lcd.setCursor(0,0);
lcd.print("Countdoem begins");
delay(1000);
lcd.clear();

for(long int j = set4;j >= 0;j--)
{
  Serial.println(j);

  lcd.setCursor(0,0);
  lcd.print("HH:MM:SS");

  long int HH = j / 3600;
  lcd .setCursor(0,1);
  Serial.println(HH);
  if(HH < 10)
  {
    lcd.print("0");
  }
  lcd.print(HH);
  lcd.print(":");

  long int MM = (j - (HH*3600))/60;
  lcd.setCursor(3,1);
  Serial.println(MM);
  if(MM < 10)
  {
    lcd.print('0');
  }
  lcd.print(MM);
  lcd.print(":");

  long int SS = j - ((HH*3600) + (MM*60));
  lcd.setCursor(6,1);
  Serial.println(SS);
  if(SS < 10)
  {
    lcd.print("0");
  }
  lcd.print(SS);
  delay(1000);

  if(digitalRead(A3) == 1)
  {
    break;
  }

  if(j == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("TImer Stop");
    lcd.setCursor(2,1);
    lcd.print("Ring-Ring");

    for(int k = 0;k <= 100; k++)
    {
      digitalWrite(A4,HIGH);
      delay(300);
      digitalWrite(A4.LOW);
      delay(300);

      if(digitalRead(A3) == 1)
      {
        break;
      }
     }
  }
}

}
}

上一篇:【常用传感器】LCD1602液晶工作原理详解及例程代码


下一篇:(4)蓝桥杯嵌入式——LCD