# Setting a time limit?

Hi
Quite New to programming and the arduino ( 2nd day in!) What I am trying to do is use the arduino to run a small motor for inflatables. I want to put a time constraint in that if the motor has been running for x amount of time it cuts out. I am struggling with the time command? From what I have read I am guessing it has to do with the millis function? but would really appreciate any help?

thanks

Rez

The most common way to determine the passage of time on the Arduino is the millis function. This returns the number of milliseconds since the system started. If you store the value of millis when you start your motor you can easily calculate how long its been running. I hope the following gives you some ideas about how to add the desired functionality to your sketch:

#define CUTOUT (1000 * 60 * 10) // cut out in 10 minutes
// this is the number of milliseconds to cutout – 1000 is ms per second, 60 is mins per second, 10 is number of minutes

// code to start motor here
unsigned long startTime = millis();
//following is example code to wait for button press to stop motor – replace with your code
while( digitalRead(buttonPin) == HIGH){ // wait for button to go low
if( millis() – startTime > CUTOUT) // check if CUTOUT ms has elapsed since motor started
break; // exit the while loop
}
// code to stop motor here

Good luck!

Thanks Mem,

Still a bit shaky on this have added it in where i think appropriate. If you dont mind going to post the programme I have written so far for comments feedback? Will be testing it when components arrive tomorrow!

Thanks

Rez

• Turns on and off a valve (pin 13) and starts the motor (pin4) inflating air bladders
• Both will be activated by the QTC cord effectively a potentiometer (pin2).
• There is a pressure sensor (pin5) in the bladder. The pressure sensor will
• cut the motor and shut the valve when the pressure > then a set amount or
• the motor and the valve have been on for longer than 10 mins.

*/

int QTCPin = 2; // select the input pin for the potentiometer [one side on the conducter]
int PressPin = 2; // select the input pin for the pressure sensor [inside the bladder]
int ValvePin = 13; // select the output pin for the Valve
int motpin = 4; // select the output pin for the Motor [check if this is a suitable output pin????]
int QTCval = 0; // variable to store the value coming from the potentiometer
int Pressval = 0; // variable to store the value coming from the pressure sensor
int QTCstate = 0; // variable to hold the last state from the QTC Cord
int Cutout // Cutout time

void setup() {
pinMode(ValvePin, OUTPUT); // declare the ValvePin as an OUTPUT
pinMode(motPin, OUTPUT); // declare the MotorPin as an OUTPUT
pinMode(QTCPin, INPUT); // declare the QTCPin as an INPUT
pinMode(PressPin, INPUT); // declare the PressPin as an INPUT
}

void loop() {

if (QTCval != QTCstate) { // the QTC cord has been pulled and thus the state has changed
if (QTCval == HIGH) { // check if the Value of the QTC is high [check if it is high when stretched]
digitalWrite(ValvePin, HIGH); // turn the Valve on
digitalWrite(Motpin , HIGH); // turn the Motor on

Cutout (1000 * 60 * 10) // cut out in 10 minutes - this is the number of milliseconds to cutout – 1000 is ms per second, 60 is mins per second, 10 is number of minutes
unsigned long startTime = millis();
while( digitalRead(MotPin) == HIGH){ // wait for button to go low
if( millis() – startTime > CUTOUT) // check if CUTOUT ms has elapsed since motor started
break; {//
digitalWrite(ValvePin, LOW); // Valve is off
digitalWrite(motpin , LOW); } // Motor is off

} else {

digitalWrite(ValvePin, LOW); // Valve is off
digitalWrite(motpin , LOW); // Motor is off
}
QTCstate= QTCval;
}
void loop(){
Pressval= digitalRead (Presspin) // This is a pressure cut off if the pressure gets too high
if (Pressval > 512) // Need to specify number
digitalWrite(ValvePin, LOW); // Valve is off
digitalWrite(motpin , LOW); // Motor is off
}

rez, the code is a little hard to follow because the formatting has been lost – its best to use the code blocks (see the # icon )

You have two loop functions- you can only have one.
Not sure if you want QTCval to be an analog or digital value. If analog you should not compare it to HIGH. If digital than you need to do a digital read.

I suggest you start off with a simpler sketch that just reads the input (from a pot or switch) and lights an LED.

Get that going and then you can add a time function. And when that's going you can add in the code to actually integrate with your devices.

have fun!

Thanks Mem

Make sense. I will make those adjustments and then test it part by part.

Rez