I thought millis() started when the program first started running.
So.
what is a good method to end it at a certain point?
loop() repeats forever so when you have finished it starts again.
One way to stop it is to hold it in another loop like:-
while(true) { }
BUT - no one ever writes a program like this.
this was my first program ever, and just to explore what could be done
what's a better structure to control a motor for a given period of time, or events
and the shut it down?
Try this, my lines have the comments at the beginning. Remove them and upload to your board. The variable run is just a flag to decide what to do. A digital input could be read to determine if you want to run the motor again by setting run to true again. That is if milli() resets each run of loop() as implied earlier.
/*
DC Motor test program
*/
int relay = 3;
unsigned long Timer;
// boolean run;
void setup() // run once, when the sketch starts
{
pinMode(relay, OUTPUT);
// run = true;
}
void loop() {
// if(run){
Timer = millis();
digitalWrite(relay, HIGH);
delay(1000);
digitalWrite(relay, LOW);
delay(1000);
if (Timer > 4000)
{
digitalWrite(relay, LOW);
// run = false;
}
// }
// Add additional code here to detect an input and reset run if wanted.