#include "ArduinoTrace.h"
#include "ptScheduler.h"
#include "ezButton.h"

ptScheduler Task1Sch=ptScheduler(1000000);

bool T1Start;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Task1Sch.call())
  {
    T1Start=true;
    Task1();
  } 
}

void Task1()
{
  enum {null,s1,s2,s3};
  static int step=s1;
  int pstep=null;
  static int timer=1;
  bool Q0=0;        //continous action
  while(step!=pstep)
  {
    pstep=step;
    switch(step)
    {
      case s1:
        if(T1Start)
        {
          T1Start=false;
          step=s2;
        }
      break;
      case s2:
        Q0=1;
        if(timer==0)
        {    
          Q0=0;     
          timer=2;
          step=s3;
        }
      break;
      case s3:
        if(timer==0)
        {       
          timer=1; 
          step=s2;
        }
      break;
    }  
  }

  if(timer>0) timer--;
  DUMP(Q0);
  DUMP(millis());
  digitalWrite(9,Q0);
  
}