Arduino with 2 relay with time delay

Hi. I'm trying to make a machine with an air piston that goes up - down in intervals set by 2 potentiometer.

For example: Press buton-> relay is turn on and piston is down, it holds the piston down x value ( input is from a potentiometer that the arduino reads value from 0sec to 120sec with map function)-> second relay turns on but still the first relay is turn on and after time y value (the same of the above)-> then release both relays at the same time.

I made a very simple sketch with delay function but I want a STOP Emergency button to release both relays quickly.
Because i have put the delay() before i cannot stop the system from turning off the relays expect the reset button from the board.But it stil takes 2-3 sec at boot time for the arduino.
Can someone help or guide me.
Thanks

#define RelayAir 9
#define RelayPiston 8
#define BTNPiston 13

const int potPin = A0;
const int potPin2 = A1;
int btn = 0;
int buttonState;
int value;
int value2;
unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 10;

int lastButtonState = LOW;

void setup() {
    Serial.begin(9600); 
   
    pinMode(RelayAir, OUTPUT);
    pinMode(RelayPiston, OUTPUT);
    pinMode(BTNPiston, INPUT);
}

void loop() {
    btn = digitalRead(BTNPiston);

    if(btn != lastButtonState) {
        lastDebounceTime = millis();
    }
    if((millis() - lastDebounceTime) > debounceDelay) {
        buttonState = btn;
        if(buttonState == LOW)
        {
            value = analogRead(potPin);     
            value = map(value, 0, 1023, 0, 120);   
            digitalWrite(RelayPiston, HIGH);       
            delay(value * 1000); 
            value2 = analogRead(potPin2); 
            value2 = map(value2, 0, 1023, 0, 120);  
            digitalWrite(RelayAir, HIGH);       
            delay(value2 * 1000);   
            digitalWrite(RelayPiston, LOW);
            digitalWrite(RelayAir, LOW);                                 
        }
    }
    delay(10);
}

delay() and millis()
never a good combination

Never, never, NEVER depend on software to operate an emergency stop. It should be an external hardware switch that cuts power.

2 Likes

I second that. And I want to describe in more detail how this emergency-stop should be wired:

It should be wired as normally closed switch. Pushing the energency-stop shall open to interrupt the powersupply directly

The reason is: whenever the wiring is broken this will have the same effect as pushing the emergeny-stop: interrupting the power-supply
Huh ? why is my machine not working ???
Find the error.

If you would use a normally closed switch a broken wire would not be detected.
And in case of a real emergency the emergency-stop would not even work.

Be the change you want to see in the world
best regards Stefan

I understant what you're saying but i need the emergency buton when pressed to lift up the piston.If i cut the power off the piston just stays down.
If there is no way of software stop then i will just choose the hardware method and rig the relay piston to go up when the buton is pressed.

If this is a real emergency the machine should be able to create a state with all power off that is as safe as possible. If there are parts that can fall down there should be emergency brakes that are opened (= not braking) as long as power is on and stop all parts from falling down as soon as power is off because the power-on-opening changes to closed = brake active.

If your machine is constructed in a way that a person could get injured through a malfunction while normally using the machine you would never get an approving for the machine in the european union.

If the piston stays down with power off. Turn the hole cyclinder 180 degrees and gravity will pull the piston to the right position.

But may be you you mean something different. If it is not a real emergency but a bring back machine into a starting state if something else does not work.
Sure this is possible.

It would be very good if you post a picture of how all this is looking like.

Be the change you want to see in the world
best regards Stefan

If it can do that without power, okay.

Can this machine cause an injury? Yes, or no? Because the whole approach to safety depends a lot on the answer.

If the answer is yes, do you have a safety interlock, for example, a lock out unless the operators hands are clear? You need that in addition to the emergency stop.

This is diy bonding machine for displays so safety is not an issue.
The stop button is for the piston to go up quickly.

This

Otherwise, see reply #2.


You should use a PLC. What is the force on the piston? You're asserting that it's not strong enough to hurt an operators fingers or hands?

the presure is 3 Mpa.
http://fsrkj.com/upfiles/201712/22/a966296d53b685a26.pdf

That's air pressure. Edit - sorry - bonding pressure. The resulting force from some certain air pressure depends on the size of the piston. You didn't answer the question. The spec you posted says nothing about the piston force. It's about the display bonding. So how do you regulate the piston force? pascals are relative to area, so 3pa on a pin head would puncture your skin, 3pa on a plate wouldn't.

Anyway, reply #2 has your software solution.

You could have programmed a PLC in the time it took to post and read here... and it would be more reliable because the Arduino is not EMI resistant.

If I understand your code right.
There are two potentiometers for adjusting times.

To start pressing you push a button you switch on the relay that makes the piston move down.

the piston shall stay at lower-position for certain time

if this waiting time is over switch on relay that does "something with air" whatever this means.
again wait some time with both relays switched on
if the second waiting time is over switch both relays off

While the waiting-time is passing by you want to have a stop-button which will switch off both relays at any time. Even when the first waiting-time has just started.

I can post a code that uses the coding-tecnique called a state-machine under one condition:

You post a very detailed description of what the piston does
you post a very detailed description of what the "air" does
you post a very detailed description of what happens while the first waiting-time is passing by
ypu post a very detailed description of what happends while the second waiting-time is passing by.

I would like to have a very clear picture of the complete process.

The reason is:
self-explaining names for everything
the variable-names, the function-names

Be the change you want to see in the world
best regards Stefan

I get your point, but...

thanks guys for the reply.
I will go with hardware stop.
@StefanL38 the air is for cooling the heating element and display when its pressed down.
btw plc is expensive for me.

This tell at least a few things about your machine not very detailed but tells a little bit.
I will try to explain the sequence of steps that your machine is doing.

certain steps might be wrong because you told just a tiny little bit about the process, though I asked for a very detailed description

The software-tecnique of a statemachine mimics a PLC.
state-machine means you are in certain modes of operation:

  1. machine ready to start waiting for button-press to start
  2. applying air-pressure for the piston to go down
  3. waiting for the piston to arrive at lower position
  4. switching on air-cooling
  5. holding piston down with air-cooling on for a certain time
    if pressuring-time is over
  6. switch off air-cooling, switch off air-pressure to piston

In parallel to this read in the position of two potentiometers to adjust time and temperature.

A state-machine allows to avoid using delay() which does what its name says: delaying. The function delay() delays everything. Even the reaction on a button-press.

A state-machine in combination with non-blocking timing keeps your control responsive all the time. Even in the situation of keeping a certain mode like "hold pressure"

Curious to learn about how this works?

Here is a tutorial about non-blocking timing
Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()

and here is a tutorial about how state-machines work

best regards Stefan

Does that mean, the picture of the machine you showed us, is just some commercial machine that you don't actually have? If so, it would have been a good idea to mention it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.