# include <ezButton.h>

const int ledPin = 7;

ezButton button1(3);  // create Button object that attach to pin 2;
ezButton button2(2);  // create Button object that attach to pin 3;
ezButton button3(5);
// ezButton button4(4);

# define doItPin  4

float period = 0;
float power = 0;
float runtime = 0;
float ledon = 0;
float ledoff = 0;
float countt = 0;

void setup() {
  Serial.begin(115200); // 9600? 1980 called and wants its baud rate back!
  Serial.println("Hello Modulo 4 World!");
  
  pinMode(ledPin, OUTPUT);
  button1.setDebounceTime(25); // set debounce time to 25 milliseconds PLENTY.
  button2.setDebounceTime(25);
  button3.setDebounceTime(25);
//  button4.setDebounceTime(25);

  pinMode(doItPin, INPUT_PULLUP);

  button1.setCountMode(COUNT_FALLING);
  button2.setCountMode(COUNT_FALLING);
  button3.setCountMode(COUNT_FALLING);
//  button4.setCountMode(COUNT_FALLING);
}


void loop() {
  static unsigned long lastTime;
  unsigned long now = millis();

  button2.loop();

  if (button2.isPressed()) Serial.println("I saw that.");
   button2.loop(); 
  if (button2.isPressed()) Serial.println("                         I see this?");
    
}


void loop0()
{
  static int counter;

  static unsigned long lastTime;
  unsigned long now = millis();

  if (now - lastTime > 777) {
    button2.loop();
    lastTime = now;
  }

  if (button2.isPressed()) {
    button2.loop();
    Serial.print(counter); counter++;
    Serial.println(" I saw that!");
  }
}


void loopx() { 
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop();
//  button4.loop();
  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
//  int btn4State = button4.getState();
  unsigned long btn1Count = button1.getCount();
  unsigned long btn2Count = button2.getCount();
  unsigned long btn3Count = button3.getCount();
//  unsigned long btn4Count = button4.getCount();

  unsigned char doTheThing = !digitalRead(doItPin);
  
  Serial.print(btn1Count % 4); Serial.print("  ");
  Serial.print(btn2Count % 4); Serial.print("  ");
  Serial.print(btn3Count % 4 ); Serial.print("  ");
  Serial.print(doTheThing); Serial.print("  ");

  Serial.print("     ");

  if ((btn1Count % 4)  == 0) {
     period = 12500;
  }
  else if ((btn1Count % 4) == 1) {
     period = 10000;
  }
  else if ((btn1Count % 4) == 2) {
     period = 4000;
  }
  else if ((btn1Count % 4) == 3) {
     period = 2000;
  }
  
  if ((btn2Count % 4) == 0) {
      power = 90;
  }
  else if ((btn2Count % 4) == 1) {
      power = 80;
  }
  else if ((btn2Count % 4) == 2) {
       power = 60;
  }
  else if ((btn2Count % 4) == 3) {
       power = 40;
  }

  if ((btn3Count % 4) == 0) {
       runtime = 6;               // runtime in sec
  }
  else  if ((btn3Count % 4) == 1) {
       runtime = 8;
  }
  else  if ((btn3Count % 4) == 2) {
       runtime = 20;
  }
  else if ((btn3Count % 4) == 3) {
       runtime = 30;
  }

  static long previousBtn4Count = 0;
  if (doTheThing) {
    ledon = period * power*10;
    ledoff = period * (100 - power)*10;
    countt = runtime*1000000 / period;

ledon /= 1000;
ledoff /= 1000;


    Serial.println("");
    Serial.print("period: ");
    Serial.println(period);
    Serial.print("power: "); 
    Serial.println(power);
    Serial.print("ledon: ");
    Serial.println(ledon);
    Serial.print("ledoff: "); 
    Serial.println(ledoff);
    Serial.print("runtime: "); 
    Serial.println(runtime);
    Serial.print("runtime: "); 
    Serial.println("countt:");
    Serial.println(countt);

// fork();
Serial.println("running the train now");
unsigned long now = millis();


    for (int y = 0; y < countt - 1; y++) {

      digitalWrite(ledPin, HIGH);
      delayMicroseconds(ledon);
      digitalWrite(ledPin, LOW);
      delayMicroseconds(ledoff);
    }

Serial.print("       it took ");
Serial.print((millis() - now) / 1000);
Serial.println(" seconds ");

    digitalWrite(ledPin, LOW);
// fork();
    delay(3000);

  }
  Serial.println("");
}

// stick a fork in this turkey [freeze the program]
void fork()
{
  Serial.println("\nFORK!\n");
  for (; ; ); 
}