1

I'm working on my own project with Arduino Uno. There are controlled 4 pneumatic cylinders via 4 coils. Every pneumatic cylinder has got their own coil which switch air to the pneumatic cylinder. Every pneumatic cylinder has got 2 magnetic sensors (when the pneumatic cylinder is retracted and extended). I created a program for arduino but it doesn't work correctly. When I write there that I want to control only 2 pneumatic cylinders, it works correctly. If I write there the others, it skip the second if statement, do the third if statement and then do the previous if. I don't know why. The behaviour is random. What's wrong with my code? Thank you for help!

// Constants for pneumatic valves
   const int dvere = 2;
   const int davkovani = 3;
   const int lisovani = 4;
   const int vyhazovani = 5;
// ----------------------------------------
// Constants for sensors
   const int vyhazovaniZasunute = 6;
   const int vyhazovaniVysunute = 7;
   const int lisovaniVysunute = 8;
   const int lisovaniZasunute = 9;
   const int dvereZasunute = 10;
   const int dvereVysunute = 11;
   const int davkovaniVysunute = 12;
   const int davkovaniZasunute = 13;


void setup() {
  // Pneumatic cylinders set as output
  pinMode(dvere, OUTPUT);    
  pinMode(davkovani, OUTPUT);    
  pinMode(lisovani, OUTPUT);    
  pinMode(vyhazovani, OUTPUT);    

  // Sensors set as input
  pinMode(vyhazovaniZasunute, INPUT);   
  pinMode(vyhazovaniVysunute, INPUT);   
  pinMode(lisovaniVysunute, INPUT);   
  pinMode(lisovaniZasunute, INPUT);   
  pinMode(dvereZasunute, INPUT);   
  pinMode(dvereVysunute, INPUT);  
  pinMode(davkovaniVysunute, INPUT);  
  pinMode(davkovaniZasunute, INPUT);  

  // Set pneumatic cylinders as they are retracted
  digitalWrite(dvere, HIGH);
  digitalWrite(davkovani, HIGH);
  digitalWrite(lisovani, HIGH);
  digitalWrite(vyhazovani, HIGH);
}

