# How to use millis() to control forward and reverse run time for a linear actuato

Hi,
I have built a h-bridge using mosfets and bjts to control a linear actuator in a solar tracker project. I am using the blink with no delay coding for the time but I can only get forward or reverse to run for the specified time not both. So I am looking for some guidance.

Here is the code I have written:

int sense1Pin = A0; //west leds
int sense2Pin = A1; //east leds
int forward = 9; // use pin 9 for forward
int reverse = 10; // use pin 10 for reverse
int tolerance = 15;
unsigned long previousMillis = 0; // will store last time motor was updated
const long interval_one = 5000; // constant won’t change, interval motor run (milliseconds)
const long interval_two = 10000;
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
analogReference(DEFAULT);
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);

}

void loop(){

unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval_one)
{
previousMillis = currentMillis;

if(((val1 + val2) > 450) && val1 > val2 + tolerance)
digitalWrite(forward, HIGH);
else digitalWrite(forward, LOW);
}

if(currentMillis - previousMillis >= interval_one)

{
previousMillis = currentMillis;

if(((val1 + val2) > 450) && val2 > val1 + tolerance)
digitalWrite(reverse, HIGH);
else digitalWrite(reverse, LOW);

}

if((val1 + val2 < 400) )
digitalWrite(reverse, HIGH);

lcd.setCursor(1,0); // set cursor to column 0, row 0 (first row)
lcd.print(“West=”);
lcd.print(val1);
lcd.setCursor(1,1);
lcd.print(“East=”);
lcd.print(val2);

delay(1000);
delay(1000);

}
// end of code.

When in doubt, read the “how to use this forum” post.

I do not understand your reply, it does not have any coding help.

Here is my code:

``````int sense1Pin = A0; //west leds
int sense2Pin = A1; //east leds
int forward = 9; // use pin 9 for forward
int reverse = 10; // use pin 10 for reverse
int tolerance = 15;
unsigned long previousMillis = 0; // will store last time motor was updated
const long interval_one = 5000; // constant won't change, interval motor run (milliseconds)
const long interval_two = 10000;
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
analogReference(DEFAULT);
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);

}

void loop(){

unsigned long currentMillis = millis();
if(currentMillis - previousMillis >=  interval_one)
{
previousMillis = currentMillis;

if(((val1 + val2) > 450) && val1 > val2 + tolerance)
digitalWrite(forward, HIGH);
else digitalWrite(forward, LOW);
}

if(currentMillis - previousMillis >=  interval_one)

{
previousMillis = currentMillis;

if(((val1 + val2) > 450) && val2 > val1 + tolerance)
digitalWrite(reverse, HIGH);
else digitalWrite(reverse, LOW);

}

if((val1 + val2 < 400) )
digitalWrite(reverse, HIGH);

lcd.setCursor(1,0);   // set cursor to column 0, row 0 (first row)
lcd.print("West=");
lcd.print(val1);
lcd.setCursor(1,1);
lcd.print("East=");
lcd.print(val2);

delay(1000);
delay(1000);

}
// end of code.
``````

Each timed thing needs a separate previousMillis variable, otherwise the first one is going to shield the others.

Thank you for your reply, that is exactly what is happening. Could you elaborate with an example related to my code please.

The demo Several Things at a Time illustrates the use of millis() to manage timing for several things.

...R

Thank you for your suggestions. I gave ever action it"s own previousMillis but to no avail, here is my updated code for your perusal.

``````int sense1Pin = A0; //west leds
int sense2Pin = A1; //east leds
int forward = 9; // use pin 9 for forward
int reverse = 10; // use pin 10 for reverse
int tolerance = 15;
unsigned long currentMillis = 0;
unsigned long previous_F_Millis = 0; // will store last time motor was updated
unsigned long previous_R_Millis = 0;
unsigned long previous_ER_Millis = 0;
const long interval_one = 5000; // constant won't change, interval motor run (milliseconds)
const long interval_two = 10000;
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
analogReference(DEFAULT);
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);

}

void loop(){

unsigned long previous_F_Millis = millis();
if(currentMillis - previous_F_Millis >=  interval_one)
{
previous_F_Millis = currentMillis;

if(((val1 + val2) > 450) && val1 > val2 + tolerance)
digitalWrite(forward, HIGH);
else digitalWrite(forward, LOW);
}

unsigned long previous_R_Millis  = millis();
if(currentMillis - previous_R_Millis >=  interval_one)

{
previous_R_Millis = currentMillis;

if(((val1 + val2) > 450) && val2 > val1 + tolerance)
digitalWrite(reverse, HIGH);
else digitalWrite(reverse, LOW);

}

unsigned long previous_ER_Millis  = millis();
if(currentMillis - previous_ER_Millis >=  interval_two)
{
previous_ER_Millis = currentMillis;
if((val1 + val2 < 400) )
digitalWrite(reverse, HIGH);
}

lcd.setCursor(1,0);   // set cursor to column 0, row 0 (first row)
lcd.print("West=");
lcd.print(val1);
lcd.setCursor(1,1);
lcd.print("East=");
lcd.print(val2);

delay(1000);
delay(1000);

}
// end of code.
``````

Having rechecked my code logic, I found some errors that have now been corrected. So to everyone who responded thank you. If anyone else wants the code for a solar tracker, using a H- bridge, LCD, and linear actuator, here it is.

``````int sense1Pin = A0; //west leds
int sense2Pin = A1; //east leds
int forward = 9; // use pin 9 for forward
int reverse = 10; // use pin 10 for reverse
int tolerance = 15;

unsigned long previous_F_Millis = 0; // will store last time motor was updated
unsigned long previous_R_Millis = 0;
unsigned long previous_ER_Millis = 0;
const long interval_one = 5000; // constant won't change, interval motor run (milliseconds)
const long interval_two = 10000;
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
analogReference(DEFAULT);
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(reverse, OUTPUT);
delay(2000);

}

void loop(){

unsigned long currentMillis = millis();
if(currentMillis - previous_F_Millis >=  interval_one)
{
previous_F_Millis = currentMillis;

if(((val1 + val2) > 450) && val1 > val2 + tolerance)
digitalWrite(forward, HIGH);
else digitalWrite(forward, LOW);
}

if(currentMillis - previous_R_Millis >=  interval_one)

{
previous_R_Millis = currentMillis;

if(((val1 + val2) > 450) && val2 > val1 + tolerance)
digitalWrite(reverse, HIGH);
else digitalWrite(reverse, LOW);

}

if(currentMillis - previous_ER_Millis >=  interval_two)
{
previous_ER_Millis = currentMillis;
if((val1 + val2 < 400) )
digitalWrite(reverse, HIGH);
}

lcd.setCursor(1,0);   // set cursor to column 0, row 0 (first row)
lcd.print("West=");
lcd.print(val1);
lcd.setCursor(1,1);
lcd.print("East=");
lcd.print(val2);

delay(1000);
delay(1000);

}
// end of code.
``````