Hi,
I have a setup to control the position of 3 linear actuators. I have three rotary potentiometers that I use to request a position for each motor and each motor has a potentiometer inside to reference its actual position.
Currently, I am using three if statements and each if statement goes as follows:
if the motor has a higher position than the requested position
move the motor in
else if the motor has a lower position that the requested position
move the motor out
else
do nothing
The sketch does work but I have the issue that the motors can sometimes overshoot and go past their requested position.
This has me thinking whether using the if statement is the effective thing to do. I think that what is happening is, that if a motor needs to be moved then it enters that motors if statement and begins moving the motor. The sketch then goes onto the next two if statements and then back to the original. At this point it has overshot and is now using the else if part of that request and moving the motor back in its original direction, resulting in the motor moving backwards and forwards in front of and past its requested position.
Is there a term which I can use that forces the sketch to satisfy a statement (the motor being in its correct position) before it continues with checking the requested position of the other two motors?
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(53, 4, 3, 52, 2, 51);
#define Pot_In_In A10
#define Pot_In_Ex A11
#define Pot_In_EGR A12
#define Pot_Motor_In A13
#define Pot_Motor_Ex A14
#define Pot_Motor_EGR A15
#define In_Motor_PWM 8
#define In_Arm_In 23
#define In_Arm_Out 22
#define Ex_Motor_PWM 7
#define Ex_Arm_In 24
#define Ex_Arm_Out 25
#define EGR_Motor_PWM 6
#define EGR_Arm_In 26
#define EGR_Arm_Out 27
#define Motor_Moving_LED 12
int In_Motor_Power = 80;
int Ex_Motor_Power = 60;
int EGR_Motor_Power = 70; //needs to be 42 min
const int Motor_Accuracy = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(20, 4);
dac.begin(); //Initalise i2C
dac.vdd(5000);
pinMode(Pot_In_In, INPUT);
pinMode(Pot_In_Ex, INPUT);
pinMode(Pot_In_EGR, INPUT);
pinMode(Pot_Motor_In, INPUT);
pinMode(Pot_Motor_Ex, INPUT);
pinMode(Pot_Motor_EGR, INPUT);
pinMode(Motor_Moving_LED, OUTPUT);
digitalWrite(In_Arm_In, LOW);
digitalWrite(In_Arm_Out, LOW);
digitalWrite(Ex_Arm_In, LOW);
digitalWrite(Ex_Arm_Out, LOW);
digitalWrite(EGR_Arm_In, LOW);
digitalWrite(EGR_Arm_Out, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("In-Req:");
lcd.setCursor(12, 0);
lcd.print("Act:");
lcd.setCursor(0, 1);
lcd.print("Ex-Req:");
lcd.setCursor(12, 1);
lcd.print("Act:");
lcd.setCursor(0, 2);
lcd.print("EGR-Re:");
lcd.setCursor(12, 2);
lcd.print("Act:");
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
int sensor_Value_In_In = analogRead(Pot_In_In);
int Percentage_In_In = map(sensor_Value_In_In, 0, 1023, 0, 100);
int sensor_Value_In_Ex = analogRead(Pot_In_Ex);
int Percentage_In_Ex = map(sensor_Value_In_Ex, 0, 1023, 0, 100);
int sensor_Value_In_EGR = analogRead(Pot_In_EGR);
int Percentage_In_EGR = map(sensor_Value_In_EGR, 0, 1023, 0, 100);
int sensor_Value_Motor_In = analogRead(Pot_Motor_In);
int Percentage_Motor_In = map(sensor_Value_Motor_In, 0, 1023, 0, 100);
int sensor_Value_Motor_Ex = analogRead(Pot_Motor_Ex);
int Percentage_Motor_Ex = map(sensor_Value_Motor_Ex, 0, 1023, 0, 100);
int sensor_Value_Motor_EGR = analogRead(Pot_Motor_EGR);
int Percentage_Motor_EGR = map(sensor_Value_Motor_EGR,0, 1023, 0, 100);
lcd.setCursor(7, 0);
lcd.print(Percentage_In_In);
lcd.print("% ");
lcd.setCursor(16, 0);
lcd.print(Percentage_Motor_In);
lcd.print("% ");
lcd.setCursor(7, 1);
lcd.print(Percentage_In_Ex);
lcd.print("% ");
lcd.setCursor(16, 1);
lcd.print(Percentage_Motor_Ex);
lcd.print("% ");
lcd.setCursor(7, 2);
lcd.print(Percentage_In_EGR);
lcd.print("% ");
lcd.setCursor(16, 2);
lcd.print(Percentage_Motor_EGR);
lcd.print("% ");
if (abs(Percentage_Motor_In - Percentage_In_In) >= Motor_Accuracy)
{
if (Percentage_Motor_In > Percentage_In_In)
{
analogWrite(In_Motor_PWM, In_Motor_Power);
digitalWrite(In_Arm_In, HIGH);
digitalWrite(In_Arm_Out, LOW);
lcd.setCursor(0, 3);
lcd.print("Intake Opening ");
}
else if (Percentage_Motor_In < Percentage_In_In)
{
analogWrite(In_Motor_PWM, In_Motor_Power);
digitalWrite(In_Arm_In, LOW);
digitalWrite(In_Arm_Out, HIGH);
lcd.setCursor(0, 3);
lcd.print("Intake Closing ");
}
}
else
{
digitalWrite(In_Arm_In, LOW);
digitalWrite(In_Arm_Out, LOW);
}
if (abs(Percentage_Motor_Ex - Percentage_In_Ex) >= Motor_Accuracy)
{
if (Percentage_Motor_Ex > Percentage_In_Ex)
{
analogWrite(Ex_Motor_PWM, Ex_Motor_Power);
digitalWrite(Ex_Arm_In, HIGH);
digitalWrite(Ex_Arm_Out, LOW);
lcd.setCursor(0, 3);
lcd.print("Exhaust Opening ");
}
else if (Percentage_Motor_Ex < Percentage_In_Ex)
{
analogWrite(Ex_Motor_PWM, Ex_Motor_Power);
digitalWrite(Ex_Arm_In, LOW);
digitalWrite(Ex_Arm_Out, HIGH);
lcd.setCursor(0, 3);
lcd.print("Exhaust Closing ");
}
}
else
{
digitalWrite(Ex_Arm_In, LOW);
digitalWrite(Ex_Arm_Out, LOW);
}
if (abs(Percentage_Motor_EGR - Percentage_In_EGR) >= Motor_Accuracy)
{
if (Percentage_Motor_EGR > Percentage_In_EGR)
{
analogWrite(EGR_Motor_PWM, EGR_Motor_Power);
digitalWrite(EGR_Arm_In, HIGH);
digitalWrite(EGR_Arm_Out, LOW);
lcd.setCursor(0, 3);
lcd.print("EGR Closing ");
}
else if (Percentage_Motor_EGR < Percentage_In_EGR)
{
analogWrite(EGR_Motor_PWM, EGR_Motor_Power);
digitalWrite(EGR_Arm_In, LOW);
digitalWrite(EGR_Arm_Out, HIGH);
lcd.setCursor(0, 3);
lcd.print("EGR Opening ");
}
}
else
{
analogWrite(EGR_Motor_PWM, 0);
digitalWrite(EGR_Arm_In, LOW);
digitalWrite(EGR_Arm_Out, LOW);
lcd.setCursor(0, 3);
lcd.print(" ");
EGR_State = 1;
}
}