Post about your Basic project here

Moderator: Mmiscool

User avatar
By Mmiscool
#54027 I modified your post to make the video show up. You need to remove all of the url and just leave the video ID.
User avatar
By russm
#54072 Thanks for fixing the post, now I know how to make it work.

Original code worked fine but when I added proportional minutes to the hour it screwed up. Had to go to a meter face of 0-12 instead of 1-12. Clock currently shows 12:42. I'll eventually modify the face to have a 12 where the 0 is as well.
Image
(note the permanent perf board sitting in front, waiting for trim pots before assembly)

I found that adding the minute proportion to the hour is all that is needed, you can't really see the seconds added to minute, plus technically that requires rescaling as well. Also, thanks Electroguard for the IP string manipulation for the broadcast address much more elegant than my original.

One last note, DST must be handled locally, not sure if the timesetup() function is supposed to do that or not since there is a DST setting. I may add a button on the clock to select DST by the user, not going to try to calculate in code, who knows, maybe we'll just stick with DST all year long!

Here's the modified code.

Code: Select all'******************************************************
'* Analog Panel Meter Clock                           *
'*   by Russ McIntire, 8/27/16                        *
'*   D0 = Hours                                       *
'*   D1 = Minutes                                     *
'*   D2 = Seconds                                     *
'*                                                    *
'* To do: HTML setup screen to select DST, TZ, SSID   *
'******************************************************

timesetup(-4,1)  'Need to figure out what is wrong with DST
delay 5000

udpbegin 5001


hour = 0
min = 0
sec = 0
M = " "
udptimer = 0
hourpwm = 0
minpwm = 0
secpwm = 0

'Determine broadcast address
let localIP = ip()
pos = instrrev(localIP,".")
netIP = left(localIP,pos)
nodeIP = mid(localIP,pos+1)
let broadcastaddr = netIP & "255"


timer 1000, [updatetime]
wait
end

[updatetime]
'Get time values from NTP server
 hour = val(time("hour"))
 min = val(time("min"))
 sec = val(time("sec"))


'Set 12 hour time and determine AM/PM
 if hour > 12 then
  hour = hour - 12
  M = "PM"
 else
  if hour = 0 then
   hour = 12
  endif
  M = "AM"
 endif

'Panel PWM calculation
'hourpwm = 85.33 * (hour)                 'divide 1024/12
hourpwm = (85.33 * hour) + (1.55 * min)   'divide 1024/12 add proportion of minutes (~1.5/min)
minpwm = 17.36 * min                      'divide 1024/59
'minpwm = (17.36 * min) + (.29 * sec)     '- don't recommend - divide 1024/59 add proportion of seconds (.29/sec)
secpwm = 17.36 * sec                      'divide 1024/59

'Panel output
io(pwo,d0,hourpwm)
io(pwo,d1,minpwm)
io(pwo,d2,secpwm)

'Daily NTP refresh at 2:00 AM
if ((hour=2) and (min=0) and (sec>0) and (sec<5) and (M="AM")) then
 timesetup(-4,1)  'Need to figure out what is wrong with DST
 delay 5000
endif

'Send UDP broadcast message out for current time every 30 seconds
 udptimer = udptimer + 1
 if udptimer > 30 then
  ct = str(hour)&":"&time("min:sec")&" "&M
  udpwrite broadcastaddr, 5001, ct
  udptimer = 0
 endif
wait
User avatar
By russm
#54080 Yet another update. I added DST and Timezone entry via web interface. Most of that came from the aquarium light timer project. Not sure if I'll add a screen to set the wifi settings or not since they are in the main settings level anyway. :) Added user setting for the UDP broadcast port.

Code: Select all'*******************************************************
'* Analog Panel Meter Clock                            *
'*   by Russ McIntire, 8/29/16                         *
'*   D0 = Hours                                        *
'*   D1 = Minutes                                      *
'*   D2 = Seconds                                      *
'*                                                     *
'* Shows hours/minutes/seconds on 3 panel displays     *
'* Allows Timezone and DST settings from webpage       *
'* Requires Internet connection for NTP time retrieval *
'* Sends UDP broadcast of current time via UDP         *
'* Can update UDP Port in settings, default is 5001    *
'*******************************************************
memclear

