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

ptScheduler Task1Sch=ptScheduler(1000000);
ezButton buttonG(3);
ezButton buttonR(2);
ezButton buttonY(4);

bool T1Ready;
bool bGFlag,bRFlag,bYFlag;
bool ledState=LOW;
//bool Q0;

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

void loop() {
  // put your main code here, to run repeatedly:
  buttonG.loop();
  buttonR.loop();
  buttonY.loop();
  if(buttonG.isPressed()) bGFlag=true;
  if(buttonR.isPressed()) bRFlag=true;
  if(buttonY.isPressed()) bYFlag=true;
  //if(Task1Sch.call()) T1Ready=true;
  if(Task1Sch.call()) Task1();
  //Task1();
}

void Task1()
{
  enum {null,s1,s2};
  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:
        Q0=1;
        if(timer==0)
        {    
          Q0=0;     
          timer=2;
          step=s2;
        }
      break;

      case s2:
        if(timer==0)
        {       
          timer=1; 
          step=s1;
        }
      break;
    }  
  }

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