#define BOARDTYPE_CONSTANT "ESP8266-MP3-OTA-PushS"
// 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 *


const int analogWriteSpeed = 55;
const int motorpin1 = 5;  // Example valid pin number
const int motorpin2 = 6;  // Example valid pin number
const int buttonPin = 2;  // Pin where the button is connected

unsigned long shortPressCount = 0;
unsigned long runtime = 0;
bool motorstart = false;
unsigned long buttonPressTime = 0;
int bitch;

void shortPressAction();
void longPressAction();

void setup() {
  Serial.begin(9600);
  //while (Serial) {}
  Serial.println("hello world");

  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up resistor

}

void loop() {
  bool buttonState = digitalRead(buttonPin);
  //Serial.println(buttonState);
  delay(100);


  if (buttonState == HIGH ) {
    bitch = millis();

    while (digitalRead(buttonPin) == HIGH) {
      delay(100);
      Serial.println("help");
      dbgi("0A", millis(), 500 );
      dbgi("0B", runtime, 500);
      dbgi("0C", millis() - runtime, 500);

      //runtime = millis() + runtime;
      dbgi("updated", runtime, 500);
    }

    if (millis() - bitch >= 3000) {
      Serial.println("Long press triggered?");
      longPressAction();
    }
    else {
      motorstart = true;
    }

  }
  loop1();
} //This is the bottom of void loop


void loop1() {
  //Serial.println("entering loop1");

  if (millis() - runtime >= 6000 ) {
    dbg("01", millis() );
    dbg("02", millis() - runtime);

    runtime = millis() + runtime;
    Serial.println("runtime update");
    dbg("updated", runtime);
  }

  if (motorstart == true) {
    shortPressAction;
  }

  delay(100);
  //Serial.println("exiting loop1");
}


void longPressAction() {
  Serial.begin(9600);
  Serial.println("Long press action triggered");

  if (Serial) {
    unsigned long totalMinutes = runtime / 60000;
    unsigned long minutes = totalMinutes % 60;
    unsigned long totalHours = totalMinutes / 60;
    unsigned long hours = totalHours % 24;
    unsigned long days = totalHours / 24;

    Serial.print("Device run time: ");
    Serial.print(days);
    Serial.print(" days, ");
    Serial.print(hours);
    Serial.print(" hours, ");
    Serial.print(minutes);
    Serial.println(" minutes");

    Serial.print("Short press action triggered ");
    Serial.print(shortPressCount);
    Serial.println(" times");
  }
}

void shortPressAction() {
  if (Serial) {
    Serial.println("shortPressAction triggered");
  }

  shortPressCount ++;
  for (int i = 0; i < 10; i++) {
    digitalWrite(LED_BUILTIN, LOW);
    analogWrite(motorpin1, analogWriteSpeed);
    delay(2000);
    digitalWrite(motorpin1, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    analogWrite(motorpin2, analogWriteSpeed);
    delay(2000);
    digitalWrite(motorpin2, LOW);
    delay(1000);
  }
  motorstart = false;
  digitalWrite(LED_BUILTIN, LOW);
}
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
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r