' Make it with Micromite (Part 10)

' MKC_Clock_v3.txt

' Code written: September 2019



' --------------------------------------------------------------------------------------------------------

' IMPORTANT NOTEs:

' ================

'

' (Ensure the IPS Driver is loaded in the Library as discussed in Part 9)

'

' 1> RUN the program

' 2> Press the 'UP' key on your IR remote and note the value displayed on the console screen

' 3> Repeat for 'DOWN', 'LEFT' and 'RIGHT' keys

' 4> EDIT the SUB IR_Int and use the values (from above) in the four lines of code starting with 'CASE'

'

' Making the above change ensures your IR remote's UP and DOWN buttons control the IPS backlight brightness.

' The LEFT button will select the Date, and the RIGHT button will select the Temperature

' --------------------------------------------------------------------------------------------------------



' SETUP

' =====

'

Option autorun on                              ' ensure program starts automatically on power-up

IR DevCode, KeyCode, IR_Int                    ' set up Infra-Red interrupt

SetTick 60000,TimeFromRTC                      ' every 60 seconds, update MM time & date from RTC (to avoid drift)

TimeFromRTC                                    ' call SUB to initially get the correct time & date from the RTC

blb=80                                         ' set BackLight Brightness variable (blb) initially to 80

PWM 2,800,blb,50                               ' PWM 2 (800Hz): backlight 80%, buzzer 50% (ON when Pin15=0v)

Box 0,0,160,80,3,RGB(green),RGB(blue)          ' clear IPS screen to a blue background with a thin green border

colon                                          ' call SUB to draw a colon for the clock (between hours and minutes)



Dim Month$(12)                                 'string array to hold 3-character Month name

Month$(1)="Jan"

Month$(2)="Feb"

Month$(3)="Mar"

Month$(4)="Apr"

Month$(5)="May"

Month$(6)="Jun"

Month$(7)="Jul"

Month$(8)="Aug"

Month$(9)="Sep"

Month$(10)="Oct"

Month$(11)="Nov"

Month$(12)="Dec"



Dim DOW$(6)=("Sun","Mon","Tue","Wed","Thu","Fri","Sat")  ' string array to hold 3-character Day name





' MAIN PROGRAM

' ============

'

Do

  Text 35,25,Left$(Time$,2),cm,7,3,RGB(yellow),RGB(Blue)              ' display hours (in 7-seg Font 7, scale 3)

  Text 92,25,Mid$(Time$,4,2),cm,7,3,RGB(yellow),RGB(Blue)             ' display minutes (in 7-seg Font 7, scale 3)

  Text 136,30,Right$(Time$,2),cm,7,2,RGB(white),RGB(blue)             ' display seconds (in 7-seg Font 7, scale 2)

  If Disp_Data = 0 Then                                               ' If Disp_Data = 0 then show date

    DayOfWeek                                                           ' get abbreviated day-name (into wwDoW$)

    DispDate$ = wwDoW$ + " " + Left$(Date$,2) + " " + Month$(Val(Mid$(Date$,4,2)))  ' construct date to display

    Line 10,49,150,49,1,RGB(blue)

    Line 10,66,150,66,3,RGB(blue)

    Text 80,58,DispDate$,cm,8,2,RGB(brown),RGB(blue)                    ' display date (in smaller Font 8)

  Else                                                                ' otherwise if Disp_Data = 1 then display Temperature

    Text 80,58," "+Str$(TEMPR(2),0,1)+Chr$(96)+"C ",cm,1,2,RGB(brown),RGB(blue)   ' display temperature

  End If

Loop





' SUBROUTINES

' ===========

'

Sub Colon                                      ' SUB to draw a colon for the clock (between hours and minutes)

  Text 63,15,".",cm,1,1,RGB(white),RGB(blue)     ' draw 'top' dot

  Text 63,29,".",cm,1,1,RGB(white),RGB(blue)     ' draw 'bottom' dot

End Sub



Sub TimeFromRTC                                ' SUB to get time from RTC and load MM system Time$ and Date$

  RTC gettime                                    ' (refer to User Manual for further details)

End Sub



Sub IR_Int                                     ' IR interrupt SUB: Increase or Decrease BackLight Brightness (blb)

