Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By Electroguard
#66804 Real Time Clock adjuster - offers easy menued time and date entry.
I'm using a DS3231, but it should work for other I2C RTCs

Image
This is something I've been wanting to do for a while to give independence against inevitable internet melt-downs, and for stand-alone portable and vehicle type projects.

Unfortunately it is now big enough to be affected by the Esp-Basic quantum weirdness complexity bug, so perhaps just use it as a demonstrator for cropping out your own minimal functionality - things like month names and day names and colours and styles are nice but non-essential, so can be pruned to give Esp-Basic a bit more wiggle room for avoiding its bug problems.

Like schrodinger's cat there's no way of knowing the outcome without trying.
If you've done 2 successful consecutive clean SAVES since the last device reboot then the probability is that the program will RUN ok.
If it doesn't, don't be tempted into chasing ghosts if the seconds don't update (note: in red edit mode the details are frozen for editing anyway), or the screen only partially displays, simply browser BACK then re-RUN and it may well then work ok... there seems no rhyme or reason to the quantum weirdness. I've found that the best chances of keeping the bug at bay is to reboot a device with 1Mb build (even if actually a 4Mb device), paste in a clean script, do a SAVE, then immediately do another SAVE, then RUN.

A possibility in the future might be for a main application script to load a dedicated RTCadjust script for adjusting the date and time parameters, which then reloads the original application script again afterwards. Both scripts could do a memclear for max resources, the application script could save and re-load any important vars from flash and/or RTC eeprom if wished.

Code: Select allmemclear
' RTC Adjuster by Electroguard 6th June 2017 using V3 A69
i2c.setup(13,12) 'using SDA=13, SCL=12
RTCaddress = 104
registers = 7  'seconds, minutes, hours, day, date, month, year
dim days$(8) 'Cant be arsed to mess about with zero
let days$(1) = "Sunday"
let days$(2) = "Monday"
let days$(3) = "Tuesday"
let days$(4) = "Wednesday"
let days$(5) = "Thursday"
let days$(6) = "Friday"
let days$(7) = "Saturday"
dim months$(13) 'Cant be arsed to mess about with zero
let months$(1) = "January"
let months$(2) = "February"
let months$(3) = "March"
let months$(4) = "April"
let months$(5) = "May"
let months$(6) = "June"
let months$(7) = "July"
let months$(8) = "August"
let months$(9) = "September"
let months$(10) = "October"
let months$(11) = "November"
let months$(12) = "December"
stop = "n"  'used to prevent continually updating from RTC registers when editing
val = 0 'Global variable used by TrackerJs BCD routines
ledpin = 5 'Optional visible tick-tock led
td = "RTC Calender Clock" 'Time and Date string
cssmain = "width:90%;margin:auto;border-width:0px;font-size:30px;" 'style default
cssblue = "color:blue;"
cssred = "color:red;"
timer 1000, [TICK]
goto [HOME]

[HOME]
cls
textbox td
cssid htmlid(), cssmain & cssblue
html "<BR><BR>"
button "Edit", [SETTIME]
wait

[TICK]
if io(laststat,ledpin) = 1 then io(po,ledpin,0) else io(po,ledpin,1)
i2c.begin(RTCaddress) 'start another transaction
delay 5
i2c.write(0) 'point to the seconds address location
i2c.end() ' Finish the write transaction
i2c.requestfrom(RTCaddress,registers)
if stop = "n" then
for count = 1 to registers 'read all the RTC registers
 val = i2c.read()
 delay 5
 gosub [DEC] 'convert each registers BCD value into decimal (courtesy of TrackerJ's BCD routines)
 if count = 1 then dsecs = val  'prefixed with d to denote the decimal equivalents of the RTC bcd value
 if count = 2 then dmins = val
 if count = 3 then dhrs = val
 if count = 4 then dday = val
 if count = 5 then ddate = val
 if count = 6 then dmonth = val
 if count = 7 then dyear = val
next count   
i2c.end()
td = days$(dday) & " " & ddate & " " & months$(dmonth) & " 20" & dyear & " " & dhrs & ":" & dmins & ":" & dsecs 'Time and date string
endif
wait

[SETTIME]
cls
stop = "y"
textbox td
cssid htmlid(), cssmain & cssred
html "<BR><BR>"
button "<", [<Secs]
button ">", [>Secs]
textbox dsecs
html "seconds<BR>"
button "<", [<Mins]
button ">", [>Mins]
textbox dmins
html "minutes<BR>"
button "<", [<Hours]
button ">", [>Hours]
textbox dhrs
html "hours<BR>"
button "<", [<Day]
button ">", [>Day]
textbox dday
html "day<BR>"
button "<", [<Date]
button ">", [>Date]
textbox ddate
html "date<BR>"
button "<", [<Month]
button ">", [>Month]
textbox dmonth
html "month<BR>"
button "<", [<Year]
button ">", [>Year]
textbox dyear
html "year<BR>"
html "<BR><BR>"
button "Save", [SAVE]
button "Abort", [ABORT]
wait

[ABORT]
stop = "n"
goto [HOME]
wait

[SAVE]
i2c.begin(RTCaddress) 'start another transaction
delay 5
i2c.write(0) 'point to the first (seconds) address location
val = dsecs
gosub [BCD]
i2c.write(val) 'writes the seconds data
val = dmins
gosub [BCD]
i2c.write(val) 'writes the minutes data
val = dhrs
gosub [BCD]
i2c.write(val) 'writes the hours data
val = dday
gosub [BCD]
i2c.write(val) 'writes the day data
val = ddate
gosub [BCD]
i2c.write(val) 'writes the date data
val = dmonth
gosub [BCD]
i2c.write(val) 'writes the month data
val = dyear
gosub [BCD]
i2c.write(val) 'writes the year data
i2c.end() ' Finish the write transaction
stop = "n"
goto [HOME]
wait

[<Secs]
dsecs = dsecs - 1
if dsecs < 0 then dsecs = 59
wait

[>Secs]
dsecs = dsecs + 1
if dsecs > 59 then dsecs = 0
wait

[<Mins]
dmins = dmins - 1
if dmins < 0 then dmins = 59
wait

[>Mins]
dmins = dmins + 1
if dmins > 59 then dmins = 0
wait

[<Hours]
dhrs = dhrs - 1
if dhrs < 0 then dhrs = 23
wait

[>Hours]
dhrs = dhrs + 1
if dhrs > 23 then dhrs = 0
wait

[<Day]
dday = dday - 1
if dday < 1 then dday = 7
wait

[>Day]
dday = dday + 1
if dday > 7 then dday = 1
wait

[<Date]
ddate = ddate - 1
if ddate < 1 then ddate = 31
wait

[>Date]
ddate = ddate + 1
if ddate > 31 then ddate = 1
wait

[<Month]
dmonth = dmonth - 1
if dmonth < 1 then dmonth = 12
wait

[>Month]
dmonth = dmonth + 1
if dmonth > 12 then dmonth = 1
wait

[<Year]
dyear = dyear - 1
if dyear < 0 then dyear = 99
wait

[>Year]
dyear = dyear + 1
if dyear > 99 then dyear = 0
wait

[DEC] 'Thanks to TrackerJ
hv = val >> 4
hl = val and 15
val = hv*10 + hl
return 

[BCD] 'Thanks to TrackerJ
d = int( val / 10 )
d1 = d * 10
d2 = val - d1
val = d*16 + d2
return