Arduino aquarium dosing pump.

So I'm going to start off by saying this is my first post here. Also id like to say I'm a beginner so this is probably a super simple issue. I have created an aquarium dosing pump powered by Arduino and an android app I made with MIT app inventor. everything has worked great up until I installed everything and filled the container up with Glut (Metricide for Co2) and tested it by pressing the button on the case. everything works great! the pump dosed the correct amount and I was very happy with it. Then I tested the Bluetooth activation of the pump and this is where I'm having issues. The pump didn't shut off and I quickly flipped the power switch off so i'm not sure if it was going to eventually shut off or not. I'm not sure why because in previous tests it always has! I just switched up the Arduino code a bit so I think that's why i'm having troubles. I do not believe it is a problem with the app because it used to work fine. Your help would be greatly appreciated. Thanks a bunch!

//Objective: Dose aquarium with chemicals daily via bluetooth app. with the amount dosed set by potentiometer
//Pin Declerations
const int pumpPin = 10;     //to relay
const int ledpin = 13;      //for test purposes
const int pumpCalPin = A0;  //pin the potentiometer goes to
const int pumpTestPin = 8;  //button to set off pump for calibration purposes
const int buttonPower = 9;  //source of power for the button because I'm out of voltage pins

//Initialize Variables
int dTime = 0;
int mVal = 0;
int val = 0;

void setup() {
  pinMode(pumpPin, OUTPUT);
  pinMode(buttonPower, OUTPUT);
  digitalWrite(buttonPower, HIGH);
  pinMode(pumpTestPin, INPUT);
  pinMode(ledpin, OUTPUT);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
  Serial.begin(9600);
}

void loop() {
  val = Serial.read();
  if (digitalRead(pumpTestPin) == HIGH) {
    pump();
  }
  if (val > 0) {
    serialEvent();
  }
}

void serialEvent() {
  pump();
}

void pump() {
  potRead();                       //Call the delay set function before the pump runs.
  digitalWrite(pumpPin, 1);
  digitalWrite(ledpin, 1);
  delay(dTime);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
}

void potRead() {
  mVal = analogRead(pumpCalPin);
  dTime = map(mVal, 0, 990, 1000, 10000); //map the potentiometer input from 1 to 10 seconds

}

Hello, I would start off by changing digitalWrite(pumpPin, 1); to digitalWrite(pumpPin, 0); That would allow you to test everything without really kicking the pump in. Just watch the LED. Does it truly never go off?

Did you check the value of 'dTime' variable, may you can do a serial write and check the value?

Ok so I undid everything from the tank and set it back up on my workbench and did some testing. So on a button press the pump goes for approximately 8 seconds (me counting out loud). With the Bluetooth on the first try it went for 8 seconds.... second try didn't go so well it went for approximately 16 before shutting off. 3rd and 4th test it went for 8 again. Then the 5th test it went for 16. I think for some reason the Arduino needs some sort of buffer on the Bluetooth because it thinks its being told to dose twice. Does anyone have any input on if this is true or not and how I would go about doing that?

OK so I've redid the code a bit and adding while (Serial.available()) has seemed to make it worse. But I've also added some indicators in the code so I can monitor it via serial and I can now see whats happening. this is the readout from the terminal on a button press: pumping done pumping This is the readout on a bluetooth trigger: pumping done pumping pumping done pumping pumping done pumping pumping

This just goes on forever now as far as I can tell. Your help is greatly appreciated thanks a bunch! I'll attach my new code below.

//Objective: Dose aquarium with chemicals daily via bluetooth app. with the amount dosed set by potentiometer
//Pin Declerations
const int pumpPin = 4;   //to relay
const int ledpin = 13;    //for test purposes
const int pumpCalPin = A0;  //pin the potentiometer goes to
const int pumpTestPin = 8;  //button to set off pump for calibration purposes
const int buttonPower = 9;  //source of power for the button because I'm out of voltage pins

