How to set output pin to high for set time???

I am trying to trip a reflective sensor and make a dc motor turn on for 5 secs then turn motor off.
This is my code, but I don't know how to set the output pin high for a set time or can this even be done??
any help would be greatly appreciated.

// constants won't change. They're used here to
// set pin numbers:
const int sensorPin = 2; // the number of the sensor pin
const int motorPin = 13; // the number of the motor pin

// variables will change:
int sensorState = 0; // variable for reading the sensorState status

void setup() {
// initialize the motor pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(sensorPin, INPUT);
}

void loop(){
// read the state of the sensorState value:
sensorState = digitalRead(sensorPin);

// check if the sensor is tripped.
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
// turn motor on:

digitalWrite(motorPin, HIGH); // turns the motor on

}
else {
// turn motor off:
digitalWrite(motorPin, LOW);
}
}

Create a variable named lastTimeTheMotorWasOff and set its value to millis() whenever you turn the motor off.

Outside of any other conditions, check to see if millis() - lastTimeTheMotorWasOff is greater than 5 seconds. If it is, turn it off.

See the Blink Without Delay example for information on using millis()

Arrch:
Create a variable named lastTimeTheMotorWasOff and set its value to millis() whenever you turn the motor off.

Outside of any other conditions, check to see if millis() - lastTimeTheMotorWasOff is greater than 5 seconds. If it is, turn it off.

See the Blink Without Delay example for information on using millis()

Thank you for the reply! I am new to all this so its a lot of trail and error. I have been working and failing over and over
on this. I am trying to understand how the millis function works, but hitting a wall so far. I am looking at blink without delay
and trying to figure it out.

wm0104:
I am trying to understand how the millis function works, but hitting a wall so far.

Then you're overthinking it. All the millis function does is return the number of milliseconds since the Arduino was last powered on or reset. It's a stopwatch that can't be paused or stopped, only reset. That's it.

This demo several things at a time may help to illistrate how to use millis().

...R

Robin2:
This demo several things at a time may help to illistrate how to use millis().

...R

I can get the led on board to blink every 5 secs, but when I activate sensor the motor only works
while the sensor is activated. ie breaking the light beam. I am trying to break the light beam with an object
and cut the dc motor on for 5 secs then everything reset. This is what I have for code and I cant get it to work correctly.

const int switchPin = 8;
unsigned long previousTime = 0;
const int ledPin = 13;

int switchState=0;
int prevSwitchState=0;
int led=2;
long interval=5000;

void setup() {
for(int x=2;x<8;x++){
pinMode(x,OUTPUT);
}
pinMode(switchPin, INPUT);
}

void loop(){
unsigned long currentTime = millis();

if(currentTime-previousTime>interval){
previousTime=currentTime;
digitalWrite(led, HIGH);
led++;
}

switchState=digitalRead(switchPin);
if(switchState!= prevSwitchState){
for(int x=2;x<8;x++){
digitalWrite(x,LOW);
}
}
}

wm0104:
I can get the led on board to blink every 5 secs, but when I activate sensor the motor only works
while the sensor is activated. ie breaking the light beam. I am trying to break the light beam with an object
and cut the dc motor on for 5 secs then everything reset. This is what I have for code and I cant get it to work correctly.

See reply #1.

wm0104:
I can get the led on board to blink every 5 secs, but when I activate sensor the motor only works
while the sensor is activated. ie breaking the light beam. I am trying to break the light beam with an object
and cut the dc motor on for 5 secs then everything reset. This is what I have for code and I cant get it to work correctly.

I find it helpful to think about millis() more like a timestamp, a number that reflects the current time.

timeStamp = millis();

elapsed time since last timestamp:

elapsedTime = millis() - timeStamp;

try getting rid of all that commentary and get it down to something simple like this:

