Hello there,
I'm trying to program two linear actuators to operate at the same time and stop when they reach a preset current threshold using a single toggle switch. They are meant to stop independently of each other. While testing with the following code, they would stop as desired, but when I try to raise them, the motor skips and I think the issue is the delay but I don't know how to fix it.
/*
Making inputs D2 or D3 HIGH opens or closes both top and bottom linear actuators. In "open" mode, motors
operate as long as the "open" switch is enabled. In "close" mode, the motors operate as long as the "close"
switch is enabled OR until a preset current limit is reached. If the current limit is reached, a motor can
no longer be operated in the "close" mode until it is reset by briefly enabling the "open" switch. (In both
directions the motors will automatically stop when they reach "end-of-travel" limits.)
*/
const int open = 2;
const int close = 3;
int LA_open = LOW;
int LA_close = LOW;
float T_thresh = A6; //current cutoff threshold for top actuator, set by pot P2. (CW increases force)
float B_thresh = A4; //current cutoff threshold for bottom actuator, set by pot P1. (CW increases force)
float T_trip = 0; //top actuator current trip point variable
float B_trip = 0; //bottom actuator current trip point variable
float T_curr_mon = A7; //input from top actuator current monitor
float B_curr_mon = A5; //input from bottom actuator current monitor
float T_curr_in = 0; //input current variable for top actuator
float B_curr_in = 0; //input current variable for bottom actuator
void setup() {
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(12, OUTPUT);
pinMode(6, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(open, INPUT_PULLUP);
pinMode(close, INPUT_PULLUP);
T_curr_in = 0;
B_curr_in = 0;
}
void loop() {
LA_open = digitalRead(open); //check state of "open/close" switch
if (LA_open == HIGH) { //if "open" switch is actuated, start both top and bottom actuator motors
digitalWrite(5, LOW); //to four inputs of top-actuator H-bridge
digitalWrite(4, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(12, LOW); //to four inputs of bottom-actuator H-bridge
digitalWrite(6, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
delay(100); //hold this state for 100 msec, then turn everything off
digitalWrite(5, LOW); //to four inputs of top-actuator H-bridge
digitalWrite(4, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(12, LOW); //to four inputs of bottom-actuator H-bridge
digitalWrite(6, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
T_curr_in = 0; //reset value for top input current variable
B_curr_in = 0; //reset value for bottom input current variable
}
T_trip = analogRead(T_thresh); //read top current-limit potentiometer
if (T_curr_in <= T_trip) { //if top input current is less than the current limit,
//update top input current value
T_curr_in = analogRead(T_curr_mon); //read top current monitor value
}
LA_close = digitalRead(close); //check state of "open/close" switch
if (LA_close == HIGH && T_curr_in < T_trip) { //if "close" switch is actuated and top input
//current < top threshold, close top actuator
digitalWrite(5, HIGH); //to four inputs of top-actuator H-bridge
digitalWrite(4, LOW);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
}
B_trip = analogRead(B_thresh); //read bottom current-limit potentiometer
if (B_curr_in <= B_trip) { //if bottom current is less than the current limit,
//update bottom input current value
B_curr_in = analogRead(B_curr_mon); //read bottom current monitor value
}
if (LA_close == HIGH && B_curr_in < B_trip) { //if "close" switch is closed and bottom input
//current < bottom threshold, close bottom actuator
digitalWrite(12, HIGH); //to four inputs of bottom-actuator H-bridge
digitalWrite(6, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
}
delay(100); //hold this state for 100 msec, then turn everything off
digitalWrite(5, LOW); //to four inputs of top-actuator H-bridge
digitalWrite(4, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(12, LOW); //to four inputs of bottom-actuator H-bridge
digitalWrite(6, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
Any help would be greatly appreciated. Thank you for your time