Go Down

Topic: need help with code for controlling a hydraulic press mode (Read 9697 times) previous topic - next topic

neofral

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  :D

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  :smiley-red:

Please take your time and help me with this :)

Thank you very much beforehand.

PaulS

Quote
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.

Quote
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?

Quote
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.

Quote
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.

The art of getting good answers lies in asking good questions.

PaulS

Code: [Select]
  if (digitalRead(P1) == LOW) HP1 = 1;
  else HP1 = 0;

In other words,
Code: [Select]
   HP1 = !digitalRead(P1);

Code: [Select]
  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.

Code: [Select]
  boolean button_S1 = handle_S1();
  boolean button_S2 = handle_S2();
  boolean button_S3 = handle_S3();

Oops, make that 6.

Code: [Select]
  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?
The art of getting good answers lies in asking good questions.

Bainesbunch

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.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

neofral

sorry for the lack of information  :smiley-red:
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.

neofral


Quote
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.

Quote
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?

Quote
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.

Quote
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?

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.

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

neofral

#7
May 03, 2014, 09:55 pm Last Edit: May 03, 2014, 10:00 pm by neofral Reason: 1

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 :)

neofral

thanks again for the support :)

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 :)

Code: [Select]

#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;
           }      
        }  
 
}

PaulS

Code: [Select]
  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.
The art of getting good answers lies in asking good questions.

neofral

#10
May 05, 2014, 08:23 pm Last Edit: May 05, 2014, 09:04 pm by neofral Reason: 1

Code: [Select]
 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  :D
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 + 2*HS2 + 3*HS3. 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  :smiley-red:

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

neofral

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 http://arduino.cc/en/Reference/Constants . 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:
Code: [Select]

#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 :)

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.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

neofral

#14
May 09, 2014, 09:28 am Last Edit: May 09, 2014, 09:52 am by neofral Reason: 1

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?

Go Up