Pumps not turned on and off at desired time - Problem with code?

I am running two motor pumps with an Arduino board, one problem that I have right now is that they don't run at the desired time. The first pump and second pump run simultaneously when I plugged in the power source. However, after 10-15 seconds, they started to run as desired. I am really confused, I checked my circuit and it doesn't seem like it's wrong, and also since they run as desired after 10-15 seconds, I think the problem is with my code. Below is what I have:

int InPin = 7; // In Pump - NPN base connected to digital pin 7
int DrainPin = 8; // Drain pump - NPN base connected to digital pin 8
int blinkPin = 13; //

int InTime = 5000; // 5 seconds (change accordingly)
int DwellTime  = 5000; // 5 seconds
int DrainTime = 5000; // 5 seconds
int WaitTime = 5000; // 5 seconds

void setup() 
{ // put your setup code here, to run once:
  pinMode(InPin, OUTPUT); // set the pin 7 as output
  pinMode(DrainPin, OUTPUT); // set the pin 8 as output
  pinMode(blinkPin, OUTPUT); //
}

void loop() 
 { // Pump water
  digitalWrite(InPin, LOW); // In NPN LOW, MOSFET HIGH, turns pump on
  digitalWrite(blinkPin, HIGH); // blinkPin ON means pumping into patient 
  delay(InTime); // Pump water for 25 secs
  
  // Dwelling
  digitalWrite(InPin, HIGH); // In NPN HIGH, MOSFET LOW, turns pump off
  digitalWrite(blinkPin, LOW); // blinkPin off means pump is turned off 
  delay(DwellTime); // dwell water for 10 secs
  
  // Draining starts
  digitalWrite(DrainPin, LOW); // Drain NPN LOW, MOSFET HIGH, turns pump on
  delay(DrainTime); //
  
  // Pause before starting another cycle
  digitalWrite(DrainPin, HIGH); // Drain NPN HIGH, MOSFET LOW, turns pump off
  delay(WaitTime); 
  // After this the In pump will be turned on again, and the cycle will be repeated for 30 times
}

Please let me know if there's something that you think I should fix! Thanks!!

=========
I removed "{" and "}" accidentally when I transferred the code just now. The code compiles, it just doesn't work the way I want it to work.

(deleted)

int InTime = 5000; // 5 seconds (change accordingly)
int DwellTime = 5000; // 5 seconds
int DrainTime = 5000; // 5 seconds
int WaitTime = 5000; // 5 seconds

Time variables are ALWAYS unsigned long.

Your code won't even compile. It is pointless to complain that something that won't even compile "doesn't work".

First, you are missing a "}" at the end of setup().

Try this:

{ // put your setup code here, to run once:
  digitalWrite(InPin,HIGH);
  digitalWrite(DrainPin,HIGH);
  pinMode(InPin, OUTPUT); // set the pin 7 as output
  pinMode(DrainPin, OUTPUT); // set the pin 8 as output
  pinMode(blinkPin, OUTPUT); //
}

The "{" and "}" were removed accidentally when I transferred the code just now.

PaulS:
Time variables are ALWAYS unsigned long.

Time stamps must be. Intervals don't have to be. They can be int or even byte and be perfectly safe, as long as you don't overflow them when you assign a value to them.

  digitalWrite(InPin, LOW); // In NPN LOW, MOSFET HIGH, turns pump on

I'm always impressed by people who use "in" in the name of an output pin.

  digitalWrite(blinkPin, HIGH); // blinkPin ON means pumping into patient

And what does HIGH mean?

  // After this the In pump will be turned on again, and the cycle will be repeated for 30 times

No, the cycle will be repeated until the Arduino wears out.

You should set the state of the output pins in setup(), since the default value of LOW means that the pump(s) is/are running, and that isn't what you want.

PaulS:

  digitalWrite(blinkPin, HIGH); // blinkPin ON means pumping into patient

And what does HIGH mean?

I was following the DigitalWrite format on the Arduino website: digitalWrite() - Arduino Reference

No, the cycle will be repeated until the Arduino wears out.

This is in the comment section, I removed the for loop because the pumps are not working the way I want them to work now.

You should set the state of the output pins in setup(), since the default value of LOW means that the pump(s) is/are running, and that isn't what you want.

Thanks! This helped solve the problem!

spycatcher2k, thanks for your comment as well! I missed that just now! :slight_smile:

aarg, thanks for the input, I'll keep that in mind when I work on my circuit!

PaulS:

  digitalWrite(InPin, LOW); // In NPN LOW, MOSFET HIGH, turns pump on

I'm always impressed by people who use "in" in the name of an output pin.

I think he has an in and an out pump. So InPin is InPumpPin.

polarpan:
I am running two motor pumps with an Arduino board, one problem that I have right now is that they don't run at the desired time. The first pump and second pump run simultaneously when I plugged in the power source. However, after 10-15 seconds, they started to run as desired.

This is because outputs are LOW by default, and in your sketch LOW turns the pump on.

Turn the pumps off in your setup.

void setup()
{ // put your setup code here, to run once:
  pinMode(InPin, OUTPUT); // set the pin 7 as output
  pinMode(DrainPin, OUTPUT); // set the pin 8 as output

  digitalWrite(InPin, HIGH); // turn pump off at startup
  digitalWrite(DrainPin, HIGH); // turn pump off at startup

  pinMode(blinkPin, OUTPUT); //
}

Hi,

digitalWrite(blinkPin, HIGH); // blinkPin ON means pumping into patient

"patient" what is your application?

If medical and for human life situations, I think you had better read the fine print at Atmel, about using their product for such applications.

Tom.... :slight_smile:

I think he has an in and an out pump. So InPin is InPumpPin.

Into and out of what? That should be what is used to name the pin. The pumps are not pumping stuff into or out of the Arduino.