 
 #define PIN_RX     1 // Serial
 #define PIN_TX     2 // Serial
-#define PIN_LED_R  3 // LED
-#define PIN_D4     4 //
+#define PIN_LED_G  3 // LED
+#define PIN_D4     4 // Button
 #define PIN_DHT    5 // Hum and Temp Sensor
-#define PIN_D6     6 //
-#define PIN_D7     7 //
-#define PIN_D8     8 //
-#define PIN_LED_G  9 // LED
-#define PIN_LED_B 10 // LED
+#define PIN_D6     6 // Button
+#define PIN_D7     7 // Button
+#define PIN_D8     8 // Button
+#define PIN_LED_B  9 // LED
+#define PIN_LED_R 10 // LED
 #define PIN_MOSI  11 // Programming
 #define PIN_MISO  12 // Programming
 #define PIN_SCK   13 // Programming
 #define PIN_A1    A1 //
 #define PIN_A2    A2 //
-#define PIN_A3    A3 //
+#define PIN_LIGHT A3 // Light
 #define PIN_A4    A4 //
-#define PIN_A5    A5 // fff
+#define PIN_A5    A5 // 
 
-#define sampleDelay 1000
+#define SAMPLE_DELAY    1000
+#define BUTTON_DELAY     500
+#define AVERAGE_COUNTER  100
+#define NUM_BUTTONS        4
 
 dht          DHT;
 CmdMessenger cmdMessenger = CmdMessenger(Serial, ',', ';');
 uint32_t     lastSampleTime;
+uint32_t     lastButtonTime[NUM_BUTTONS];
 
 enum{
   kAcknowledge,

  kSetB,
  kGetTemp,
  kGetHumidity,
};

void attachCommandCallbacks(){
  cmdMessenger.attach(OnUnknownCommand);
  cmdMessenger.attach(kSetRgb,   OnSetRgb);
  cmdMessenger.attach(kSetR,        OnSetR);
  cmdMessenger.attach(kSetG,        OnSetG);
  cmdMessenger.attach(kSetB,        OnSetB);
  cmdMessenger.attach(kGetTemp,     OnGetTemp);
  cmdMessenger.attach(kGetHumidity, OnGetHumidity);
}

void OnUnknownCommand(){

  cmdMessenger.sendCmd(kAcknowledge, "Arduino ready!");
  wdt_enable(WDTO_2S);
  lastSampleTime = 0;
  for(uint8_t i = 0; i < NUM_BUTTONS; i++){
    lastButtonTime[i] = 0;    
  }
  analogWrite(PIN_LED_R, 0);
  analogWrite(PIN_LED_G, 0);
  analogWrite(PIN_LED_B, 0);
  pinMode(PIN_D4, INPUT_PULLUP);
  pinMode(PIN_D6, INPUT_PULLUP);
  pinMode(PIN_D7, INPUT_PULLUP);
  pinMode(PIN_D8, INPUT_PULLUP);
}

void buttonCheck(uint16_t pin, uint8_t button){
  if(!digitalRead(pin)) { 
    if( (millis() - lastButtonTime[button]) > BUTTON_DELAY){
      cmdMessenger.sendCmd(kButtonPressed, button);
      lastButtonTime[button] = millis(); 
    }  
  }
}

void parseButtons(){
  buttonCheck(PIN_D4, 0);
  buttonCheck(PIN_D6, 1);
  buttonCheck(PIN_D7, 2);
  buttonCheck(PIN_D8, 3);
}

void loop() {  
  cmdMessenger.feedinSerialData();
  parseButtons();
  wdt_reset();
}

