two (2) input buttons/two (2) micro-pumps with two (2) time delays per pump

The goal of my project is dispense a certain volume of liquid by a quick press of the button for each pump (e.g button1held <= option1) and to prime the pumps by pressing the button longer (e.g. button1held >= option2) and obviously do nothing if button not pressed (while loop?). As of now arduino uno not accepting the time variable of my code (e.g. as of now I press on the button and pump stays on and I release the button pump turns off) Need expertise please... attached is an image of my circuit and the code:

// constants won't change. They're used here to set pin numbers:
const int button1 = 3;     // button #1 pin
const int button2 = 4;     // button #2 pin
const int pump1 = 7;       // pump #1 pin
const int pump2 = 6;       // pump #2 pin

// variables will change:
int button1state = 0;         // variable for reading the button #1 status
int button2state = 0;         // variable for reading the button #2 status
int option1 = 1000;           // time #1
int option2 = 2000;           // time #2
float button1held = 0;        // time button #1 held
float button2held = 0;        // time button #2 held


void setup() {
   // initialize the button #1 pin as an input:
  pinMode(button1, INPUT);
   // initialize the button #2 pin as an input:
  pinMode(button2, INPUT);
  // initialize the pump #1 pin as an output:
  pinMode(pump1, OUTPUT);
  // initialize the pump #2 pinn pin as an input:
  pinMode(pump2, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value:
  button1state = digitalRead(button1);
  button2state = digitalRead(button2);

  while (button1state == HIGH) {
  delay(100);
  button1held = button1held + 100; 
  }
  if (button1held <= option1) {
    // turn pump#1 on:
    digitalWrite(pump1, HIGH);
    delay(400);
 }
    else if (button1held >= option2) {
      //turn pump#1 off
      digitalWrite(pump1, HIGH);
    }
 button1held = 0;
 
 
 while (button2state == HIGH) {
  delay(100);
  button2held = button2held + 100; 
 } 
   if (button2held <= option1) {
    // turn pump#2 on:
    digitalWrite(pump2, HIGH);
    delay(800); 
  }
  else if (button2held >= option2) {
    //turn pump#2 off
    digitalWrite(pump2,HIGH);
  }
button2held = 0;

6V_micro_pumps.ino.ino (1.65 KB)

Please post your code, and post all of your error message, not a picture of it.

Does not give me an error message:

// constants won't change. They're used here to set pin numbers:
const int button1 = 3; // button #1 pin
const int button2 = 4; // button #2 pin
const int pump1 = 7; // pump #1 pin
const int pump2 = 6; // pump #2 pin

// variables will change:
int button1state = 0; // variable for reading the button #1 status
int button2state = 0; // variable for reading the button #2 status
int option1 = 1000; // time #1
int option2 = 2000; // time #2
float button1held = 0; // time button #1 held
float button2held = 0; // time button #2 held

void setup() {
// initialize the button #1 pin as an input:
pinMode(button1, INPUT);
// initialize the button #2 pin as an input:
pinMode(button2, INPUT);
// initialize the pump #1 pin as an output:
pinMode(pump1, OUTPUT);
// initialize the pump #2 pinn pin as an input:
pinMode(pump2, OUTPUT);
}

void loop() {
// read the state of the pushbutton value:
button1state = digitalRead(button1);
button2state = digitalRead(button2);

while (button1state == HIGH) {
delay(100);
button1held = button1held + 100;
}
if (button1held <= option1) {
// turn pump#1 on:
digitalWrite(pump1, HIGH);
delay(400);
}
else if (button1held >= option2) {
//turn pump#1 off
digitalWrite(pump1, HIGH);
}
button1held = 0;

while (button2state == HIGH) {
delay(100);
button2held = button2held + 100;
}
if (button2held <= option1) {
// turn pump#2 on:
digitalWrite(pump2, HIGH);
delay(800);
}
else if (button2held >= option2) {
//turn pump#2 off
digitalWrite(pump2,HIGH);
}
button2held = 0;

Does not give me an error message

...is the wrong answer.

What is it that you would like to see? I have a picture of the circuit and I have pasted the code. What else do you need to make this happen?

I would like to see all your code, in code tags.
If, like the code you posted in reply #2, the code doesn't compile, I want to see the error messages.

I also want to know what

As of now arduino uno not accepting the time variable of my code

means

I was trying to mimic the code from this link: How to Make One Button Have the Functionality of Two or More with Arduino - Programming Electronics Academy

They seem to do a counter time variable in the code. I initialized button1held = 0; and then delayed for 100 millisecond time intervals which is button1held in my code and compared it to option1 or option2 which I made integers.

while (button1state == HIGH) {
delay(100);
button1held = button1held + 100;
}
if (button1held <= option1) {
// turn pump#1 on:
digitalWrite(pump1, HIGH);
delay(400);

Buttons not wired correctly, what kind of transistor? You should learn to draw simple schematics and forget Fritzing.

This is a bit confusing.

There is code in Reply #2 and also in the Original Post and the Original Post was edited after Reply #2 was written. Which is the latest version that we should look at?

This confusion is easily avoided by not making major changes to older Posts so that the Thread can be read in chronological order from top to bottom.

You seem to have lots of delay()s in your program. If you want a responsive program they must all go as they block the Arduino from doing other things. Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

I'm still waiting to see all the code.

All of my code is on the first post. The code I pasted on the third post is identical. I will read up on millis() for more responsiveness. My question has still not been answered. A button that can have two (2) different functions based on how long the button is pressed. No one can answer the simple question.

joshdaking777:
All of my code is on the first post.

Please don't waste time.
That code is missing at least a closing brace, possibly more.

joshdaking777:
All of my code is on the first post. The code I pasted on the third post is identical. I will read up on millis() for more responsiveness. My question has still not been answered. A button that can have two (2) different functions based on how long the button is pressed. No one can answer the simple question.

OK so to summarise are you trying to say that you want to take different actions based on the length of time a button is held down ?

If so then you need to remove the delay statements and poll the Digital input on a tight loop looking for state changes - incrementing a variable representing the amount of time lapsed with each poll.

Definitely read up on the Millis function or look at one of the other librarys that wrap this up in something easier for a newbie to use. (Hint - SimpleTimer is one of those)

Craig

Here is a solution for the beginners.
But i suggest you study and find the way do the same without delay function.

const int button1 = 3;
int button1state = LOW;

void setup() {
  pinMode(button1, INPUT);
}

void loop() {
 button1state = digitalRead(button1); 
 
if (button1state == HIGH) {
  delay (500);
  button1state = digitalRead(button1);
  if (button1state == HIGH) {
    // EXCECUTE THE FUNCTIN "A"
  }
  else if (button1state == LOW) {
    // EXCECUTE THE FUNCTIN "B"
  }
}

}

The code in this Post has a neat way to detect different button presses.

...R

thank you craigcurtin, only one that directly answered my question