Post about your Basic project here

Moderator: Mmiscool

User avatar
By joeygbsn
#53501 Here is some code for a alarm clock using the max7219 and a 4 digit display. I got a little lazy commenting the code but just wanted to get it posted, so let me know if you have any questions. Even if you don't have a display set up you can still run the program and watch the variables in the debugger. The alarm is set using the browser, if the alarm is triggered you can hit the flash button to add a 10 minute snooze. Here is a small video showing the alarm going off and the snooze function. This code was written using v3 alpha 41.



And here is the code:
Code: Select allmemclear
spi.setup(500000) 'clk on gpio14(d5) mosi on gpio13(d7) load on gpio15(d8)
timesetup(-5,0)

let load = 15
let ampm = 0
let stat = "AM"
let alarmhour = 12
let alarmmin = 30
let alarmampm = 0
let digit1 = "01"
let digit2 = "02"
let digit3 = "03"
let digit4 = "04"
let digit5 = "05" 'digit 5 is on no decode mode, with the clocks colon on segment A.
let n1 = "1"
let n2 = "2"
let n3 = "3"
let n4 = "4"
textbox alarmhour
wprint ":"
textbox alarmmin
dropdown stat, "AM,PM"
io(po,load,0)'set load pin low

'initialize max7219
spi.hex("090F",2)   'decode address 0x0F code b for digits 0->3.
io(po,load,1)  'pulse load to latch data
delay 50
io(po,load,0)
spi.hex("0B04",2)  'scan limit address 0x03 enable digits 0->4.
io(po,load,1)
delay 50
io(po,load,0)
spi.hex("0A02",2)  'intensity address 0x08 medium intensity.
io(po,load,1)
delay 50
io(po,load,0)
spi.hex("0C01",2)  'power on. 01 for on 00 for off.
io(po,load,1)
delay 50
io(po,load,0)
spi.hex("0F00",2)  'test off. 01 for on 00 for off.
io(po,load,1)
delay 50
io(po,load,0)

[init]
timer 1000, [main]'refresh display values once a second
wait

[main]
io(pwo,4,0)
if stat == "AM" then let alarmampm = 0 else let alarmampm = 1
spi.hex(digit5 & "0F",2) 'blanks digit 5
io(po,load,1)
delay 50
io(po,load,0)
gosub [gettime]
gosub [write7219]
if hr = alarmhour and val(minute) = alarmmin then goto [alarm]
wait

[alarm]
if ampm = 0 and alarmampm = 0 then
 for x = 1 to 30
  timer 0
  io(pwo,4,512)
  delay 100
  io(pwo,4,0)
  delay 100
  io(pwo,4,512)
  delay 100
  io(pwo,4,0)
  delay 100
  if io(pi,0) = 0 then
   let alarmmin = alarmmin + 10
   if alarmmin >= 60 then
    let alarmhour = alarmhour + 1
    let alarmmin = alarmmin - 60
   end if
   goto [init]
  end if
 next x
 let alarmmin = alarmmin + 1
end if
if ampm = 1 and alarmampm = 1 then
 for x = 1 to 30
  timer 0
  io(pwo,4,512)
  delay 100
  io(pwo,4,0)
  delay 100
  io(pwo,4,512)
  delay 100
  io(pwo,4,0)
  delay 100
  if io(pi,0) = 0 then
   let alarmmin = alarmmin + 10
   if alarmmin >= 60 then
    let alarmhour = alarmhour + 1
    let alarmmin = alarmmin - 60
   end if
   goto [init]
  end if
 next x
 let alarmmin = alarmmin + 1
end if
goto [init]

[gettime]
let hr = val(time(hour))
let minute = time(min)
'converts to twelve hour clock and sets am or pm
if hr >= 12 then
 if hr > 12 then let hr = hr - 12
 let ampm = 1
else
 let ampm = 0
endif
if hr = 0 then let hr = 12
'assigns data to each digit. Also adds leading zeros.
if len(str(hr)) = 1 then let n1 = f else let n1 = left(str(hr),1)
let n2 = right(str(hr),1)
if len(min) = 1 then let n3 = 0 else let n3 = left(minute,1)
if ampm = 1 then let n4 = "8" & right(minute,1) else let n4 = right(minute,1) 'if it is PM then set decimal point on last digit high
return

[write7219]
spi.hex(digit1 & n1,2)
io(po,load,1)
delay 50
io(po,load,0)
spi.hex(digit2 & n2,2)
io(po,load,1)
delay 50
io(po,load,0)
spi.hex(digit3 & n3,2)
io(po,load,1)
delay 50
io(po,load,0)
spi.hex(digit4 & n4,2)
io(po,load,1)
delay 50
io(po,load,0)
spi.hex(digit5 & "40",2) 'turns on segment A of digit 5
io(po,load,1)
delay 50
io(po,load,0)
return