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;
// 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;
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);
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.
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. 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)