Author Options:

Arduino code not working. Super basic but I'm stuck. Help is appreciated. Answered

     So I have wrote an Arduino code for a simple Aquarium dosing pump controlled by Android and Arduino. I have a button on pin 8 and I want the button to just be a physical trigger for the dosing for calibration purposes. for whatever reason i cant seem to figure out how to do this and help would be greatly appreciated. Thanks a bunch (:

(Warning: I'm far from an expert at Arduino I'm sure there is lots of ways this could be improved)

//Objective: Dose aquarium with chemicals daily via bluetooth app. with the amount dosed set by potentiometer
int pumpPin = 10; //to relay
int ledpin = 13; //for test purposes
int state = 0;
int flag = 0;
int Dtime = 0; //value for how long to dose
int Mval = 0; //Variable that holds the value of dosing time
int PumpCalPin = A0; //pin the potentiometer goes to
int pumpTestPin = 8; //button to set off pump for calibration purposes
int buttonPower = 9; //source of power for the button because I'm out of voltage pins

void setup() {
  pinMode(pumpPin, OUTPUT);
  pinMode(buttonPower, OUTPUT);
  digitalWrite(buttonPower, HIGH);
  pinMode(pumpTestPin, INPUT);
  pinMode(ledpin, OUTPUT);
  digitalWrite(pumpPin, LOW);
  digitalWrite(ledpin, LOW);
void loop() {
  int testPinVal = digitalRead(pumpTestPin);
  if (testPinVal == 1) {
    state = 1;
    flag = 0;
  if (Serial.available() > 0) {
    state = Serial.read();
    flag = 0;
    Mval = analogRead(PumpCalPin);
    Dtime = map(Mval, 0, 990, 1000, 5000); //map the potentiometer input from 1 to 5 seconds
  if (state == '1') {
    digitalWrite(pumpPin, HIGH);
    digitalWrite(ledpin, HIGH);
    digitalWrite(pumpPin, LOW);
    digitalWrite(ledpin, LOW);
    if (flag == 0) {
      state = 0;
      flag = 1;


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 4 years ago

So pushing the button sets 'state' to '1'. Then if there is a seriel connection it overwrites state and the final If.Then never runs. If there is no serial connection then it turns on the pump for 'Dtime' which is still '0'. Basically you will never get the pump to run either with the button or the bluetooth because the button turns the pump on but doesn't set the time and the bluetooth sets the time but doesn't run the pump.

The only way this would work is to connect the bluetooth which would set the time and then disconnect it and push the button.


Answer 4 years ago

Thank you! So how could this be fixed? How do I need to change my code so I can use the Bluetooth or the button?


Answer 4 years ago

Sorry, busy weekend.

If I were writing this I'd probably break out a few things you want to do over and over into their own functions. Checking the pots position and running the pump being the most obvious.

That means your loop just needs to check for new serial data or a button push. with a very light Loop() it will be checking so frequently that you don't have to worry about it missing a button push. Basically:


Checks for serial or button


Sets the delay time based on the pot setting when called


Runs the pump for the set delay when called

I can help you write more in depth code for this but I think you should take a shot at it yourself first. Check out Arduino website for lots of great references and help. If you still can't get it running hit me up with a private message and I'll work through it with you.


4 years ago

I am no expert either but I had to struggle with the map function on my laser cutter.
You use the analog read function to be used in the mapping function.
This should actually be limited to the available or possible range, this part is not clear in your code so it might be good already.
After all this you use the calculate value to set a delay, which in theory should work great but in reality works better if the variables are defined as integer values (which you did) and to actually check the input and output - either using a display or the serial console.
For troubleshooting these problems it is best to know and see what goes in what comes out.
In my case with the laser the problem was that the mapping function did not react as planned in all cases due to procession speed problems.
Another potential problem I see is that you try to use the set values directly in terms of a delayed switching of outputs.
I don't know what you use to disperse the chemicals but a syringe system with a stepper motor would give you much better control.
This way you define step instead of time and this means it is repeatable without considering fluid resistance.
Same for a little pump.
Consider the way ink jet printers clean:
Here a motor spins a set of rollers that act as a pump on a flexible hose.
If done by time there is no way of telling how much is pumped if the resistance or force changes and causes the motor to run slower.
A stepper motor will have a set number of steps that won't change with the load.