// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *

#include <Stepper.h>
int stepsPerRevolution = 512; // 2048 - 360 degrees
Stepper st(stepsPerRevolution, 8, 10, 9, 11);
const byte redPin   = 13;
const byte greenPin = 12;
const byte bluePin  = 5; // RGB LED

const byte button1Pin = 7;
const byte button2Pin = 6; // Push button 1 and 2

const byte btnPressed  = LOW;
const byte btnReleased = HIGH;

int t = 300; // delay time
int button1New, button1Old = 0;
int button2New, button2Old = 0;
int stPosNew = 1; // 1 - closed, 0 - open
int speed = 10;

void setup() {
  Serial.begin(9600);
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(redPin,   OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin,  OUTPUT);
  st.setSpeed(speed);

  digitalWrite(redPin, LOW);
  digitalWrite(bluePin, LOW);
  digitalWrite(greenPin, HIGH);

}

void loop() {
  button1New = digitalRead(button1Pin);
  button2New = digitalRead(button2Pin);
  //Serial.print(button2New);
  //Serial.print(" ");
  //Serial.println(button1New);
  //delay(t);
  dbgc("01",button1New); // print once if value has changed
  dbgc("02",button2New); // print once if value has changed

  dbgc("03",stPosNew); // print once if value has changed

  // Button 1 pressed
  if (button1New == btnPressed && button1Old == btnReleased) {
    if (stPosNew == 1) { // Door is closed
      st.step(-stepsPerRevolution); // Opening door
      delay(50);
      stPosNew = 0; //Door is open
    }
    else { // Door is open
      st.step(stepsPerRevolution); // Closing door
      delay(50);
      digitalWrite(redPin, HIGH);
      digitalWrite(greenPin, LOW);
      digitalWrite(bluePin, LOW);
      stPosNew = 1;
    }
  }
  button1Old = button1New;


  // Button 2 pressed
  if (button2New == btnPressed && button2Old == btnReleased) {
    if (stPosNew == 1) {
      st.step(-stepsPerRevolution); // Opening door
      delay(50);
      stPosNew = 0;

    } else { // Door is open
      st.step(stepsPerRevolution); // Closing door
      delay(50);
      stPosNew = 1;
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin, LOW);
    }
  }
  button2Old = button2New;
}