How to control the time that the motor runs

Hello, i have a itead l298 dual h-bridge motor driver
http://www.ptrobotics.com/2341-itead-l298-dual-h-bridge-motor-driver.html

and i need to control the motor that is inside of this linear actuator
http://www.aliexpress.com/snapshot/6076827430.html

Here is is the part of the code i am using to test the actuator .

//enabling motor A
 Serial.println ("Enabling Motor A");
 digitalWrite (enableA, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinA1,LOW);
 digitalWrite (pinA2,HIGH);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableA, LOW);
 delay (2000);

My problem is to control the time that the motor goes forward and backwards.
I also want to control the velocity but is not so important at this time.

Thank you

Andreee99:
My problem is to control the time that the motor goes forward and backwards.

That is what the delay() calls are doing. You can change the delays to a different constant or change them to variables that you calculate in your sketch.

Yes, but i cant change the time, i try to put 10.000 instead of 5000 and it goes forward and backwards the same time as erlier.

It sims that the arduino reads the same time allways.

Hi Andre

Can you please post your complete code?

And have you tried adding more debug println statements, to see if / when the program gets to the reverse and stop sections?

All the best

Ray

Andreee99:
Yes, but i cant change the time, i try to put 10.000 instead of 5000 and it goes forward and backwards the same time as erlier.

After you change the sketch does it compile and upload without error?

Yes it runs and upload without errors , so i am thinking that the motor is damage or not working ok, because the problem is not the code.

//Testing the DC Motors
//Define Pins
 //Motor A
 int enableA = 10;
 int pinA1 = 2;
 int pinA2 = 3;
//Motor B
 int enableB = 8;
 int pinB1 = 4;
 int pinB2 = 5;
/*-----( Import needed libraries )-----*/
/*-----( Declare Constants )-----*/
#define RELAY_ON 1
#define RELAY_OFF 0
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  7  // Arduino Digital I/O pin number

void setup()   /****** SETUP: RUNS ONCE ******/
{
// Atuadores
  Serial.begin (9600);
 //configure pin modes
 pinMode (enableA, OUTPUT);
 pinMode (pinA1, OUTPUT);
 pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
 pinMode (pinB1, OUTPUT);
 pinMode (pinB2, OUTPUT);
  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  delay(1000); //Check that all relays are inactive at Reset
  // Buzer
 
  pinMode(9, OUTPUT);   // sets the pin as output


}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  // Teste atuadores
 //enabling motor A
 Serial.println ("Enabling Motor A");
 digitalWrite (enableA, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
//5s forward
 delay (5000);
//reverse
 digitalWrite (pinA1,LOW);
 digitalWrite (pinA2,HIGH);
//5s backwards
 delay (5000);
//stop
 digitalWrite (enableA, LOW);
 delay (2000);
//enabling motor B
 //Since motor B is mounted reversed, PINs must be exchanged
 Serial.println ("Enabling Motor B");
 digitalWrite (enableB, HIGH);
//do something
 //forward
 Serial.println ("Forward");
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
//5s forward
 delay (1000);
//reverse
 digitalWrite (pinB1,HIGH);
 digitalWrite (pinB2,LOW);
//5s backwards
 delay (1000);
//stop
 digitalWrite (enableB, LOW);
 delay (500); 
//---( Turn all 4 relays OFF in sequence)---  
  
  
  
   analogWrite(9,128);
     delay(500);
     digitalWrite(9, HIGH);
     delay(100);
     
     
     digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
  delay(400);              // wait for a second
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
  delay(900);              // wait for a second    
        
}//--(end main loop )---

No ,i have not try and sory i dont know what is that, I am not realy using the "println" for nothing , but i dont know if i can delete it .

thank you , sory the bad english

can you make a simple schematic ?

for testing, you can comment out the print statements. they are there to send text to your screen to tell you what mode you are in.

Serial.println (“Forward”); // this is just to send out text to tell you the state the program is in.
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
//5s backwards
delay (5000); // think this is being used to run the motor ?

digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(400); // wait for a second
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
delay(900); // wait for a second

try changing the delay values for the relays. if they are the devices in control then these would change the time.
without a schematic, it is only guessing, but you said changing the delay in PinA1 had no effect, look here maybe ??

Hello , and thanks for your help.

Yes i know that the prints is to write the state on a screen orsomething like that but in thois case in not using any screen so i dont use the prints for nothing.

Ok, in the relays i can control the delay perfectly , i change the time and it works fine.

but in the other case( to control the linear actuator) i cant control the time so i think that the motor is damage .

Other thing i want to do is to make my code run only 3 times? can i do a counter of cycles in the program?

Thank you

Thank you very much, thank´s to you i discover that the engine has a problem and not the program.

Good Projects :smiley: