Hi, first of all English is not my native language so pls understand me.
i am very new at programing and arduino.
i built a simple machine and trying to control it with arduino, what machine does is powering 4 selonoid valves at spesific times and after switch presings.
even its a very primative code still, my code works %99 of a time and does what i want to do with it. but some times it freze for a few seconds or misfire wrong valve or startup at ıts own does a couple of cylces and stops. i dont know the problem and hoping you guys can direct me to a solution.
i am using a relay card (to operate valves) which is powerd by arduino which is powerd by 9v adaptor.
void setup()
{
pinMode(A1,INPUT_PULLUP); //start stop
pinMode(A2,INPUT_PULLUP); //second switch
pinMode(A3,INPUT_PULLUP); //first switch
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(10,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
digitalWrite(5,LOW);
}
void loop()
{
if (digitalRead(A1)==LOW) //start stop
{
if (digitalRead(A2)==HIGH) //second switch is not presing
{
digitalWrite(10,LOW); // first valve open
if (digitalRead(A3)==LOW) //first switch
{
delay(50);
digitalWrite(8,LOW); // second valve open
delay(250); // second valve travel time
digitalWrite(7,LOW); // third valve open
if (digitalRead(A2)==LOW) //second switch is presing
{
digitalWrite(10,HIGH); // first valve close
digitalWrite(5,LOW); // third valve open
delay(500); // third valve open travel time
digitalWrite(5,HIGH); // third valve close
digitalWrite(8,HIGH); // second valve close
delay(250); // second valve close travel time
digitalWrite(7,HIGH); // third valve close
}
}
}
}
else
{
digitalWrite(10,HIGH);
digitalWrite(8,HIGH);
digitalWrite(7,HIGH);
digitalWrite(5,HIGH);
}
}
I'd reduce the code to just operating 2 solenoids and see if the problem shows up. If it does you got 1/2 of the problem solved, if not you know to look at the other two solenoids and their code.
Still, having 50 + 250 + 500 + 250 milli seconds worth of delays is a lot of delaying action.
In the Arduino IDE look under examples / digital and find blink without delay. Study that and apply what you learn to what you are trying to do. Your program is almost all delay, it will never work well. You need to learn to use millis for timing.
My eyes glazed over a bit while trying to follow that nest if if()s.... I wonder if it's possible that you're not actually catching the buttons when you expect to.
Suggestion: Write down what's supposed to happen, and let's see if we can work towards a state diagram. Then we can code it up as a state machine with millis() and no delay()s and make it more robust.
But this is scary:
startup at ıts own
With the internal pullups enabled, you should not get spurious lows that would fire up the process.
12Stepper:
My eyes glazed over a bit while trying to follow that nest if if()s.... I wonder if it's possible that you're not actually catching the buttons when you expect to.
Suggestion: Write down what's supposed to happen, and let's see if we can work towards a state diagram. Then we can code it up as a state machine with millis() and no delay()s and make it more robust.
But this is scary:
With the internal pullups enabled, you should not get spurious lows that would fire up the process.
What i want to do with this code is:
When A1 button pushed,
(relays are controling selonoid valves which moves pneumatic cylinders. the delays for the movement time for these cylinders)
energize relay 1 (D10)
wait (lets say) 50 ms
energize relay 2 (D8)
wait 250 ms
energize relay 3 (D7)
deenergize relay 1
energize relay 4 (d5)
wait 500 ms
deenergize relay 4
deenergize relay 2
wait 500 ms
deenergize relay 3
limit switchs are for safety of the machine like if the first cylinder didnt reach position dont energize second one.
... is what should happen, and the limit switches would "break" the cycle so to speak.
One last question:
What happens when it gets to the end (deenergize relay 3)? The loopy nature of loop() means as it stands that it will go back to the top and if A1 is still low, it will go through the whole cycle again. Is that what's supposed to happen? Is A1 a toggle switch or a button? Is it likely to be still low after one cycle?
I thought these were human switches... so as limit switches they are attached to the system?
And this:
energize relay 1 (D10)
wait (lets say) 50 ms
energize relay 2 (D8)
wait 250 ms
energize relay 3 (D7)
deenergize relay 1
energize relay 4 (d5)
wait 500 ms
deenergize relay 4
deenergize relay 2
wait 500 ms
deenergize relay 3
... is what should happen, and the limit switches would "break" the cycle so to speak.
One last question:
What happens when it gets to the end (deenergize relay 3)? The loopy nature of loop() means as it stands that it will go back to the top and if A1 is still low, it will go through the whole cycle again. Is that what's supposed to happen? Is A1 a toggle switch or a button? Is it likely to be still low after one cycle?
yesa1 is toggle switch.
it will loop until a1 toggle high again.
There's one thing you could do to make your code more understandable by others, and that's give the pins human-friendly names. So 8 could be valve2, and A3 could be limit1 etc.
I know they're all explained in the comments, but they could just as easily have better names.
Why are there 4 relays but only 2 limit switches?- I'm trying to get my head round the layout. You mentioned cylinders?
i am using 4 pneumatic cylinders and only need 2 limit switch for the safety reasons. for the other 2 cylinder i am using delay for their movement time.
even its a very primative code still, my code works %99 of a time and does what i want to do with it. but some times it freze for a few seconds or misfire wrong valve or startup at ıts own does a couple of cylces and stops. i dont know the problem and hoping you guys can direct me to a solution.
i am using a relay card (to operate valves) which is powerd by arduino which is powerd by 9v adaptor.
This sounds more like a hardware/power problem to me. Can you provide a hand drawn schematic of your wiring? Can you provide details of the relay card and how it is wired?
cattledog:
This sounds more like a hardware/power problem to me.
That's why I said in #3 that it firing up on its own was "scary" in the light of the start switch having internal pullup enabled, which should prevent spurious lows.
But I'm still keen to help OP get a nice robust state machine here; I'm just not getting a picture of how the mechanical system hangs together, and what the limit switches are checking, and if they start low or high and what they should change to, and what they actually signify.