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

ptScheduler Task1Sch=ptScheduler(1000000);
ezButton button0(2);
ezButton button1(3);

bool btn0Psd,btn1Psd;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  button0.setDebounceTime(50);
  button1.setDebounceTime(50);
  pinMode(4, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(8, OUTPUT);
}

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

void Task1()
{
  enum {null,s1,s2,s3,s4};
  static int step=s1;
  int pstep=null;
  static int timer=5;
  bool G=0,Y=0;
  static bool R=0;        //stored action
  while(step!=pstep)
  {
    pstep=step;
    switch(step)
    {
      case s1:
        G=1;
        if(timer==0)
        {     
          G=0;  
          timer=2;
          step=s2;
        }
      break;
      case s2:
        Y=1;
        if (timer==0)
        {
          Y=0;
          timer=4;
          step=s3;
        }
      break;
      case s3:
        R=1;
        if (timer==0)
        {
          timer=2;
          step=s4;
        }
      break;
      case s4:
        Y=1;
        if(timer==0)
        {
          Y=0; R=0;
          timer=15;
          step=s1;
        }


      break;
    }  
  }

  btn0Psd=false;
  btn1Psd=false;

  if(timer>0) timer--;
  digitalWrite(8,G);
  digitalWrite(6,Y);
  digitalWrite(4,R);
}