0

I have a single simple DC motor that upon a condition I wish to let it run for 3 minute. How can I do it in Arduino? a simple addition to the code will be great help.

As I use Adafruit motor shield this is my current code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

void setup() {
   Serial.begin(9600);           // set up Serial library at 9600 bps
   AFMS.begin();  // create with the default frequency 1.6KHz
}

void loop() {
IOvalue = 1000

if (IOvalue = 1000) { 

 //this is the motor I wish to run for 3 min 
   myMotor->setSpeed(25); 
   myMotor->run(FORWARD); 
   delay(100); 
 }  
}

Thanks a lot

1
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
boolean motorRunning = false;
unsigned long motorStartMillis;
unsigned long motorRunMillis = 180000;

void setup() {
   Serial.begin(9600);           // set up Serial library at 9600 bps
   AFMS.begin();  // create with the default frequency 1.6KHz
}

void loop() {
  IOvalue = 1000

  if (IOvalue = 1000) {
    //this is the motor I wish to run for 3 min 
    myMotor->setSpeed(25); 
    myMotor->run(FORWARD); 
    motorStartMillis = millis();
    motorRunning = true;
  }

  // check if motor is running and time-to-run has elapsed
  if( motorRunning && (millis()-motorStartMillis > motorRunMillis) )
  {
    myMotor->setSpeed(0); 
    myMotor->run(RELEASE);
    motorRunning = false;
  }
}
-1
long int currentMillis;
void setup() {
    pinMode(motorPin, OUTPUT);
    digitalWrite(motorPin, LOW);
}
void loop() {
    if(condition is met){
        startOfMotor = millis();
        while(millis() != (startOfMotor+180000)){
            digitalWrite(motorPin, HIGH);
        }
    digitalWrite(motorPin, LOW);
    }
}

This motorPin is the pin to which the motor is connected. millis() gets the milliseconds after the board is started. 3 min = 180 seconds = 180000 ms.

  • Thanks. I forgot to mention that I use Adafruit motor shield and therefore how can I integrate you code into this snippet. Code: void loop() { ….. …. if (IOvalue = 1000) { //this is the motor I wish to run for 3 min myMotor->setSpeed(25); myMotor->run(FORWARD); delay(100); } } – dkin Apr 25 '15 at 8:29
  • Can you edit your question to show the code? It is hard to read it in comments. – Prithvish Baidya Apr 25 '15 at 8:36
  • About the shield.. I need to know more about your setup. – Prithvish Baidya Apr 25 '15 at 8:38
  • Updated the question and embedded the code. – dkin Apr 25 '15 at 9:01
  • I have no idea about the Adafruit motor shield. Sorry! (For such simple operations you are better off using the Arduino itself. ) – Prithvish Baidya Apr 25 '15 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.