The edit option is the pencil icon at the bottom of your post. To change the code portion of your post select the text and delete it. Then press the ‘</>’ icon in the top bar of the edit window to create a small sample of formatted text, and paste the code over that text.
The problem with code in a message (in addition to the readability issue) is that single and double quotes get changed to opening and closing quotes (which the Arduino IDE does not understand) and odd characters like non-breaking spaces can get added.
The code below implements most of the functions you mention. There is no code to alter the LCD brightness because that will depend on the model of LCD you are using.
The formatting of date and time is somewhat cryptic, but the simplification is justified and making further changes is much simpler. Strings are used to prepare the display rather than updating the LCD piecemeal - space and speed considerations are not significant in this usage. Also, preparing the display text as strings makes debugging with the console particularly easy, and it’s necessary for the scrolling function. The temperature code has been confined to two parts - initialization and getting the value - because your device might be different. Similarly for the ambient light detector, although that code is much simpler. My RTC is different than yours, but this only affects the object instantiation. I have extended the text for month and day to make better use of the scrolling function, and optimised the calculation of the month name.
// Real TIme Clock with LCD Display.
// RTC I2C at 0x68
// EEProm I2C at 0x50
// LCD I2C at 0x27
// DS18B20 at Pin 2
// Photoresistor at pin A0
#include <Wire.h> // for I2C communication
#include <LiquidCrystal_I2C.h> // for LCD
#include <RTClib.h> // for RTC
// DS18B20 single-wire temperature sensor library and object.
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin);
//
LiquidCrystal_I2C lcd(0x27, 16, 2); // create LCD with I2C address 0x27, 16 characters per line, 2 lines
//RTC_DS3231 rtc; // create rtc for the DS3231 RTC module, address is fixed at 0x68
RTC_DS1307 rtc; // create rtc for the DS31307 RTC module, address is fixed at 0x68
/*
function to update RTC time using user input
*/
void updateRTC()
{
lcd.clear(); // clear LCD display
lcd.setCursor(0, 0);
lcd.print("Edit Mode…");
// ask user to enter new date and time
const char txt[6][15] = { "year [4 - digit]", "month [1~12]", "day [1~31]",
"hours [0~23]", "minutes [0~59]", "seconds [0~59]"
};
String str = "";
long newDate[6];
while (Serial.available()) {
Serial.read(); // clear serial buffer
}
for (int i = 0; i < 6; i++) {
Serial.print("Enter ");
Serial.print(txt[i]);
Serial.print(": ");
while (!Serial.available()) {
; // wait for user input
}
str = Serial.readString(); // read user input
newDate[i] = str.toInt(); // convert user input to number and save to array
Serial.println(newDate[i]); // show user input
}
// update RTC
rtc.adjust(DateTime(newDate[0], newDate[1], newDate[2], newDate[3], newDate[4], newDate[5]));
Serial.println("RTC Updated!");
}
//*************
// Get temperature from DS18B20.
// Replace as required for other sensors.
float getTemp() {
//returns the temperature from one DS18b20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's complement
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
//*********************
/*
function to update LCD text
*/
void updateLCD()
{
/*
create array to convert digit days to words:
0 = Sunday | 4 = Thursday
1 = Monday | 5 = Friday
2 = Tuesday | 6 = Saturday
3 = Wednesday |
*/
const char dayInWords[7][6] = {"SUN", "MON", "TUES", "WEDNES", "THURS", "FRI", "SATUR"};
/*
create array to convert digit months to words:
1 = January | 6 = June
2 = February | 7 = July
3 = March | 8 = August
4 = April | 9 = September
5 = May | 10 = October
6 = June | 11 = November
7 = July | 12 = December
*/
const char monthInWords[12][9] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"
};
// pointer for LCD scroll start position
static int pointer = 0;
// get time and date from RTC and save in variables
DateTime rtcTime = rtc.now();
int ss = rtcTime.second();
int mm = rtcTime.minute();
int hh = rtcTime.twelveHour();
int DD = rtcTime.dayOfTheWeek();
int dd = rtcTime.day();
int MM = rtcTime.month() - 1;
int yyyy = rtcTime.year();
// Get Temperature
float tc = getTemp();
// Serial.println(tc);
// Get Light Value
int lv = analogRead(A0);
// move LCD cursor to upper-left position
lcd.setCursor(0, 0);
// Create and display time string
String msg = ((hh < 10) ? "0" : "") + String(hh) + ":"
+ ((mm < 10) ? "0" : "") + String(mm) + ":"
+ ((ss < 10) ? "0" : "") + String(ss) + " "
+ (rtcTime.isPM() ? "PM" : "AM")
;
//Serial.println(msg);
lcd.print(msg);
// move LCD cursor to lower-left position
lcd.setCursor(0, 1);
// Create and display date string
msg = String(dayInWords[DD]) + "DAY" + " "
+ String(dd) + " "
+ String(monthInWords[MM]) + " "
+ String(yyyy) + " "
+ String(tc) + char(223) + "C "
+ "Light = " + ((lv < 800) ? "Lo" : "Hi") + " "
;
// Serial.println(msg);
unsigned int msgLength = msg.length();
// lcd scrolling - allow for overflow
msg += msg.substring(0, 16);
lcd.print(msg.substring(pointer, pointer + 16));
pointer = (pointer + 1) % msgLength;
}
void setup()
{
Serial.begin(9600); // initialize serial
lcd.init(); // initialize lcd
lcd.backlight(); // switch-on lcd backlight
rtc.begin(); // initialize rtc
}
void loop()
{
updateLCD(); // update LCD text
delay(200);
if (Serial.available()) {
char input = Serial.read();
if (input == 'u') updateRTC(); // update RTC time
}
}