okay, so here is the problem, i have two motors that can initialize and start just fine but for some reason my code isn't stopping them after given time frame. i would greatly appreciate some help. i will provide the section that runs the motors first and then the entire sketch , something just isn't working right, though there are no errors during compile and upload.
here is the code from the loop:
if (IOvalue = 1000) {
//this is the motor I wish to run for 10 sec
myMotor->setSpeed(255);
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;
serial.print("tock");
}
if (IOvalue = 1000) {
//this is the motor I wish to run for 10 sec
myMotor2->setSpeed(255);
myMotor2->run(FORWARD);
motorStartMillis = millis();
motorRunning = true;
}
// check if motor is running and time-to-run has elapsed
if( motorRunning && (millis()-motorStartMillis > motorRunMillis) )
{
myMotor2->setSpeed(0);
myMotor2->run(RELEASE);
motorRunning = false;
}}
}
here is the entire sketch:
sketch ino
had to use link since the post was too long.
so at the init's, i have this:
int IOvalue = 1000;
boolean motorRunning = false;
unsigned long motorStartMillis;
unsigned long motorRunMillis = 10000;
to run it for 10 seconds(just for a test)
and for some reason, it won't stop the motor even though the condition is set to turn off with this:
// check if motor is running and time-to-run has elapsed
if( motorRunning && (millis()-motorStartMillis > motorRunMillis) )
{
myMotor->setSpeed(0);
myMotor->run(RELEASE);
motorRunning = false;
serial.print("tock");
any help would be appreciated.
marco_c:
== not =
i'm not sure what using an extra = would do since the code doesn't seem to have errors.
using the extra = didn't seem to do anything anyway. and didn't change the operation of the motors, and i still haven't found a working way to make the motors run and stop when a given time has elapsed.
seems this milis stuff just makes things more complicated.
i just want the motor run for x time, turn off and wait for x time before starting over again.
IE: wait for 1500millis
start motor and run for 10 seconds(or set time)
stop motor for 10 seconds(or set time)
wait for set time to start the motors again for the same settings.
= this assigns a value to the variable
== this checks for equality
What are you trying to do?
And yes, the compiler will not complain if you have = instead of ==. Try changing the order around (1000 =ioValue). Mathematically equivalent if you are doing a comparison but a nonsense if you are assigning a value.
The structure of your program is actually incorrect if you are using the millis() method. As it is you are setting up the motor everytime you run the code, so it will never 'wait'. You need to read up on Finite State Machines.
Also, the code fragment does not really help to understand what else is going on that may stop the program from running properly. Errors are rarely where you think they are.
thank you for your replies, would anyone be able to provide some code for running the motors for given period of time, i have reviewed sample codes and wiki's on the use of millis, but i'm still at a standstill as to how to format the sketch and set up the motors to run for a given time then wait and start again.
basically, i want to have the motors run for (example, 10 min, wait 2 hours, run for 10 min)
any help or thoughts?
oh, and by the way, if it makes any difference, i am using the adafruit motor/stepper/servo shield.
OK. Here's a few clues to get you going.
Think of millis() as a timeline. If youwant to do anything for a period of time, you need to remember where you are on the timeline at the start by saving the current value of millis(). Then you continue to compare future current values of millis() to you start time until the difference is greater than or equal to your required activity time period.
The next thing is how to implement a finite state machine. An easy way is to use a switch statement, like this one that I did for another project some time ago. Please note that this is not all the code in that project - just enough to illustrate the point. You see the millis() technique being used to ramp the motor up and down.
// The possible states for the motor
#define MOTOR_BRAKE 0
#define MOTOR_STOP 1
#define MOTOR_RUN 2
#define MOTOR_PRE_RAMP_UP 3
#define MOTOR_RAMP_UP 4
#define MOTOR_PRE_RAMP_DOWN 5
#define MOTOR_RAMP_DOWN 6
loop
{
static uint8_t motorState = MOTOR_BRAKE; // current state for the motor
switch(motorState)
{
case MOTOR_BRAKE:
digitalWrite(IN1,HIGH);
digitalWrite(IN2,HIGH);
digitalWrite(ENA,HIGH);
motorDirection = DIR_UNDEF;
motorSpeedCV = 0;
motorState = MOTOR_STOP;
break;
case MOTOR_STOP:
if ((Command == CMD_FWD) || (Command == CMD_REV))
{
motorDirection = (Command == CMD_FWD) ? DIR_FWD : DIR_REV;
motorState = MOTOR_PRE_RAMP_UP;
}
break;
case MOTOR_PRE_RAMP_UP:
motorSpeedCV = MIN_POWER;
timerRamp = millis();
motorState = MOTOR_RAMP_UP;
break;
case MOTOR_RAMP_UP:
if (millis()-timerRamp < RAMP_UP_TIME)
{
motorSpeedCV = MIN_POWER + (((motorSpeedSP-MIN_POWER) * (millis()-timerRamp)) / RAMP_UP_TIME);
}
else
{
motorSpeedCV = motorSpeedSP;
StateTransition(motorState, MOTOR_RUN);
}
SetSpeed(motorDirection, motorSpeedCV);
break;
case MOTOR_RUN:
if (Command == CMD_STOP)
{
motorState = MOTOR_PRE_RAMP_DOWN;
}
break;
case MOTOR_PRE_RAMP_DOWN:
timerRamp = millis();
motorState = MOTOR_RAMP_DOWN;
break;
case MOTOR_RAMP_DOWN:
if (millis()-timerRamp < RAMP_DOWN_TIME)
{
motorSpeedCV = motorSpeedSP - (((motorSpeedSP-MIN_POWER) * (millis()-timerRamp)) / RAMP_DOWN_TIME);
}
else
{
motorState = MOTOR_BRAKE);
}
SetSpeed(motorDirection, motorSpeedCV);
break;
}
}