Using C with Arduino UNO, 16×2 LCD and 4×3 keypad to make a leap year calculator

A video to demo the project followed by some explanation and the source code.

Leap year calculator

The LCD will prompt the user to input the Year followed by # or * key (# and * act as ‘Enter’ key, user can press any one of them).

  • Any input smaller than 1500 will raise an exception (“Must be > 1500”) and Arduino will re-prompt the user with “Year then # or *”.
  • Any input greater than 9999 will raise an exception (“Max.year is 9999”) and Arduino will re-prompt the user with “Year then # or *”.
  • Any input between1500 and 9999 (both end inclusive) eg. 2020 or 2003, will return the user with either “2020 = leap year” or “Year 2003 is not leap year” .

Now all that is left to do is to make an enclosure. 😉

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


//for Keypad
//byte is unsigned int from 0 to 255, save memory space
const byte rows = 4;
const byte cols = 3;

char keysArr[rows][cols] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'},
  };

byte mcuPinsRow[rows] = {6,5,4,3};
byte mcuPinsCol[cols] = {2,1,0};

//for LCD
const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13 ;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 

Keypad myKeypad = Keypad(makeKeymap(keysArr),mcuPinsRow, mcuPinsCol, rows, cols) ;

//Array to store char 
char numArr [5] ; 

byte count = 0;


void setup() {
  // put your setup code here, to run once:

  lcd.begin(16,2) ; 
  lcd.setCursor (0,0) ;
  lcd.print("Year then # or *") ; 
  lcd.setCursor (0,1) ;
}



void loop() {
  // put your main code here, to run repeatedly:

  char keyPressed = myKeypad.getKey();

  if ((keyPressed)&&(keyPressed != '#') && (keyPressed != '*')&& (count < 4)){
    lcd.print(keyPressed) ;
    numArr[count] = keyPressed ;
    count++ ;
  }

  else if((keyPressed)&&(keyPressed != '#') && (keyPressed != '*')&& (count == 4)){
    lcd.clear();
    lcd.print("Max.year is 9999");
    delay(2000);
    lcd.setCursor (0,0) ;
    lcd.print("Year then # or *") ; 
    lcd.setCursor (0,1) ;
    numArr[0] = '\0';
    count = 0;
 }

 else if((keyPressed)&&(keyPressed == '#'|| keyPressed == '*') && (count < 5)){
    lcd.clear();
    String stringYear(numArr);
    int intYear;
    sscanf(numArr, "%d", &intYear);
      if (intYear <1500){
        lcd.clear();
        lcd.print("Must be > 1500");
        delay(2000);
        lcd.setCursor (0,0) ;
        lcd.print("Year then # or *") ; 
        lcd.setCursor (0,1) ;
        numArr[0] = '\0';
        count = 0;
        
        }
      else if ((intYear % 4 == 0 && intYear % 100 != 0) || (intYear % 4 == 0 && intYear % 100 == 0 && intYear % 400 == 0)){
        String fullString = stringYear + " = leap year" ;
        lcd.print(fullString);
        }
       else {
        String fullString = "Year " + stringYear ;
        lcd.print(fullString);
        lcd.setCursor(0,1);
        lcd.print("is not leap year");
        
        }
     
    delay(3000);
    lcd.clear();
    lcd.print("Year then # or *") ; 
    lcd.setCursor (0,1) ;
    //numArr[0] = '\0';
    numArr[0] = 0;
    numArr[1] = 0;
    numArr[2] = 0;
    numArr[3] = 0;
    count = 0;
 }
 

}

The circuit diagram as per below: