need help with code for controlling a hydraulic press mode

Hi all,

i'm using Arduino Leonardo and want to make a hydraulic press for pressing either by position limit sensor or pressure sensor. the press has an indicator light when the press is in up possition before pressing. i want to use that light as step counting and attach 2 switches to the Arduino board to set how many steps to use the position sensor or pressure sensor.

The big issue is that i'm not a programmer but like to create stuff and learn new things. normally i take my time and read and spend as much time as necessary to achieve my goal, but this time i need the solution ASAP so that's why i need your help.

the best way you can help me is to provide me with the full code :smiley:

i have successfully attached the switches and i can successfully read how many steps in each mode.
e.g.: i would like 3 steps each so i have attached the pins 2->7 using pullup method.
next, i have created a small switch that converts the 50V/AC (of the presses indicator light) to 5.6V/DC to use as the trigger for counter and next step on the arduino (i can use pin 8 for this). and at the end i need to attach a micro switch to attach to the presses switch to the switch that controls whether to use position sensor or pressure sensor).

so, i have all the components needed for this project. the only thing remains the code for the arduino.
i'm also attaching my current progress in coding :blush:

Please take your time and help me with this :slight_smile:

Thank you very much beforehand.

Press.ino (3.08 KB)

the best way you can help me is to provide me with the full code

No problem. I'll get right on that, as soon as you send me the press.

i would like 3 steps each so i have attached the pins 2->7 using pullup method.

Attached what to the 6 pins? Why do you need to use 6 pins to read one light and two switches?

next, i have created a small switch that converts the 50V/AC (of the presses indicator light) to 5.6V/DC

A switch can't do that.

and at the end i need to attach a micro switch to attach to the presses switch to the switch that controls whether to use position sensor or pressure sensor).

I read that three times. After the third reading, it still made no sense.

  if (digitalRead(P1) == LOW) HP1 = 1;
  else HP1 = 0;

In other words,

   HP1 = !digitalRead(P1);
  boolean button_P1 = handle_P1();
  boolean button_P2 = handle_P2();
  boolean button_P3 = handle_P3();

Pass an argument to ONE function - the pin number. Don't write three nearly identical functions.

  boolean button_S1 = handle_S1();
  boolean button_S2 = handle_S2();
  boolean button_S3 = handle_S3();

Oops, make that 6.

  buttonState = digitalRead(buttonPin);

Switches work so much better than buttons. The names do not contribute to understanding what the code does. Sure, you are reading a pin, but there is no clue as to why. What is connected to that pin? FOR WHAT PURPOSE?

Also you need to have some de-bounce on the counter pin if it is a mechanical switch or you will count the contact bounces as well giving false readings.

sorry for the lack of information :blush:
i also apologize for the code. i know it might even be totally inappropriate for my intention but this is where i got so far by reading examples on the internet. as i said earlier, i'm not a programmer but i like to learn.

i will try and make more clear my intentions. i have a hydraulic press that works in 2 modes. it can use position switch that when the piston is pressing until x position, the press returns up. the second mode is with a pressure sensor that controls the piston until it reaches x amount of pressure than go up. so the up position should be the time when i can manipulate the switch on the press for using each sensor for the next step.

since the light indicating that the cylinder is in up/rest position gives 50VAC, i will need to convert this to 5VDC so i can read in a pin that the value is HIGH. i got parts and instructions for making this conversion from the guy at the electronics parts store where i also bought the arduino. i will weld the items of the first photo in a form of a mini shield and use as the input for the presses up position.

on the second photo i'm using 2 rotary switches to help me set the amount of steps in each mode of the press (pressure or position). i want to be able to change the amont of steps as i work so that's why i need these switches that i can mount on the box that will hold the arduino.

the third photo is the micro switch that will enable me to bypass the switch on the press to give command to the presses board in which mode to do the next step.

sorry for the delay on the return of information, i had some urgent matters to attend to.

DSC_0021_1.jpg

PaulS:

the best way you can help me is to provide me with the full code

No problem. I'll get right on that, as soon as you send me the press.

i would like 3 steps each so i have attached the pins 2->7 using pullup method.

Attached what to the 6 pins? Why do you need to use 6 pins to read one light and two switches?

next, i have created a small switch that converts the 50V/AC (of the presses indicator light) to 5.6V/DC

A switch can't do that.

and at the end i need to attach a micro switch to attach to the presses switch to the switch that controls whether to use position sensor or pressure sensor).

I read that three times. After the third reading, it still made no sense.

did you check my answer?

Hi.

As the others tried to point out, it's important to use the correct names for the items you are using, or else the people trying to help you do not understand what you're talking about.
A microswitch looks like this:

(grabbed from tamiya website).
You are showing a relay, which is a completely different part.

Also, 50 volts AC is unsafe to connect to your Arduino.
The person at the shop you bought this stuff from, has built you a rectifier to make DC from the AC, using a diode and a capacitor.
Then he reduced the voltage by a huge (in size) 5 Watt, 6800 Ohm resistor and a zener diode.
Did he explain what this all does, and more important, do you understand that.
If not, what compels you to use this piece of hardware that you do not understand ?
In this case a relay would be a better choice, maybe with help of a resistor in case you can't find a 50 volts version.

MAS3:
Hi.

As the others tried to point out, it's important to use the correct names for the items you are using, or else the people trying to help you do not understand what you're talking about.
A microswitch looks like this:

(grabbed from tamiya website).
You are showing a relay, which is a completely different part.

Also, 50 volts AC is unsafe to connect to your Arduino.
The person at the shop you bought this stuff from, has built you a rectifier to make DC from the AC, using a diode and a capacitor.
Then he reduced the voltage by a huge (in size) 5 Watt, 6800 Ohm resistor and a zener diode.
Did he explain what this all does, and more important, do you understand that.
If not, what compels you to use this piece of hardware that you do not understand ?
In this case a relay would be a better choice, maybe with help of a resistor in case you can't find a 50 volts version.

first of all, thank you for your support. i think i should have mentioned that i don't hold a degree in electronics. i will accept any suggestion to change items that i use in order to achieve my goal.

now, as per the rectifier... i asked the guy to give me a relay for this, it was his idea for this and said that this is much simpler if i only need 5VDC to send to the arduino. i didn't connect this to the arduino yet since i had the breadboard and simulating input as i'm trying to get around with the code

EDIT: now i think i should have made some schematics and post pictures of the items and apologize for the lack of correct identifying of the items. sorry for that. but i think with the pictures i posted you can identify items and suggest replacement if needed :slight_smile:

thanks again for the support :slight_smile:

so far i got the loops to count using state change detection method on the examples from the Aurduino IDE.

however, when "stepspressure" reaches the limit, it immediately counts the first step of the| stepsposition" condition.

i think that i'm not setting the limits or the trigger right. any help will be highly appreciated :slight_smile:

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