const int sensorPin = 2;     // the number of the sensor pin
const int motorPin =  13;      // the number of the motor pin
unsigned long startTime;
int oldState;
//
void setup() 
{
  pinMode(motorPin, OUTPUT);      
  pinMode(sensorPin, INPUT);     
}
//
void loop()
{
  int sensorState = digitalRead(sensorPin);
  if (sensorState == HIGH) 
  {
    if (oldState == LOW)
    {     
      digitalWrite(motorPin, HIGH);   // turns the motor on
      startTime = millis();
    } 
  }
  oldState = sensorState;
  if (digitalRead(motorPin == HIGH))
  {
    if (millis() - startTime >= 5000UL)
    {
      digitalWrite(motorPin, LOW);
    }
  }
}

BulldogLowell:

wm0104:
I can get the led on board to blink every 5 secs, but when I activate sensor the motor only works
while the sensor is activated. ie breaking the light beam. I am trying to break the light beam with an object
and cut the dc motor on for 5 secs then everything reset. This is what I have for code and I cant get it to work correctly.

I find it helpful to think about millis() more like a timestamp, a number that reflects the current time.

timeStamp = millis();

elapsed time since last timestamp:

elapsedTime = millis() - timeStamp;

try getting rid of all that commentary and get it down to something simple like this:

const int sensorPin = 2;     // the number of the sensor pin

const int motorPin =  13;      // the number of the motor pin
unsigned long startTime;
int oldState;
//
void setup()
{
  pinMode(motorPin, OUTPUT);     
  pinMode(sensorPin, INPUT);     
}
//
void loop()
{
  int sensorState = digitalRead(sensorPin);
  if (sensorState == HIGH)
  {
    if (oldState == LOW)
    {     
      digitalWrite(motorPin, HIGH);   // turns the motor on
      startTime = millis();
    }
  }
  oldState = sensorState;
  if (digitalRead(motorPin == HIGH))
  {
    if (millis() - startTime >= 5000UL)
    {
      digitalWrite(motorPin, LOW);
    }
  }
}

Thank you for your reply!!
I ran this code and as long as the object is in front of the sensor the motor will run but as soon as you move it the motor will stop?

sensor I am using is a "Keyence FS-T1".......
I have the sensor mounted in a hole drilled in a tube.
I am dropping a ball down the tube and when the ball breaks the beam motor will turn on
but when the ball clears the beam motor will stop. I want the motor to run for 5 secs when the beam is broken.

you may need to invert the logic, depending on your sensor logic.

try debugging by adding Serial.print() functions throughout.

const int sensorPin = 2;     // the number of the sensor pin
const int motorPin =  13;      // the number of the motor pin
unsigned long startTime;
int oldState;
//
void setup() 
{
  pinMode(motorPin, OUTPUT);      
  pinMode(sensorPin, INPUT);     
}
//
void loop()
{
  int sensorState = digitalRead(sensorPin);
  if (sensorState == LOW) //inverted
  {
    if (oldState == HIGH)
    {     
      Serial.println("Button Pushed");
      digitalWrite(motorPin, HIGH);   // turns the motor on
      startTime = millis();
    } 
  }
  oldState = sensorState;
  if (digitalRead(motorPin == HIGH))
  {
    if (millis() - startTime >= 5000UL)
    {
      Serial.println("Timer Expired");
      digitalWrite(motorPin, LOW);
    }
  }
}

see what happens. try to notice if the action happens when the ball breaks the beam or makes the beam on its absence...

Ok, I ended up switching sensor with a push button to see if the sensor was the problem.
The push button worked flawless! Exactly what I was looking for! I cant think you enough for
helping me out with code!

Now I just need to figure out why that sensor will not work? Its a new sensor so I know its not bad.
I wired it exactly like the push button but using a external 24volt power supply. When I hook the sensor up
and break the beam it doesn't do anything? Any clue ?

Update
sensor is working and everything running perfect!! Thank you again for all
your help!!!!!!!

Cheers Bulldog
Just what I was looking for.
Karma added