Here's my attempt. I'm having to call the changeMotorState() function within the new delay function, which is undesirable. I want a simple delay function based on millis.
// constants won't change. Used here to set a pin number :
const int motorPin = 13; // the number of the motor pin
// Variables will change :
int motorState = LOW; // motorState used to set the motor
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time motorState was updated
void setup() {
// set the digital pin as output:
pinMode(motorPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// if the motor is off turn it on and vice-versa:
waitNoDelay(5000);
// set the motor with the motorState of the variable:
digitalWrite(motorPin, motorState);
}
void waitNoDelay(const long interval) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the motor
previousMillis = currentMillis;
cahngeMotorState();
}
}
void cahngeMotorState() {
if (motorState == LOW) {
motorState = HIGH;
} else {
motorState = LOW;
}
}
However, I want control over how long I can keep the LED on or off. For example, LOW for 30s, then HIGH for 15s, then LOW for 300s.