Question

In: Electrical Engineering

Arduino code , using DS3231 library to set time/ date by keypad and print that on...

Arduino code , using DS3231 library to set time/ date by keypad and print that on lcd. i would like to make user to set time and date from the keypad .

Solutions

Expert Solution


//************libraries**************//
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>

//************************************//
LiquidCrystal_I2C lcd(0x3F,16,2); // Display I2C 16 x 2
RTC_DS1307 RTC;

//************Keys*****************//
int P1=6; // Keys SET options'
int P2=7; // Keys +
int P3=8; // Keys -

//************Variables**************//
int hours;
int mins;
int years;
int months;
int days;
int options = 0;

void setup()
{

lcd.begin();
lcd.backlight();
lcd.clear();

pinMode(P1,INPUT);
pinMode(P2,INPUT);
pinMode(P3,INPUT);

Serial.begin(9600);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the date and time at compile time
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time
// The default display shows the date and time
int options=0;
}

void loop()
{

// check if you press the SET Keys and increase the options index
if(digitalRead(P1))
{
options=options+1;
}
// in which subroutine should we go?
if (options==0)
{
DisplayDateTime(); // void DisplayDateTime

}
if (options==1)
{
DisplaySetHour();
}
if (options==2)
{
DisplaySetMinute();
}
if (options==3)
{
DisplaySetYear();
}
if (options==4)
{
DisplaySetMonth();
}
if (options==5)
{
DisplaySetDay();
}
if (options==6)
{
StoreAgg();
delay(500);
options=0;
}
delay(100);
}

void DisplayDateTime ()
{
// We show the current date and time
DateTime now = RTC.now();

lcd.setCursor(0, 1);
lcd.print("Hour:");
if (now.hour()<=9)
{
lcd.print("0");
}
lcd.print(now.hour(), DEC);
hours=now.hour();
lcd.print(":");
if (now.minute()<=9)
{
lcd.print("0");
}
lcd.print(now.minute(), DEC);
mins=now.minute();
lcd.print(":");
if (now.second()<=9)
{
lcd.print("0");
}
lcd.print(now.second(), DEC);

lcd.setCursor(0, 0);
lcd.print("Date: ");
if (now.day()<=9)
{
lcd.print("0");
}
lcd.print(now.day(), DEC);
days=now.day();
lcd.print("/");
if (now.month()<=9)
{
lcd.print("0");
}
lcd.print(now.month(), DEC);
months=now.month();
lcd.print("/");
lcd.print(now.year(), DEC);
years=now.year();
}

void DisplaySetHour()
{
// time setting
lcd.clear();
DateTime now = RTC.now();
if(digitalRead(P2)==HIGH)
{
if(hours==23)
{
hours=0;
}
else
{
hours=hours+1;
}
}
if(digitalRead(P3)==HIGH)
{
if(hours==0)
{
hours=23;
}
else
{
hours=hours-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set time:");
lcd.setCursor(0,1);
lcd.print(hours,DEC);
delay(200);
}

void DisplaySetMinute()
{
// Setting the minutes
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (mins==59)
{
mins=0;
}
else
{
mins=mins+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (mins==0)
{
mins=59;
}
else
{
mins=mins-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Minutes:");
lcd.setCursor(0,1);
lcd.print(mins,DEC);
delay(200);
}
  
void DisplaySetYear()
{
// setting the year
lcd.clear();
if(digitalRead(P2)==HIGH)
{   
years=years+1;
}
if(digitalRead(P3)==HIGH)
{
years=years-1;
}
lcd.setCursor(0,0);
lcd.print("Set Year:");
lcd.setCursor(0,1);
lcd.print(years,DEC);
delay(200);
}

void DisplaySetMonth()
{
// Setting the month
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (months==12)
{
months=1;
}
else
{
months=months+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (months==1)
{
months=12;
}
else
{
months=months-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Month:");
lcd.setCursor(0,1);
lcd.print(months,DEC);
delay(200);
}

void DisplaySetDay()
{
// Setting the day
lcd.clear();
if(digitalRead(P2)==HIGH)
{
if (days==31)
{
days=1;
}
else
{
days=days+1;
}
}
if(digitalRead(P3)==HIGH)
{
if (days==1)
{
days=31;
}
else
{
days=days-1;
}
}
lcd.setCursor(0,0);
lcd.print("Set Day:");
lcd.setCursor(0,1);
lcd.print(days,DEC);
delay(200);
}

void StoreAgg()
{
// Variable saving
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SAVING IN");
lcd.setCursor(0,1);
lcd.print("PROGRESS");
RTC.adjust(DateTime(years,months,days,hours,mins,0));
delay(200);
}


Related Solutions

Build an interactive calculator. Using an Arduino Uno, the OLED Display and matrix keypad to build...
Build an interactive calculator. Using an Arduino Uno, the OLED Display and matrix keypad to build a simple calculator application. The user presses the operands and operators using the keypad. The calculation output should be displayed on the OLED display. You may make the following simplifying assumptions. a)Assume 1 digit numbers b) Assume only two operators ‘+’ and ‘-’ Upload the breadboard diagram and the code
what is the following (ARDUINO): -Examples of a standard Arduino library -The Arduino uses a structured...
what is the following (ARDUINO): -Examples of a standard Arduino library -The Arduino uses a structured programming language-what type -Channels of A/D conversion on the Arduino Uno -The Arduino correlates temperature readings with what
Using Arduino to blink an LED in morse code. Morse code is used as one of...
Using Arduino to blink an LED in morse code. Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must...
Write code using the Arduino IDE that compiles with no errors. 2-bit adder: The code must read two digital input signals and turn on two LEDS as needed to show the sum of the inputs. e.g. 0 + 1 = 01.
Draw the schematic and code for an Arduino controlled system using a temperature sensor and fan.
Draw the schematic and code for an Arduino controlled system using a temperature sensor and fan.
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an...
Using pseudocode or C++ code, write code to print “small” if the magnitude M of an earthquake is in the range [0, 3), “medium” if M is in the range [3, 6), “large” if M is in the range [6, 9) and “epic” if M is greater than or equal to 9, where M is input by a user via the keyboard. (in c++)
write the code of 4 digit 7-segment display using arduino uno in assembly language by using...
write the code of 4 digit 7-segment display using arduino uno in assembly language by using software AVR STUDIO 5.1?
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me #include <stdlib.h> #include <DHT.h> #define DHTPIN 5         // DHT data pin connected to Arduino pin 5 #define DHTTYPE DHT11     // DHT11 (DHT Sensor Type ) DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "WiFi Name"     // "WiFi Name" #define PASS "WiFi Password"       // "Password" #define IP "184.106.153.149"// thingspeak.com ip String msg = "GET /update?key=Your API...
Using an Arduino MEGA write the code for the following problem. You have a pinball machine...
Using an Arduino MEGA write the code for the following problem. You have a pinball machine with four switches on the playboard. When a ball rolls over one of the switches that action must trigger the Arduino to add 20 points to the score being displayed on a 16x4 LCD, and actuate a relay that will trigger a bumping solenoid elsewhere on the board. The solenoid should return to home after the switch is no longer depressed by the ball....
Please attach R code: Use library(nycflights13) a. plot histogram for distance where carrier is "FL". Print...
Please attach R code: Use library(nycflights13) a. plot histogram for distance where carrier is "FL". Print graph(s). b. plot Two box plots side by side for distance where carrier are "FL" and “US”. Print graph(s). c. plot two histograms and two box plots in one graph where carrier are "FL" and “US”, equal scale for each box plot pair. Print graph(s).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT