// https://forum.arduino.cc/t/how-to-delay-using-millis/1142938
// https://wokwi.com/projects/395109385751281665

// version 6. Sue me.

# include <Servo.h>

const byte startPin = 3;
const byte goodPin = 2;
const byte testingLED = 7;

Servo myServo;
const byte servoPin = 9;

const unsigned long testInterval = 2500; // action 2.5 seconds for testing - life too short

void setup() {
  Serial.begin(115200);
  Serial.println("\nWake up!\n");

  pinMode (startPin, INPUT_PULLUP);
  pinMode (goodPin, INPUT_PULLUP);

  pinMode (testingLED, OUTPUT);

  myServo.attach(servoPin);
  myServo.write(90);
}

# define PRESST LOW

void loop() {
  static unsigned long startTime; // test timer

  static bool inTest;             // are we testing?
  unsigned long now = millis();   // fancier 'currentMillis' ooh!

  bool partOK = digitalRead(goodPin) == PRESST;
  bool startTest = digitalRead(startPin) == PRESST;

  bool passed = false;
  bool failed = false;

  if (!inTest) {
    if (startTest) {
      startTime = now;
      Serial.println("testing...");
      inTest = true;
    }
  }

  if (inTest) {
    if (now - startTime > testInterval) {
      Serial.println("            device failed."); 
      failed = true;  
      inTest = false; 
    }

    if (partOK) {
      Serial.println("            device passed!");
      passed = true;  
      inTest = false; 
    }
  }

  digitalWrite(testingLED, inTest ? HIGH : LOW);
  
  if (passed) {
    Serial.println("\nPASS");
    myServo.write(0);
    delay(500);
  }

  if (failed) {
    Serial.println("\nFAIL");
    myServo.write(180);
    delay(500);
  }

  myServo.write(90);
}

/*
void loop0() {
  static unsigned long lastLoop;

  static byte previousButton;
  static byte theState;

  static unsigned long startTime; // time at action
  static bool inTestPhase;        // are we testing?

  bool gotPressed = false;        // set each loop - use it or lose it
  bool gotReleased = false;       // also

  unsigned long now = millis();   // fancier 'currentMillis' ooh!

// just run the rest of this at 1000 / RATE Hz

  if (now - lastLoop < RATE) return;
  lastLoop = now;



// read and deglitch the input
  int button = digitalRead(buttonPin) == PRESST;

  if (button == previousButton) {   // stable input
    if (button != theState) {       // edge detected

      if (button) {
        // Serial.println("I see that go pressed");
        gotPressed = true;
      }
      else {
        // Serial.println("I see that go released");
        gotReleased = true;
      }

      theState = button;
    }
  }
  previousButton = button;


// now we have theState, and gotPressed and gotReleased to work with and we develop

  bool passed = false;
  bool failed = false;

// when the button is pressed, reset the timer, turn ON the LED

  if (gotPressed) {
    startTime = now;
    digitalWrite(ledPin, HIGH);
    Serial.println("                 new world. LED ON");
    inTestPhase = true;
  }

  if (!theState && inTestPhase) {
    digitalWrite(ledPin, LOW);
    Serial.println("            device failed..."); 
    failed = true;  
    inTestPhase = false; 
  }

// when (if!) the timer expires, see if we made it or not
  if (now - startTime >= actionDelay) {

// but we do the thing just once
    if (inTestPhase) {
      digitalWrite(ledPin, LOW);
      Serial.println("                            timer!"); 

      if (theState) {
        Serial.println("You made it! I hope you feel a m a z i n g!");
        passed = true;
      }
      else {
        Serial.println("this should never print.");
      }
    
      inTestPhase = false;
    }
  }

  if (passed) Serial.println("\nPASS");
  if (failed) Serial.println("\nFAIL");
}

*/
START
GOOD
TESTING
FAIL
PASS