void loop() {

  if(digitalRead(dvereZasunute) == HIGH)
  {
      digitalWrite(dvere, LOW);      // extend the first pneumatic cylinder when the sensor is lighted on because the pneumatic cylinder is retracted
  }

  if(digitalRead(dvereVysunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH
  )
  {
      digitalWrite(davkovani, LOW);      // extend the second pneumatic cylinder
      delay(4000);
      digitalWrite(davkovani, HIGH);    // retract the second pneumatic cylinder after 4 seconds, 
      // this code works when it is alone there, the next if statements not
  }

  if(digitalRead(dvereVysunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH && digitalRead(lisovaniZasunute) == HIGH)
  {
      digitalWrite(lisovani, LOW);    // extend the third pneumatic cylinder
      delay(3000);
      digitalWrite(lisovani, HIGH);   // retract the secondthird pneumatic cylinder after 3 seconds
  }

  if(digitalRead(dvereVysunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH && digitalRead(lisovaniZasunute) == HIGH)
  {
      digitalWrite(dvere, HIGH);      // retract the first pneumatic cylinder
  }

  if(digitalRead(dvereZasunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH && digitalRead(lisovaniZasunute) == HIGH && digitalRead(vyhazovaniZasunute) == HIGH)
  {
      digitalWrite(vyhazovani, LOW);    // extend the fourth pneumatic cylinder
  }

  if(digitalRead(dvereZasunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH && digitalRead(lisovaniZasunute) == HIGH&& digitalRead(vyhazovaniVysunute) == HIGH)
  {
      digitalWrite(vyhazovani, HIGH);    // retract the fourth pneumatic cylinder
  }

  // sensors - HIGH - light on, LOW - light off,
  // pneumatic cylinders - HIGH - retracted, LOW - extended.
}
  • This is what I refer article. But, the language may be barrier for you to read. There is an alternative method this article's author mentions a library, TimeAction.h. The library has the same method as I mentioned. You can google it. Otherwise, the "interrupt controlling" about Arduino you may interest. Please refer to the relative articles. I don't know your practical hardware architecture. Here just want to provide what I know information for you. Hope these are helpful. TimeAction.h [interrupt contrlling](arduino.cc/en – jefferyear Aug 25 '16 at 0:21
  • If you're using multiple conditions, i.e. &&, you need to watch the parentheses. – Tom M Aug 25 '16 at 11:23
  • this code works - please define "works". What actually happens? – Nick Gammon Aug 25 '16 at 20:56
  • The pneumatic cylinder takes time to extend/retract. How do you manage that? – user31481 Jul 25 '17 at 8:59
  • can i get your configuration on board. I am designing same project but with single solenoid valve. Please share your Circuit configuration if possible. – Sanket Salunkhe Jan 22 '18 at 7:45
1
void loop() {

...

  if(digitalRead(dvereVysunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH
  )
  {
      digitalWrite(davkovani, LOW);      // extend the second pneumatic cylinder
      delay(4000);
      digitalWrite(davkovani, HIGH);    // retract the second pneumatic cylinder after 4 seconds, 
      // this code works when it is alone there, the next if statements not
  }

...
}

If the condition is met, then davkovani is going to be LOW 99.999% of the time. It will only briefly go HIGH, as there is no delay there. The loop will iterate and set it LOW again. So it may seem to be "not working" whereas the problem is that it "works" very briefly.

  • Why the delay doesn't work there? – d_pawe Aug 30 '16 at 18:43
  • 1
    The delay works, but you need two delays. You need: LOW / delay / HIGH / delay if you are going to have any hope of noticing the HIGH state. – Nick Gammon Aug 30 '16 at 21:46
0

Your code's variable names must be meaningful. You mention "2 magnetic sensors (when the pneumatic cylinder is retracted and extended)"-I guess it is solenoid valve. I hope I don't guess wrongly. This help us to understand your code. Otherwise, do you have any procedure diagram-it is like square and prismatic shape to illustrate your programing and its sequence.

  • Yeah, they are solenoid valves. You're right. – d_pawe Aug 18 '16 at 10:15
  • Hello, Sorry for that I just see you have comment on this program, so I review the code. Otherwise, I have a bit longer not to contact Arduino. What I say it's just for a reference. I wish I can help you. – jefferyear Aug 19 '16 at 1:56
  • Hello, Sorry for that I just see you have comment on this program, so I review the code. Otherwise, I have a bit longer not to contact Arduino. What I say it's just for a reference. I wish I can help you. Your evert statements are all clear. The problem you mention it's on loop function. I see on No.4 if statement the "dvere" this pneumatic valve is extended. But, the No.1 if statement is already on LOW to extend. I also see in No.4 statement you add a logical operation on "lisovaniZasunute" this sensor if it is "HIGH" level. No.3 and No.4 if statement are same description on its writing. – jefferyear Aug 19 '16 at 2:07
  • I continue to say that the No.3 and No.4 statement is same as its condition. if(digitalRead(dvereVysunute) == HIGH && digitalRead(davkovaniZasunute) == HIGH && digitalRead(lisovaniZasunute) == HIGH){} Do you want to use a nest loop to write your code? The No.5 and No.6 are similar to NO.3 and No.4.. – jefferyear Aug 19 '16 at 2:08
  • Yeah, in the if statement no. 4 should be HIGH, because I need retract the pneumatic valve. Statements no. 5 and no. 6 aren't the same. The last operation is different there. – d_pawe Aug 19 '16 at 13:41
0

Hi~ The synchronized method is below. The function millis() you can refer Arduino.org. It is a function to show you current time. Here I use it to create a time interval to run synchronization.

Declaration the variables are below:

/* Declaration time interval variables for synchronizing pneumaticValve1 and pneumaticValve2. The interval is 1000ms.  */ 
long perviousTimepneumaticValve1 = 0; 
long perviousTimepneumaticValve2 = 0; 
long previousTimeLocal = 0; 
long interval = 1000; 

The loop() function is below:

void loop() { 
checkPneumaticValve1(); 
checkPneumaticValve2(); 
} 

void checkPneumaticValve1() { 
  if (millis()-previousTimeLocal>interval) {
    printPneumaticValve1(); 
    previousTimeLocal=millis();
   }
 }

void printPneumaticValve1() { 
     //Here write your code for PneumaticValve1
   }

void checkPneumaticValve2() { 
   if (millis() - previousTimeLocal > interval) {
     printPneumaticValve2(); 
     previousTimeLocal=millis();
   }
  }

void printPneumaticValve2() { 
  //Here write your code for PneumaticValve2 
  }
  • Neither of the ways doesn't work. – d_pawe Aug 24 '16 at 17:01

protected by Community Jan 22 '18 at 11:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.