'''
Description: Onboard Timer / Scheduler Program.
Author     : M.Pugazhendi
Date       : 03rdJuly2021

A. Two timers are initialized.
B. The timer_two is used for changing the state @20 Sec interval
C. And initialize timer_one, trigger LED blink period either 100mSec or 250mSec or 1Sec.

'''

from machine import Pin, Timer

#Initialize the onboard LED as ouput
led = Pin(25, Pin.OUT)

#Initialize timer_one. Used for toggeling the LED
timer_one = Timer()

#Initialize timer_two. Used for changing the State
timer_two = Timer()

#Initialize state variable. Used for changing the State
state = 1

def BlinkLED(timer_one):
    led.toggle()
    
def ChangeState(timer_two):
    global state
    
    if state == 1:
     # 100mS Timer initialization   
     timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
     state = state+1
    elif state == 2:
     # 250mS Timer initialization
     timer_one.init(freq=4, mode=Timer.PERIODIC, callback=BlinkLED)
     state = state+1
    elif state == 3:
     # 1000mS Timer initialization
     timer_one.init(freq=1, mode=Timer.PERIODIC, callback=BlinkLED)
     state = 1
    else:
      # Default state
      # 100mS Timer initialization
      state = 1
      timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
 
# Initialize the timer one for first time
timer_one.init(freq=10, mode=Timer.PERIODIC, callback=BlinkLED)
state = state+1

#Subcequent timer states for 20 seconds interval
timer_two.init(freq=0.05, mode=Timer.PERIODIC, callback=ChangeState)