'Get DST and Timezone info from memory
DST = read.val(DaylightSavings)
timezone = read.val(TZ)
udpportval = read.val(UdpPort)

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

if udpportval = 0 then udpportval = 5001
udpbegin udpportval

timesetup(tz,DST)
delay 5000

hour = 0
min = 0
sec = 0
M = " "
udptimer = 0
hourpwm = 0
minpwm = 0
secpwm = 0
updated = 0
updatetimer = 0
cts = time("hour:min:sec")
uvsr = "Running..."

'Determine broadcast address
let localIP = ip()
pos = instrrev(localIP,".")
netIP = left(localIP,pos)
nodeIP = mid(localIP,pos+1)
let broadcastaddr = netIP & "255"

'GUI setup
cls
wprint |<HTML>|
wprint |<HEAD>|
'wprint |<meta http-equiv='refresh'content='5;URL=/input?'>|
wprint |<h2>Analog Clock Setup Info</h2>|
wprint |</HEAD>|
'wprint |<body bgcolor="grey">|
wprint "<br>"

wprint "The time is currently: "
wprint htmlvar(cts)  ' time("hour:min:sec")
wprint "<br>"

wprint "Timezone: "
textbox timezone
wprint "<br>"
wprint "DST:         "
textbox DST
wprint "<br>"
button "Time Info Update", [timezone_update]
wprint "<br> <br>"
wprint "UDP Port:  "
textbox udpportval
wprint "<br>"
button "UDPPort Update", [udpport_update]
wprint "<br> <br>"

wprint htmlvar(uvsr)

timer 1000, [updatetime]
wait
end

[updatetime]
'Get time values from NTP server
 hour = val(time("hour"))
 min = val(time("min"))
 sec = val(time("sec"))
 cts = time("hour:min:sec")

'Set 12 hour time and determine AM/PM
 if hour > 12 then
  hour = hour - 12
  M = "PM"
 else
  if hour = 0 then
   hour = 12
  endif
  M = "AM"
 endif

'Panel PWM calculation
'hourpwm = 85.33 * (hour)                 'divide 1024/12
hourpwm = (85.33 * hour) + (1.55 * min)   'divide 1024/12 add proportion of minutes (~1.5/min)
minpwm = 17.36 * min                      'divide 1024/59
'minpwm = (17.36 * min) + (.29 * sec)     '- don't recommend - divide 1024/59 add proportion of seconds (.29/sec)
secpwm = 17.36 * sec                      'divide 1024/59

'Panel output
io(pwo,d0,hourpwm)
io(pwo,d1,minpwm)
io(pwo,d2,secpwm)

'Daily NTP refresh at 2:00 AM
if ((hour=2) and (min=0) and (sec>0) and (sec<5) and (M="AM")) then
 timesetup(tz,DST)  'Need to figure out what is wrong with DST
 delay 5000
endif

gosub [udptimer]
wait

[timezone_update]
'Persist timezone information and UDPPort
write(DaylightSavings,str(DST))
write(TZ,str(timezone))

if DST = 1 then
 tz = timezone + 1
else
 tz = timezone
endif

timesetup(tz,DST)
delay 5000

'serialprintln "Timezone Updated"
'serialprintln "The Updated time is: "
'serialprintln time("hour:min")

cts = time("hour:min:sec")
uvsr = "<mark> Updated... </mark>"
updated = 1
returngui
wait

[udpport_update]
write(UdpPort,str(udpportval))
udpbegin udpportval
cts = time("hour:min:sec")
uvsr = "<mark> Updated... </mark>"
updated = 1
returngui
wait

[udptimer]
'Send UDP broadcast message out for current time every 30 seconds
 udptimer = udptimer + 1
 if udptimer > 30 then
  ct = str(hour)&":"&time("min:sec")&" "&M
  udpwrite broadcastaddr, 5001, ct
  udptimer = 0
 endif

'Check to see if Timezone or DST was updated
 if updated = 1 then
  updatetimer = updatetimer + 1
    if updatetimer > 6 then
        uvsr = "Running..."
        updated = 0
        updatetimer = 0
        returngui
    endif
 endif     
return