//Initialize Variables
int dTime = 0;
int mVal = 0;
int val = 0;

void setup() {
  pinMode(pumpPin, OUTPUT);
  pinMode(buttonPower, OUTPUT);
  digitalWrite(buttonPower, HIGH);
  pinMode(pumpTestPin, INPUT);
  pinMode(ledpin, OUTPUT);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
  Serial.begin(9600);
}

void loop() {
  while (Serial.available()) {
    val = Serial.read();
  }
  if (digitalRead(pumpTestPin) == HIGH) {
    pump();
  }
  if (val > 0) {
    serialEvent();
  }
}

void serialEvent() {
  pump();
}

void pump() {
  Serial.println("pumping");
  potRead();            //Call the delay set function before the pump runs.
  digitalWrite(pumpPin, 1);
  digitalWrite(ledpin, 1);
  delay(dTime);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
  Serial.println("done pumping");
}

void potRead() {
  mVal = analogRead(pumpCalPin);
  dTime = map(mVal, 0, 990, 1000, 10000); //map the potentiometer input from 1 to 10 seconds

}

Just go through the post related to Serial communication, http://forum.arduino.cc/index.php?topic=396450

Also to avoid pumping continuously, try to set 'val to 0' after pumping is done.

void pump(){
   .....
   .....
   Serial.println("done pumping");
    val=0;
}

Thank you very much! this is my end code and its working great!

//Objective: Dose aquarium with chemicals daily via bluetooth app. with the amount dosed set by potentiometer
//to do: add a temperature and PH probe that sends data back to the Android app, Build this on a smaller PCB (Again without frying this one) which has connectors for plugging in more dosing pumps and sensors.
//Pin Declerations
const int pumpPin = 4;   //to relay
const int ledpin = 13;    //for test purposes
const int pumpCalPin = A0;  //pin the potentiometer goes to
const int pumpTestPin = 8;  //button to set off pump for calibration purposes
const int buttonPower = 9;  //source of power for the button because I'm out of voltage pins

//Initialize Variables
int dTime = 0; //the mapped version of mVal that specifies the amount of time the pump runs.
int mVal = 0; //raw data from potentiometer that is to be mapped into seconds for the variable dTime
int val = 0; //incoming value from the serial comunication
boolean trigger1 = false; //trigger for the pump

void setup() {
  pinMode(pumpPin, OUTPUT);
  pinMode(buttonPower, OUTPUT);
  digitalWrite(buttonPower, HIGH);
  pinMode(pumpTestPin, INPUT);
  pinMode(ledpin, OUTPUT);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
  Serial.begin(9600);
}

void loop() {
  recNumber();
  if (digitalRead(pumpTestPin) == HIGH) {
    pump();
  }
  if (trigger1 == true) {
    serialEvent();
  }
}

void serialEvent() {
  pump();
}
//recieves the incoming number from the serial comunication with the bluetooth/computer(for testing)
void recNumber() {
  while (Serial.available()) {
    val = Serial.read();
    if (val == 1) {
      trigger1 = true;
    }

  }

}
//runs the pump and test LED while also changing the trigger back to false
void pump() {
  Serial.println("pumping"); //for testing purposes 
  potRead();            //Call the delay set function before the pump runs.
  digitalWrite(pumpPin, 1); //pump on
  digitalWrite(ledpin, 1); //test LED on
  delay(dTime); //pump for the amount of time specified by the variable dTime
  digitalWrite(pumpPin, LOW); //pump off
  digitalWrite(ledpin, LOW); //test LED off
  Serial.println("done pumping"); //for testing purposes
  trigger1 = false; //sets the trigger to false to prevent continous pumping
}
//reads the potentiometer and maps the value
void potRead() {
  mVal = analogRead(pumpCalPin);
  dTime = map(mVal, 0, 990, 1000, 10000); //map the potentiometer input from 1 to 10 seconds

}

You have no idea how much help that was. Thank you very much

good to hear that, your issue is resolved.