Motor run time

Hi guys

Please help me out here.

Id like to run a motor for 5 seconds on a single push of a switch, (without holding down the button for 10 seconds)

What command do i need ?

What code have you got so far - can you read the switch? drive the motor? understand BlinkWithoutDelay
example?

Have look at the demo several things at a time. It is an extended example of BWoD

Basically you need to record millis() when the button is pressed and the motor is started and then keep checking to see if the 5 seconds has elapsed.

...R

Building off what Robin2 said, try this

const int button1Pin =  13;      
int buttonState = HIGH;             
unsigned long previousMillis = 0;        // will store last time BUTTON was updated
const long interval = 5000;           // interval TO RUN MOTOR

void setup() {
  // SET UP YOUR MOTOR HERE
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.
  unsigned long currentMillis = millis();
buttonState = digitalRead(button1Pin);
 if (buttonState == LOW)
 {
    RUN(motor); //you have replace with your code to run the motor here
 }
  if(currentMillis - previousMillis >= interval) 
  {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    STOP(motor); // replace with your code to stop the motor here
  }
}
/[code]
[quote author=MouseMan link=msg=2350136 date=1439146876]
Id like to run a motor for 5 seconds on a single push of a switch, (without holding down the button for 10 seconds)  
What command do i need ?
[/quote]

Pranesh, you really should edit your post to put that code into [ code ] tags and while you're doing that you can fix it so that the code actually works. Your current code doesn't ever read the button pin and even if it did, it causes the motor to stop every 5 seconds, not 5 seconds after the button was pushed.

I believe that is what he/she wanted. Press button and motor runs for 5 seconds.
And thanks for the

 comment.

[quote author=MorganS link=msg=2350319 date=1439154940]
Pranesh, you really should edit your post to put that code into [ code ] tags and while you're doing that you can fix it so that the code actually works. Your current code doesn't ever read the button pin and even if it did, it causes the motor to stop every 5 seconds, not 5 seconds after the button was pushed.
[/quote]

No, it doesn't run for 5 seconds. Imagine if you've just started the Arduino 4 seconds ago, so previousMillis is zero and currentMillis is 4000. Now push the button. [You need to add code to read the button.] The motor runs. Great!

One second later, currentMillis is 5000, so the interval condition is met and the motor is stopped. The motor was only running for 1 second. Not great.

Hi guys thanks for the help.
so i got this thing working but i need to get rid of the delay function and assume use the millis instead

here is the code im currently running

void setup() {
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(10, OUTPUT);
pinMode(3, INPUT_PULLUP);
pinMode(11, OUTPUT);

}

void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
int sensorVal1 = digitalRead(3);

//print out the value of the pushbutton
Serial.println(sensorVal);

// Keep in mind the pullup means the pushbutton's
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(10, HIGH);
delay(3000);
}
else {
digitalWrite(10, LOW);
}

if (sensorVal1 == HIGH) {
digitalWrite(11, HIGH);
delay(3000);
}
else {
digitalWrite(11, LOW);
}
}

MouseMan:
Hi guys thanks for the help.
so i got this thing working but i need to get rid of the delay function and assume use the millis instead

I gave you a link to several things at a time in Reply #2

...R