$regfile = "M328PDEF.DAT"
'Arduino Nano

$crystal = 2000000                                          'Crystal/8

$hwstack = 100
$swstack = 100
$framesize = 100

Portb = &HFF
Portc = &HFF
Portd = &HFF                                                'Pullups

Tasterw Alias Pinb.1
Tasterc Alias Pinb.0

Portd.7 = 0
Servopower Alias Ddrd.7                                     'as open collector inverted

Ddrb.5 = 1
Led Alias Portb.5
Led = 0

Config Timer0 = Timer , Prescale = 1024                     'counts 512µs, top 131ms
On Timer0 T0ovf

Enable Timer0

Dim T0counter As Byte                                       'max 64s

Tccr1b.wgm13 = 1
Tccr1a.wgm10 = 1                                            'Phase correct pwm, OCR1A as Top
Tccr1a.com1b1 = 1                                           'PB.2 / D10 on Nano, Clear on match upcounting, set downcounting
Tccr1b.cs01 = 1                                             'pescaler 1, 0,5µs
Ocr1a = 40000                                               '20ms period
Portb.2 = 0
Ddrb.2 = 1
Servo Alias Ocr1b                                           'in 1µs, 1000-2000 (phase correct)

Const Posw = 2200
Const Posc = 800
Const Neutral = 1500

Enable Interrupts

Dim 2second As Long
Dim Day As Byte

'####### All fixed values ######
Const Warmhr = 19
Const Coldhr = 19

Dim Warmmin As Long
Warmmin = 30
Dim Coldmin As Long
Coldmin = 31

Dim Warmsec As Long
Dim Coldsec As Long

Warmsec = Warmhr * 1800
Warmmin = Warmmin * 30
Warmsec = Warmsec + Warmmin
Coldsec = Coldhr * 1800
Coldmin = Coldmin * 30
Coldsec = Coldsec + Coldmin
'###############################
Const Starthour = 3
Dim Startmin As Long
Startmin = 11
Startmin = Startmin * 30
2second = Starthour * 1800
2second = 2second + Startmin

Servo = Neutral

Do
   Idle

   If T0counter = 16 Then                                   '2seconds
      T0counter = 0
      Servo = 0
      Servopower = 0                                        'Off after 2s in every case

      Incr 2second
      If 2second = 43200 Then                               'Overflow day
         2second = 0
         Incr Day
         If Day = 7 Then Day = 0
      End If

      Select Case 2second
         Case Warmsec:
            Servopower = 1
            Servo = Posw
         Case Coldsec:
            Servopower = 1
            Servo = Posc
         Case Else :
            Servopower = 0
      End Select

      If Tasterc = 0 Then
         If Servo > Posc Then
            Servopower = 1
            Servo = Servo - 200
         End If
      End If
      If Tasterw = 0 Then
         If Servo < Posw Then
            Servopower = 1
            Servo = Servo + 200
         End If
      End If
   End If

Loop

'##########################
T0ovf:
   Timer0 = 12
   Incr T0counter
Return