Hey, I'm pretty new to this Arduino world and I have a question about a program setup.
I have a system, containing a small compressor, two pressure tanks, two solenoid valves and two pressure drop valves (solenoid) and a pressure sensor. The solenoid valves allows the air to enter each pressure tank, connected to each pressure drop valve. I want my Arduino UNO to run a program like this:
1: Turn the compressor and the Solenoid valve to tank 1 on.
2: When the pressure sensor reads a specific pressure, open the solenoid valve to tank 2 and close the one for tank 1.
3: When the pressure sensor reads the same specific pressure again, turn the compressor off, close the solenoid valve.
4: Open the pressure drop valve for Tank 1.
5: Wait 5 seconds and open the pressure drop valve for tank 2.
I have tried with this code:
void setup() {
// put your setup code here, to run once:
pinMode(10, OUTPUT); // Compressor
pinMode(11, OUTPUT); // Solenoid Valve 1
pinMode(12,OUTPUT); // Solenoid Valve 2
pinMode(8,OUTPUT); // Pressure Drop Valve 1
pinMode(9,OUTPUT); // Pressure Drop Valve 2
}
float desiredValue = 4.7; // Random chosen number. The pressure transducer will be calibratet later.
void loop() {
while(analogRead(A0)*(5.0/1023) < desiredValue){
digitalWrite(10,HIGH); // Turn the compressor on
digitalWrite(11,HIGH); // Open the Solenoid valve
if (analogRead(A0) < desiredValue) {
digitalWrite(10,HIGH); // This if-statement are made so the Arduinio not will turn on continually.
digitalWrite(11,HIGH);
}
}
digitalWrite(11,LOW);// Close the solnoid valve for pressure tank 1.
digitalWrite(12,HIGH); // Open the solenoid valve for pressure tank 2.
while(1) {} // This while loop are made so the solenoid valve 1, stay closed.
// The code works until here..
while(analogRead(A0)*(5.0/1023) < desiredValue) { // Read pressure again.
digitalWrite(12,HIGH); // Let the solenoid valve 2 stay open.
if (analogRead(A0) < desiredValue) {
digitalWrite(12,HIGH);
}
}
digitalWrite(12,LOW); // Turn the pressure off, when the desired pressure reached.
}
But It did not continue after the first while loop?
while(1) {} // This while loop are made so the solenoid valve 1, stay closed.
// The code works until here..
That is an infinite loop. The sketch will loop there forever and never go on to do the rest of your sketch. If you want that loop to ever end you will have to add code to end the loop under the desired conditions.
digitalWrite(10,HIGH); // Turn the compressor on
digitalWrite(11,HIGH); // Open the Solenoid valve
if (analogRead(A0) < desiredValue) {
digitalWrite(10,HIGH); // This if-statement are made so the Arduinio not will turn on continually.
digitalWrite(11,HIGH);
}
This code says that if the value of A0 is less than desiredValue, write HIGH to pins 10 and 11 which were already written to HIGH previously and unconditionally. What is the point of the "if" statement?
Hi,
Also comparing floating point values it is unpredictable. Must be done in integer.
int pressure_val ;
Normally when comparing floating numbers I do scaling as follow :
pressure_val = (analogRead(A0) * (5.0/1023.0)*10.0);//The value would be 49 instead 49.853372.
pressure_val = 49
//**************************************************************
void loop() {
int pressure_val ;
digitalWrite(10,HIGH); // Turn the compressor on
digitalWrite(11,HIGH); // Open the Solenoid valve 1
while(pressure_val < 47 ){ //multiply the reference by 4.7 10 = 47
pressure_val = (analogRead(A0)(5.0/1023.0)*10.0);
// loop until reach pressure of 4.7 psi
}
digitalWrite(11,LOW); // Close the solenoid valve for pressure tank 1.
digitalWrite(12,HIGH); // Open the solenoid valve for pressure tank 2.
Did you set the pins to outputs ?
Did the compressor ran?
Did the solenoid 11 was enable?
Did the pressure reach 4.7psi ?
Did the solenoid 12 was enable?
Can you run the program again. I added a print to see the pressure before turn on solenoid 12.
It will print the pressure when the it reach 4.7 psi. Also I added the pins modes.
//******************************************************************
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(10, OUTPUT); // Compressor
pinMode(11, OUTPUT); // Solenoid Valve 1
pinMode(12, OUTPUT); // Solenoid Valve 2
pinMode(8, OUTPUT); // Pressure Drop Valve 1
pinMode(9, OUTPUT); // Pressure Drop Valve 2
}
int desiredValue = 47; // Random chosen number. The pressure transducer will be calibratet later.
void loop() {
int pressure_val ;
digitalWrite(10, HIGH); // Turn the compressor on
digitalWrite(11, HIGH); // Open the Solenoid valve 1
while (pressure_val < desiredValue) {
pressure_val = (analogRead(A0) * (5.0 / 1023.0) * 10.0);
// loop until reach pressure
}
Serial.print("Tank 1 pressure = "); Serial.println(pressure_val ,DEC);
digitalWrite(11, LOW); // Close the solenoid valve for pressure tank 1.
digitalWrite(12, HIGH); // Open the solenoid valve for pressure tank 2.
while (pressure_val < desiredValue) { // Read pressure again.
//wait until reach pressure tank 2
pressure_val = (analogRead(A0) * (5.0 / 1023.0) * 10.0);
}
Serial.print("Tank 2 pressure = "); Serial.println(pressure_val ,DEC);
digitalWrite(12, LOW); // Solenoid Valve 2, when the desired pressure reached.
digitalWrite(10, LOW); // Turn the compressor off, when both tank are full
}