Question
Please adjust the following C++ code such that time is displayed with AM or PM (12 hour cycles), and the button toggles the LCD's backlight
Please adjust the following C++ code such that time is displayed with AM or PM (12 hour cycles), and the button toggles the LCD's backlight on and off. Useful information from the Time library includes: and from the Liquid Crystal library includes:
hourFormat12(); // The hour now in 12 hour format lcd.backlight(); isAM(); // Returns true if time now is AM lcd.noBacklight(); isPM(); // Returns true if time now is PM now(); // Returns the current time as seconds
#include
#define currentHour 8 #define currentMinute 29 #define currentSecond 40 #define currentYear 17 #define currentMonth 6 #define currentDay 11
#define buttonPin 3
LiquidCrystal_I2C lcd (0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int buttonVal = 0; int lastButtonState = LOW;
unsigned long btnDnTime; unsigned long btnUpTime; unsigned long debounce = 40;
bool LED_State = LOW; bool hourFormat12(); bool PM();
void setup() { pinMode(buttonPin, INPUT_PULLUP); lcd.begin(16, 2); Serial.begin(115200); setTime(currentHour, currentMinute, currentSecond, currentDay, currentMonth, currentYear); Alarm.timerRepeat(1, displayTime); }
void loop() { buttonVal = digitalRead(buttonPin); if (buttonVal == LOW && lastButtonState == HIGH && (millis() - btnUpTime) > debounce) { btnDnTime = millis(); Serial.printf("Button just pressed %lu -- ", btnDnTime); } if (buttonVal == HIGH && lastButtonState == LOW && (millis() - btnDnTime) > debounce) { toggleBacklight(); btnUpTime = millis(); Serial.printf("Button just released %lu -- ", btnDnTime); } lastButtonState = buttonVal; Alarm.delay(5); }
void displayTime() { Serial.printf("%02d/%02d/%02d ", month(), day(), year()); Serial.printf("%02d:%02d:%02d ", hour(), minute(), second()); lcd.setCursor(3,0); lcd.printf("%02d:%02d:%02d ", hour(), minute(), second()); lcd.setCursor(2,1); lcd.printf("%02d/%02d/%02d ", month(), day(), year()); }
void toggleBacklight() { if (LED_State == true) { digitalWrite(buttonPin , LOW); LED_State = false; } else { digitalWrite(buttonPin , HIGH); LED_State = true; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started