void setup(){
  pinMode(P1, INPUT);
  pinMode(P2, INPUT);
  pinMode(P3, INPUT);
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(s, OUTPUT);
  pinMode(p, OUTPUT);
  digitalWrite(P1, HIGH); 
  digitalWrite(P2, HIGH);
  digitalWrite(P3, HIGH);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, 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  HS1 = !digitalRead(S1);
  int  HS2 = !digitalRead(S2);
  int  HS3 = !digitalRead(S3);
  int stepsposition = HS1 + 2*HS2 + 3*HS3;
  int stepspressure = HP1 + 2*HP2 + 3*HP3;

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

Given that HS1, HS2, and HS3 can be 0 or 1, how can you distinguish between HS1 = 1, HS2 = 1 and HS3 = 0 and HS1 = 0, HS2 = 0, and HS3 = 1? Both result in stepsposition being assigned the value of 3.

PaulS:

  int stepsposition = HS1 + 2*HS2 + 3*HS3;

Given that HS1, HS2, and HS3 can be 0 or 1, how can you distinguish between HS1 = 1, HS2 = 1 and HS3 = 0 and HS1 = 0, HS2 = 0, and HS3 = 1? Both result in stepsposition being assigned the value of 3.

i know it's complicated, that's why i've been scratching my head for over a week :smiley:
the rotary switches give only 1 input to one of the pins with value = 1 per set of three. as i said before i have 2 groups of steps from 1 to 3. i forgot to mention, i'm using PULL_UP method to read the input. so the pins get connection to GND to have value =1.
so, in the void loop the reading is successful setting the correct reading for stepspressure stepsposition= HS1 + 2HS2 + 3HS3. it gives me a value of 1 to 3 for stepspressure stepsposition.

the incrementing of the count also is going well. but in the first loop until stepspressure1=stepspressure, it counts both. after the reseting of the value of stepspressure1=0, it restarts the counting....

so, the first loop is going both ways. i might have a problem with that, but since it's only a few steps i'll use the manual switch on the machine to start about 10 steps before i switch to Arduino take control.

EDIT: sorry for the confusion. i mixed the integers. i corrected with a strike-through :blush:

i know it's complicated

But, it's not. A single switch is pressed or not. That's a 0 or 1 in one bit of a binary number. The three bits then look like:
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7
If the columns represent switch states for switches 3, 2, and 1, you can see that pressing switch 3 contributes 4 to the value. Not pressing it contributes 0 to the value. Similarly, pressing switch 2 contributes 2 to the value. Not pressing it contributes 0 to the value. And, pressing switch 1 contributes 1 to the value. Not pressing it contributes 0 to the value. So, your multipliers should be 1, 2, and 4, not 1, 2, and 3.

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

Oh no !

Please get rid of the mistakes ans errors before you do a write up of this DIY project, or else you'd better upload it to instructables.
That site is a horrible way of showing how not to do things.

Why all this:
There's no diodes in sight near your relays.
Do not do that.

MAS3:
Oh no !

Please get rid of the mistakes ans errors before you do a write up of this DIY project, or else you'd better upload it to instructables.
That site is a horrible way of showing how not to do things.

Why all this:
There's no diodes in sight near your relays.
Do not do that.

i know that i didn't do a proper write of this project since the start. that's because i neither hold a degree on electronics nor i had the time needed to straighten the things.

what's the point in saying something it's not correct without showing how is the right way?

Here's some points.
First of all, it's supposed to set you to do the research that you should have done earlier, now.

Second:
This is something that is told in about every post around here, where people are told how to use relays.
Because leaving those out will do serious damage sooner or later.

And last but not least:
You will remember this next time.

Research 'protection diode', using search function.

I think your main problem is that you have picked a project application that is far beyond your present experience level in both basic electronics and programming. If you really wish to do this for yourself rather then pay someone to deliver a solution you need to back up and pick more basic projects so you have a chance of acquiring the necessary experience. The arduino IDE has many many example sketches that show you how to perform basic tasks and are easy to wire up to basic input and output devices.

It's as if you don't yet know how to drive a car but your project is to build a formula one race car and enter a race. And asking for help with this specific project is bound to give you advice that you won't understand and in many cases will not be the help you actually need.

Sorry, but my advice is to lower your expectations and learn to walk before you run.

MAS3:
Research 'protection diode', using search function.

thank you very much vor the advice. and this is more constructive posting on a colaboration thread :slight_smile:
i'll look into it ASAP and modify the relay board.

retrolefty:
It's as if you don't yet know how to drive a car but your project is to build a formula one race car and enter a race. And asking for help with this specific project is bound to give you advice that you won't understand and in many cases will not be the help you actually need.

Sorry, but my advice is to lower your expectations and learn to walk before you run.

didn't you read any of my posts? i'm sure you'll find there somewhere mention of the use of the examples from the Arduino IDE.

oh by the way, thank you for taking your time and posting on this thread.
firstly, i didn't know that this page is used to procure people/solutions. sorry, another of my mistakes.
and secondly, whatever you call it (formula 1) or just a driving... - didn't you read that the project is finished?

And that is exactly where the problem is and why you are suddenly getting less positive responses.
Your project is not finished at all, it just happens to do what you wanted it to do for now.
It will fail and you have now been warned for that.
In case it fails, what would be the consequences ?
The hydraulic system overloading or worse ?
What about any people near that machine trying to control it ?

You can and should prevent that failure before you can call this project finished.
Would it have been a school project, you would not have achieved anything close to an A grade.