Wheres my voltage? (what am I doing wrong?)

holmes4:
Add this or somthing very like it to your code

// add the following

unsigned long motorStart = 0;
boolean motorOn = false;
int motorRuntime = 200;// length of time to run motor in millis

void doMotor(){
 // motor control stuff
 int curTime = 0;
 if (motorOn){
   curTime = millis();
   if (curTime-motorStart>motorRuntime){
     motorOn = false;
     digitalWrite(motorPin, LOW);
 }
}

void startMotor(){
   digitalWrite(motorPin, HIGH);
   motorStart = millis();
   motorOn = true;
}




replace your "digitalWrite(motorPin, HIGH);" calls to startMotor();

and at the start of loop add a call to doMotor();

This should do the job.



Mark

Thanks..

I like the idea.

(kinda like an auto shut-off.... a watch dog/timer for when the motor should be turned off)

I just tried to implement it.... works great the first 10 or so times.. then it seems to execute the motor off portion.. immediately.

Not sure if this has to do with clearing out some var/timer...or what..

I'll try poking around and messing with it more.. seems like a great idea to solve my problem

thanks

edit:

seems outside of the missing bracket, just casting curTime as a unsigned long seemed to work..

great idea again..

:slight_smile: