Motor Drive with Potentiometer Delay

Hello all!

i'm a total newbie on Arduino and of course i have a question for you helpful people :slight_smile:

in my project i have two dc motors on L293D and here is what i want to make and what's going on:

i want to run these two motors sequentally like 1 sec motor A then 1 sec motor B, but when i set a delay time with pot it stucks to the value till the loop ends although i change the duration value. all i want is an immediate delay time change. i've tried to control using "for" and i manage to read pot value for short intervals but couldn't make the system work.

thanks for further help

I assume you want to write some NON-BLOCKING code.
but we will need you to post your CODE showing the issue …

sorry here it is

void setup()
{
 pinMode(A0,INPUT); //motor speed control
 pinMode(A1,INPUT); //duration control
 Serial.begin(9600);
 }
void loop()
{
  int rate=analogRead(A0);
  rate = map(rate,0,1023,0,255);
  analogWrite(3,rate);
  digitalWrite(4,LOW);
  digitalWrite(2,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(7,HIGH);
  
  int duration=analogRead(A1);
  duration = map(duration,0,1023,1000,6000);  //1000 and 6000 are just imaginary delay times
  Serial.println(duration);
   
//motor a  
  digitalWrite(4,LOW);
  digitalWrite(2,LOW);
  digitalWrite(8,LOW);
  digitalWrite(7,HIGH);
  delay(duration);
//motor b
  digitalWrite(4,HIGH);
  digitalWrite(2,LOW);
  digitalWrite(8,LOW);
  digitalWrite(7,LOW);
  delay(duration);
    }

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time

...R

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

but when i set a delay time with pot it stucks to the value till the loop ends although i change the duration value.

Yes that is exactly what you have written the code to do, so no surprise it does it.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Grumpy_Mike:
Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

thanks for correction.

i messed up with my for loop, so i wrote down the basic code to give an example

I didn’t see a pin for speed control of motor B so I just picked pin #6.

/* MotorWithDelay
 * By Duane Degn 
 * November 1, 2015
 */
const int IN1_MOTOR_A = 4;
const int IN2_MOTOR_A = 2;
const int IN1_MOTOR_B = 8;
const int IN2_MOTOR_B = 7;
const int ENABLE_MOTOR_A = 3;
const int ENABLE_MOTOR_B = 6; // I'm guessing
const int BETWEEN_MOTOR_DELAY = 2000; // I'm guessing
const int MIN_DELAY = 1000;
const int MAX_DELAY = 6000;
//unsigned long potReadTime;
//unsigned long endActionTime;
//duration;
  
void setup()
{
 pinMode(IN1_MOTOR_A, OUTPUT); 
 pinMode(IN2_MOTOR_A, OUTPUT); 
 pinMode(IN1_MOTOR_B, OUTPUT); 
 pinMode(IN2_MOTOR_B, OUTPUT); 
 pinMode(A0,INPUT); //motor speed control
 pinMode(A1,INPUT); //duration control
 Serial.begin(9600);
 }
void loop()
{
  MonitorMotor(IN1_MOTOR_A, IN2_MOTOR_A, ENABLE_MOTOR_A);
  delay(BETWEEN_MOTOR_DELAY);
  MonitorMotor(IN1_MOTOR_B, IN2_MOTOR_B, ENABLE_MOTOR_B);
  delay(BETWEEN_MOTOR_DELAY);
}

void MonitorMotor(int in1, int in2, int enable)
{
  unsigned long duration;
  unsigned long startTime = millis();
  do
  {
    int rate = analogRead(A0);
    rate = map(rate, 0, 1023, 0, 255); // Use this line for positive rates.
    //rate = map(rate, 0, 1023, -255, 255); // Use this line to allow negative rates.
    duration = analogRead(A1);
    duration = map(duration, 0, 1023, MIN_DELAY, MAX_DELAY); 
    SetMotor(in1, in2, enable, rate);
    Serial.print("rate = ");
    Serial.print(rate);
    Serial.print(", total duration = ");
    Serial.print(duration);
    Serial.print(", time remaing = ");
    Serial.println(duration - (millis() - startTime));
     
  }
  while (millis() - startTime < duration);  
}
  
void SetMotor(int in1, int in2, int enable, int power)
// "power" may be positive or negative.
// A negative power value will cause the motor to reverse.
{
  if (power > 0)
  {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }  
  else if (power == 0) // This will cause motor to brake. 
  {
    digitalWrite(in1, LOW); // Comment out these two lines
    digitalWrite(in2, LOW); // to coast to a stop.
  } 
  else 
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    power *= -1; // Set power to positive since motor has been set to reverse.
  }     
  analogWrite(enable, power);
}

There’s a two second delay between motors being powered on. You can change the amount of time with this constant.

const int BETWEEN_MOTOR_DELAY = 2000;

I used the same range for the maximum and minimum motor run times. Again, if you want to change these times, you can do so with these constants.

const int MIN_DELAY = 1000;
const int MAX_DELAY = 6000;

I wasn’t sure if you wanted to be able to adjust the speed of the motors while they were running but I decided it would be interesting to be able to adjust both the speed and the duration on the fly so I included calculating the rate inside the loop which monitors the motor.

I’m relatively new to Arduinos but not programming and microcontrollers. I’m not sure if I have all the syntax correct or not. The program compiles but I haven’t tested it.

I hope you let me know if it works as expected or not.