need help with code for controlling a hydraulic press mode

THANK YOU ALL VERY MUCH for your help.

i finally finished the project. i used an Arduino Leonardo, 2 rotary switches, and a custom made board for 2 relays, a 24DC relay.

  • multiple position rotary switches for setting the number of steps in each mode, photo1.
  • board with 2 relays since i couldn't find a single relay with 3 positions. i used 2 to give me closed circuit for each position of the switch on the press that controls in which mode the press operates, photo 2.
  • 24DC relay for giving input to arduino whenever the press is on up/ready position.

i have set up the arduino to use the INPUT_PULLUP method on all inputs so on integer level i read 0 for LOW, and 1 for HIGH. as explained here constants - Arduino Reference . i found this method to be very simple since i can keep the things simple and just send ground to each pin that i want to read input.

sorry for not preparing in detail on the DIY. i think that the information provided will provide the necessary explanation on how the solution worked out. when i have time i will be preparing a step-by-step DIY and post it.

and here is the code:

#define P1        2            //defining inputs of the positions on the rotary switch
#define P2        3
#define P3        4
#define P4        5
#define S1        6
#define S2        7
#define S3        8
#define S4        9
const int buttonPin = 12;       //the input to read the position of the press
const int ledPin = 13;          //test led
const int s = 10;               //relay to position mode
const int p = 11;               //relay to pressure mode
int buttonPushCounter;   
int buttonState;         
int lastButtonState = HIGH;     
int HP1;
int HP2;
int HP3;
int HP4;
int HS1;
int HS2;
int HS3;
int HS4;
int stepspressure;
int stepsposition;
int stepsposition1;
int stepspressure1;
long lastDebounceTime = 0;
long debounceDelay = 200;


void setup(){
  pinMode(P1, INPUT);
  pinMode(P2, INPUT);
  pinMode(P3, INPUT);
  pinMode(P4, INPUT);
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(S4, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(s, OUTPUT);
  pinMode(p, OUTPUT);
  digitalWrite(P1, HIGH); 
  digitalWrite(P2, HIGH);
  digitalWrite(P3, HIGH);
  digitalWrite(P4, HIGH);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  digitalWrite(S4, HIGH);
  digitalWrite(buttonPin, HIGH);
  Serial.begin(9600);
  delay(4000);
  Serial.println("Hello Sami");
}

void loop(){ 
  int  HP1 = !digitalRead(P1);
  int  HP2 = !digitalRead(P2);
  int  HP3 = !digitalRead(P3);
  int  HP4 = !digitalRead(P4);
  int  HS1 = !digitalRead(S1);
  int  HS2 = !digitalRead(S2);
  int  HS3 = !digitalRead(S3);
  int  HS4 = !digitalRead(S4);
  int stepsposition = HS1 + 2*HS2 + 3*HS3 + 4*HS4;
  int stepspressure = HP1 + 2*HP2 + 3*HP3 + 4*HP4;
  if (stepspressure1 == 0 && stepsposition1 == 0){
    stepsposition1 = stepsposition;
    digitalWrite(p, HIGH);}

  buttonState = digitalRead(buttonPin);
    if ((millis() - lastDebounceTime) > debounceDelay) {
      if (buttonState != lastButtonState) {
        if (buttonState == LOW) {
          if (stepspressure1 < stepspressure){
            stepspressure1++;
            digitalWrite(p, HIGH);
            Serial.print("pressure ");
            Serial.println(stepspressure1);
              if (stepspressure1 == stepspressure){
                stepsposition1 = 0;
                digitalWrite(p, LOW);
                digitalWrite(s, HIGH);          
              }           
            }
            if (stepsposition1 < stepsposition){
              stepsposition1++;
              digitalWrite(s, HIGH);      
              Serial.print("p ");
              Serial.println(stepsposition1);
                if (stepsposition1 == stepsposition){
                  stepspressure1 = 0;
                  digitalWrite(s, LOW);
                  digitalWrite(p, HIGH);
                }  
            }
            }
            lastDebounceTime = millis();
            lastButtonState = buttonState;
            }
         }  
  
}

please feel free to ask any additional information. i'll answer as soon as i have time :slight_smile:

1.jpg

relay schematic.jpg