Hello Everyone. This is my first program and I would love criticism, help, suggestions, etc. It uses an Uno to control a robot arm with a joystick.
/* This program uses an Arduino Uno at compares operator input from a potentiometer joystick to control the position of air cylinders via PWM proportional valves with
* a positive feedback loop from potentiometers on the air cylinders.
*/
int aft=0;
int port=0;
int diff=0;
int diff1=0;
int wheel=0;
int brake=0;
int throttle=0;
unsigned long previousMillis = 0;
void setup() {
pinMode(4,INPUT); //estop switch
pinMode(5, OUTPUT);//PWM Valve#1
pinMode(6, OUTPUT);//PWM Valve#2
pinMode(10, OUTPUT);//Switch#1
pinMode(11, OUTPUT);//Switch#2
pinMode(12, OUTPUT);//Switch#3
pinMode(13, OUTPUT);//Switch#4
//these are not needed only for reference
// pinMode(A0,INPUT);//aft cylinder potentiometer Input
//pinMode(A1,INPUT); //port cylinder potentiometer Input
//pinMode(A2,INPUT); /throttle joystick control input
//pinMode(A3,INPUT);//brake joystick control input
//pinMode(A4,INPUT);// wheel joystick control input
digitalWrite(10,HIGH);//High Turns off 4 chananel relays - LOW to turn on
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void loop() {
while(digitalRead(4)==LOW){
digitalWrite(10,HIGH);//High Turns off 4 chananel relays - LOW to turn on
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}//if switch is low do nothing
analogRead(A0);
aft =analogRead(A0); //read first analog input potentiometer twice to settle DAC
delay(2);
port =analogRead(A1);//potentiometer
delay(2);
brake=analogRead(A2);//potentiometer
delay(2);
throttle=analogRead(A3);//potentiometer
delay(2);
wheel=analogRead(A4); //potentiometer
/*wheel pot goes 0-850
* brake joystick 728-849
* gas joystick 700-850
* aft pot pneumatic cylinder 300-900
* port pot penumatic cylinder 389-975
*/
port=constrain(port,389,975);//needed to constrain actual max & min numbers come from the pots
port=map(port,389,975,0,500);
wheel=constrain(wheel,54,966);
wheel=map(wheel,54,966,0,500);
aft=constrain(aft,300,850);
aft=map(aft,300,850,-250,250);
brake=constrain(brake,671,862);
brake=map(brake,671,862,-250,250);
throttle=constrain(throttle,755,852);
throttle=map(throttle,755,852,-250,250);
unsigned long currentMillis = millis();//this is a timing loop because the relays should fire no more than 20x/s.
if (currentMillis - previousMillis >= 50) {
previousMillis = currentMillis;
/*below we take the difference from where we are (cylinder pots) to where we want to be (joystick pots) and
send it to the PWM valves. The valves are proportional so the bigger the
difference the wider the PWM valves open. It is a non-linear equation so writing it
with multiple if statements is easier to adjust in the field.
/////////////////////////Left and Right//////////////////////////////
*/
diff=(port-wheel);
if(diff>200){digitalWrite(12,HIGH);digitalWrite(13,LOW);analogWrite(6,150);}
if(diff>100 && diff<=200){digitalWrite(12,HIGH);digitalWrite(13,LOW);analogWrite(6,120);}
if(diff>20 && diff<=100){digitalWrite(12,HIGH);digitalWrite(13,LOW);analogWrite(6,100);}
if(diff<=20 && diff>=-20){digitalWrite(12,HIGH);digitalWrite(13,HIGH);analogWrite(6,0);}//this is called the deadband
if(diff<-20 && diff>=-100){digitalWrite(12,LOW);digitalWrite(13,HIGH);analogWrite(6,100);}
if(diff<-100 && diff>=-200){digitalWrite(12,LOW);digitalWrite(13,HIGH);analogWrite(6,120);}
if(diff<-200){digitalWrite(12,LOW);digitalWrite(13,HIGH);analogWrite(6,140);}
/////////////////throttle and brake//////////////////////////////////////////////////
diff1=(brake + aft - throttle);
if(diff1>200){digitalWrite(10,HIGH);digitalWrite(11,LOW);analogWrite(5,125);}
if(diff1>100 && diff1<=200){digitalWrite(10,HIGH);digitalWrite(11,LOW);analogWrite(5,90);}
if(diff1>20 && diff1<=100){digitalWrite(10,HIGH);digitalWrite(11,LOW);analogWrite(5,80);}//extend or brake (recover from go fast)
if(diff1<=20 && diff1>=-20){digitalWrite(10,HIGH);digitalWrite(11,HIGH);analogWrite(5,0);}
if(diff1<-20 && diff1>=-100){digitalWrite(10,LOW);digitalWrite(11,HIGH);analogWrite(5,80);}
if(diff1<-100 && diff1>=-200){digitalWrite(10,LOW);digitalWrite(11,HIGH);analogWrite(5,90);}//retract or go fast
if(diff1<-200){digitalWrite(10,LOW);digitalWrite(11,HIGH);analogWrite(5,120);}//retract or go fast
}
}