Automatic Air Pump

HI There :
I'm trying to build an automatic air pump for one of the projects for school. There will be a 12v DC motor connected to a 2N2222 transistor and then connected to the arduino input. this will mainly input the pressure in the air chamber. There is also a solenoid valve 12v connected to the valve to empty the extra air pressure. The third part will be the pressure sensor that will be reading the pressure up to 300 mmHg. I also included a switch that the user could press the bottom and let the automatic pump to start the task. I have attached the code and the schematic of my project. Unfortunately when I upload the code to the arduino the air pump will stay on and it will not move to the next loop. I tried the pressure sensor code and it works perfectly using a Aneroid. I tried to look over the code over and over again but i cant find the problem. I'm pretty new to arduino and Probably I'm missing some parts, any help will be appropriated.
Thanks

NEW Project (AIR PUMP).txt (7.86 KB)

your getPressure function is way too big.
it has far more responsibilities than getting the pressure

advice: dissect it into parts that have single function

bring it back to something like

void loop()
{
  int hg = getPressure();

  if (digitalRead(powerswitch) == HIGH)  // switch device on/off
  {
    if (hg > threshold) digitalWrite(pump, LOW);
    else digitalWrite(pump, HIGH);
  }
  else  digitalWrite(pump, LOW);
}

You need fly-back diodes parallel with the valve and the motor. They are inductive loads.

Can you send text to the serial monitor with the Serial library ?

So you can see for example the pressure on the computer while the sketch is busy.

For myself I would prefer other names, to make it more readable.
For example "pinMotor" instead of "DCM".
And "pinValve" instead of "SOL"

Which pressure sensor are you using ?

During the delays, the buttons are ignored. You may have to rewrite your sketch later. For you, do as robtillaart wrote, split your sketch into parts with functions according to the functionality of that code.

Hi, what are the power requirements for the pump motor, and the solenoid?
2N2222 is only rated to 1A max.
As Caltoa says, you need fly-back diodes on those two components.

Tom..... :slight_smile:

Thanks for the responses, sorry for the late reply. So after all of the different testings that I have done I modified my code as much as possible and I came up with the code that I have right now I just created a code that will create the first pulse (raise from 0mmHg to only 25mmHg and hold it ). It kinda works but how could I modify it and create the next pulse of the 50mmHg pressure ? the code that I wrote is not constantly giving me the same values (when I press on the push button some times I get 25mmHg, some times I get 30mmHg, on the other hand the solenoid valve doe not empty the air sometimes. Is there a glitch (bug ) in my code that i cant find? any help is needed.
again thanks for the quick responses.

working code !!.txt (2.72 KB)

I wrote before:
For example "pinMotor" instead of "DCM".
And "pinValve" instead of "SOL"
And I have a few others like that.

You define the pins, but use a number in the code.
This is preferred:

const int pinButton = 5;

pinMode (pinButton, INPUT);

if (digitalRead (pinButton) == HIGH)

How did you connect the button ? If it is connected to ground, you have to check it for being LOW.

The pressure are float variables, but you test it against integer values. I would test it against float values.
The pressure is almost never the exact value.
Perhaps it is better to see the range 24 and 26 as the good pressure:

const float THR_low = 24.0;
const float THR_high = 26.0;

if (pressuremmHg > THR_high)
{
  // The pressure to too high
  ...
}
else if (pressuremmHg < THR_low)
{
  // The pressure to too low
  ...
}
else
{
  // The pressure is good, between low and high.
  ...
}