Start stop with delay

Yes.
This is my proposal for a solution based on the BWOD example.
Please check this out to your requierments. The sketch is UNO tested.

// BLOCK COMMENT
// ATTENTION: This Sketch contains elements of C++.
// https://www.learncpp.com/cpp-tutorial/
// https://forum.arduino.cc/t/start-stop-with-delay/908129
#define ProjectName "Start stop with delay"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware and needs.
constexpr byte Button_ {A0};  // portPin o---|button|---GND
constexpr byte Analoque {A3};
constexpr long minTime {500};
constexpr long maxTime {5000};
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  pinMode(Button_, INPUT_PULLUP);
}
void loop () {
  unsigned long currentTime = millis();
  static unsigned long blinkLed;
  static bool control_;
  if (!digitalRead(Button_)&& !digitalRead(LED_BUILTIN)) {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println(F("LED on"));
    control_ = true;
    blinkLed = currentTime;
  }
  if (currentTime - blinkLed >= (unsigned long) map(analogRead(Analoque), 0, 1023, minTime, maxTime) && control_) {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println(F("LED off"));
    control_ = false;
  }
}

Have a nice day and enjoy coding in C++.

1 Like