Print keycode

  Select Case keycode                            ' ** CUSTOMISE VALUES FOR YOUR IR TRANSMITTER

    Case 98                                      ' ** UP button ** (Increase Brightness)

      blb=blb+10                                 ' increase blb value by 10

      If blb>100 Then                            ' see if blb is bigger than the maximum allowed value (of 100)

        blb=100                                  ' if so, then ensure it remains at 100

      Else

        Beep                                     ' otherwise a valid increment - so sound piezo

      End If

    Case 168                                      ' ** DOWN button ** (Decrease Brightness)

      blb=blb-10                                 ' decrease blb value by 10

      If blb<0 Then                              ' see if blb is smaller than minimum allowed value (of 0)

        blb=0                                    ' if so, then ensure it remains at 0

      Else

        Beep                                     ' otherwise a valid decrement - so sound piezo

      End If

    Case 34                                     ' ** LEFT button ** (select Date)

      If Disp_Data = 1 Then      ' see if currently on Temp

        Disp_Data = 0         ' if so, then set to 0 = Date to display in bottom of screen

        Beep                                         ' and beep once

      End If

    Case 194                                      ' ** RIGHT button ** (select Temperature)

      If Disp_Data = 0 Then                        ' see if currently on Date

        Disp_Data = 1                                ' if so, then set to 1 = Temp to display in bottom of screen

        beep                                         ' and beep once

      End If

  End Select

End Sub



Sub Beep                                       ' make a beep sound (and set backlight brightness)

  PWM 2,800,blb,50                               ' set the correct backlight brightness (blb)

  SetPin 15,dout                                 ' set pin 15 to 0v i.e. piezo will sound (at 800Hz, at 50% duty)

  Pause 10                                       ' brief pause to allow audible 'beep'

  SetPin 15,off                                  ' return pin 15 to 'floating' i.e. switch piezo OFF

End Sub



Sub DayOfWeek                                  ' formula to calculate day-of-week from a numeric date

  wwYear=Val(Right$(Date$,2))

  wwMonth=Val(Mid$(Date$,4,2))

  wwDay=Val(Mid$(Date$,1,2))

  a = Int((14-wwmonth)/12)

  m = wwmonth + 12*a - 2

  y = wwyear - a

  wwDoW$=DOW$((wwday+y+Int(y/4)-Int(y/100)+Int(y/400)+Int(31*m/12)) Mod 7)    ' load wwDoW$ with 3-letter day name

End Sub



DefineFont #8                                  ' smaller Font  (6 x 8) - used to ensure Date fits on IPS screen

5C200806

00000000 82200000 00800008 00004551 4F510000 0045F994 2B1CEA21 690000C2

B04C2090 AA104A62 84608046 00000000 40108410 81400081 00841004 A89C8A00

82000080 0080203E 30000000 00000042 0000003E 01000000 21000086 00004208

CAAA2972 82210027 00872008 21842072 42F8800F 00270A04 F824C510 0FFA0041

00270A02 8A3C0831 21F80027 00044108 8A9C2872 28720027 0027089E 61008601

86010080 00846000 40208410 0F000081 0000F880 10028140 20720084 00022084

BA9A2072 28720027 8028FAA2 8ABC28F2 2872002F 00278220 8AA248E2 08FA004E

800F823C 823C08FA 28720008 80278A2E 8ABE288A 82708028 00872008 12044138

4A8A0046 8048A230 82200882 6A8B800F 80288AAA 9AAA2C8A 28728028 00278AA2

82BC28F2 28720008 8046AAA2 A2BC28F2 087A8048 002F081C 200882F8 288A0082

00278AA2 89A2288A 288A0042 00A5AAAA 5208258A 288A8028 00822094 420821F8

0471800F 00074110 A9944AA9 4170804A 00471004 00804821 00000000 800F0000

00008140 07000000 80277A02 8A320B82 0700002F 00278220 8AA62608 07008027

0007FA22 41382431 E8010004 002778A2 8A320B82 06208028 00872008 12040310

09820046 0089C228 20088260 0D000087 8028AA2A 8A320B00 07008028 00278A22

F2220F00 06000008 802078A6 82320B00 07000008 002F7020 41100E41 08000023

80668AA2 89A20800 08000042 00A5AAA2 21940800 08008048 002778A2 21840F00

0400800F 0EE24028

End DefineFont



DefineFont #7                                  ' 7-Segment style font (8 x 13) - used for Time digits

  0B300D08 82827C00 82008282 7C828282 02020000 00020202 02020202 7C000002

  02020202 8080807C 00007C80 0202027C 02027C02 007C0202 82828200 027C8282

  02020202 807C0000 7C808080 02020202 7C00007C 80808080 8282827C 00007C82

  0202027C 02020002 00020202 82827C00 827C8282 7C828282 827C0000 7C828282

  02020202 0000007C 00303000 30300000 00000000

End DefineFont