Hello Everyone and thank you. I have a project that uses a joystick to control some pneumatic cylinders. My problem is that I get oscillations of the cylinders around the desired position without stopping at the desired position sometimes. (see graph) I have tried PID loops which were really complicated and could not get to work. I am a noobie and would love any suggestions to try. Thank you.
/* This program uses an Arduino Uno and compares operator input from a control potentiometers on a joystick
vs. positional potentiometers on air cylinders to actuate relays that move valves that move the pneumatic cylinders.
*/
//---------------------constants (don't change)---------------------------
const byte ESWITCHPIN = 2;
const byte PORTPOTPIN = A1;
const byte JOYWHEELPIN = A2;
const byte RELAYPIN7 = 8;
const byte RELAYPIN3 = 12;
const byte RELAYPIN4 = 13;
//-------------------------variables (will change)---------------------
int PortPot = 0;
int PotDiff = 0;
int JoyWheel = 0;
unsigned long previousMillis = 0;
//-----------------------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(ESWITCHPIN, INPUT); //estop switch
pinMode(RELAYPIN3, OUTPUT);//relay#3
pinMode(RELAYPIN4, OUTPUT);//relay#4
pinMode(RELAYPIN7, OUTPUT);//relay#7
}
//================================================================================
void loop() {
digitalRead(ESWITCHPIN);
while (digitalRead(ESWITCHPIN) == HIGH) {
digitalWrite(RELAYPIN7, LOW);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 30) {
previousMillis = currentMillis;
updatePots();
updateSolenoids();
}
}
digitalWrite(RELAYPIN7, HIGH);
}
//==================================================================================
void updatePots() {
analogRead(PORTPOTPIN);
PortPot = analogRead(PORTPOTPIN); //Port cylinder potentiometer
delay(2);
JoyWheel = analogRead(JOYWHEELPIN); //Joystick wheel potentiometer
delay(2);
PortPot = map(PortPot, 215, 916, 0, 500);
JoyWheel = map(JoyWheel, 1, 1023, 0, 500);
PotDiff = (PortPot - JoyWheel);
Serial.print(JoyWheel);
Serial.print(",");
Serial.println(PortPot);
}
//===================================================================================================
void updateSolenoids() {
if (PotDiff >= 30) {
digitalWrite(RELAYPIN3, HIGH);
digitalWrite(RELAYPIN4, LOW);
}
else if (PotDiff <= -30) {
digitalWrite(RELAYPIN3, LOW);
digitalWrite(RELAYPIN4, HIGH);
}
else {
digitalWrite(RELAYPIN3, HIGH);
digitalWrite(RELAYPIN4, HIGH);
}
}
Untitled 1.pdf (37.2 KB)