#include <mechButton.h>
#include <idlers.h>
#include <timeObj.h>


// ********************************************
// Create a class that manages the relays.
// ********************************************


#define RELAY1 13
#define RELAY2 12
#define RELAY3 11
#define RELAY4 10

class relayThing : public idler {

  public:
          relayThing(void);
          ~relayThing(void);

          void begin(void);
          void startRunning();
          void setRelays(bool r1,bool r2,bool r3,bool r4);
  virtual void idle(void);

          timeObj timer;
          bool    running;
          int     step;
};


relayThing::relayThing(void) { }


relayThing::~relayThing(void) {  }


void relayThing::begin(void) {

  hookup();
  running = false;
  step    = 1;
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);
  setRelays(false,false,true,false); 
  timer.setTime(2200);
}


void relayThing::startRunning() { running = true; }


void relayThing::setRelays(bool r1,bool r2,bool r3,bool r4) {

  digitalWrite(RELAY1, r1); 
  digitalWrite(RELAY2, r2); 
  digitalWrite(RELAY3, r3); 
  digitalWrite(RELAY4, r4);
}


void relayThing::idle(void) {

  if (running) {
    if (timer.ding()) {
      step++;
      if (step>7) {
        step = 1;
      }
      switch(step) {
        case 1  :
          setRelays(false,false,true,false);
          running = false;
          Serial.println("Back to step 1");
          Serial.println("Click button to begin again.");
        break;
        case 2  :
          setRelays(false,true,true,false);
          Serial.println("step 2");
          timer.start();
        break;
        case 3  :
          setRelays(false,true,false,true);
          Serial.println("step 3");
          timer.start();
        break;
        case 4  :
          setRelays(true,true,false,true);
          Serial.println("step 4");
          timer.start();
        break;
        case 5  :
          setRelays(true,false,false,true);
          Serial.println("step 5");
          timer.start();
        break;
        case 6  :
          setRelays(true,false,true,false);
          Serial.println("step 6");
          timer.start();
        break;
        case 7  :
          setRelays(true,false,true,false);
          Serial.println("step 7");
          timer.start();
        break;
      }
    }
  }
}
    


// ********************************************
// Using the class, write the program..
// ********************************************

mechButton  button(6);  // Button manager
relayThing  relayMgr;   // Relay manager.


void setup() {

  Serial.begin(115200);                             // Start serial to amuse the user.
  Serial.println("CLick button to start pattern."); // Let her know what's going on.
  button.setCallback(btnClick);                     // When the button state changes, call this function.               
  relayMgr.begin();                                 // Do all them thing needed to setup the relays.
}


// This is called when the button state changes.
void btnClick(void) {

  if (!button.getState()) {               // If it's been grounded. (Pressed)
    if (relayMgr.running) {               // Already running?
      Serial.println("Sorry, running.");  // Tell user we can't do anything.
    } else {                              // Else, not running?
      relayMgr.startRunning();            // Cool! Start it up!
    }
  }
}


void loop() { idle(); }   // Idle runs all the magic.
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
led4:A
led4:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r