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

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.
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++)
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.
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).
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 R code >> Set seed number as "12345" every time you generate random numbers....
<< Using R code >> Set seed number as "12345" every time you generate random numbers. For each answer, use # to explain if necessary. 2) Generate a data.frame "D" with 3 variables. The 1st variable "v1" has 50 number of N(5,3^2) (normal with mean 5, standard deviation 3) The 2nd variable "v2" has 50 number of exp(5) (exponential with parameter 5) The 3rd variable "v3" has 50 random characters from lower case alphabets. 2-1) Rename the variable from "v1",...
arduino programing Question 1: write an APL program that will print the following output on the...
arduino programing Question 1: write an APL program that will print the following output on the serial monitor 10 times only.   Output:  1 2 3 4 5 Question 2: write an APL program that will turn the traffic lights on and off by taking input from the Serial port working at 11500 bits processing rate. Use r or R for RED LIGHTS, y or Y for YELLOW LIGHTS and g or G for GREEN LIGHTS.    Question 3: write an APL...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot...
Write code in C language in the Arduino IDE ADC data using Serial plotter Serial plot : raw data, delay data (int) Purpose: Delay Block (**Using Class**) Input : u(t) Output : o(t)=u(t-h) sample time=0.02 Delay (h) = 0.4
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT