Do you have a pullup / pulldown resistor from pin 35 to +V / GND to keep the pin from floating when the button is not pressed? How is the pushbutton wired?
Switch function is working fine, as far as the debouncing. I feel the error is solely related to the timer function. I have removed the debounce part, and button still works (although triggered multiple times since there is no debouncing).
One side of switch is connected to 5V.
Pin 35 is tied to ground via a 10k and connected to other side of switch, so pin 35 is tied to ground until switch is pressed when it goes high.
Put a serial print debug statement in the dispense1() and I believe you will see it is only being called when the button state changes from LOW to HIGH. In order to turn off the pump you will need to call dispense1() each time through loop() while the timerRunning flag is set.
I got the code working inside void loop, however if I try to create a seperate "dispense1" function, I can't get it to work in the function. I have the same result where the Serial is printed, LED turns on (pin goes high) but doesn't shut off.
This works:
const int dispenseButton1 = 35;
int dispenseButton1State;
unsigned long timeNow;
unsigned long startTime;
int timerRunning;
unsigned long dispense1Time = 1000;
const int linearPump = 12;
void setup() {
Serial.begin(115200);
pinMode(dispenseButton1, INPUT);
pinMode(linearPump, OUTPUT);
}
void loop(){
if ((digitalRead(dispenseButton1) == HIGH) && (timerRunning == 0)){
startTime = millis();
digitalWrite (linearPump, HIGH);
Serial.println(F("Dispense1 Button Pressed"));
timerRunning = 1;
}
if (timerRunning == 1){
timeNow = millis();
if ((timeNow - startTime)>= dispense1Time){
timerRunning = 0;
digitalWrite (linearPump, LOW);
}
}
}
This does not, and I can't figure out why:
const int dispenseButton1 = 35;
int dispenseButton1State;
unsigned long timeNow;
unsigned long startTime;
int timerRunning;
unsigned long dispense1Time = 1000;
const int linearPump = 12;
void setup() {
Serial.begin(115200);
pinMode(dispenseButton1, INPUT);
pinMode(linearPump, OUTPUT);
}
void loop() {
dispenseButton1State = digitalRead(dispenseButton1);
if (dispenseButton1State == HIGH) {
dispense1();
}
}
void dispense1() {
if ((dispenseButton1State == HIGH) && (timerRunning == 0)) {
startTime = millis();
digitalWrite (linearPump, HIGH);
Serial.println(F("Dispense1 Button Pressed"));
timerRunning = 1;
}
if (timerRunning == 1){
timeNow = millis();
if ((timeNow - startTime)>= dispense1Time){
timerRunning = 0;
digitalWrite (linearPump, LOW);
}
}
}
poor_red_neck:
I got the code working inside void loop, however if I try to create a seperate "dispense1" function, I can't get it to work in the function. I have the same result where the Serial is printed, LED turns on (pin goes high) but doesn't shut off.
Like I said earlier you are only calling the dispense1 function when you press the button. You have to call it every time in order for it to keep calculating the timeout for the pump.
ToddL1962:
Put a serial print debug statement in the dispense1() and I believe you will see it is only being called when the button state changes from LOW to HIGH. In order to turn off the pump you will need to call dispense1() each time through loop() while the timerRunning flag is set.
Without plainly giving me the answer (I'm here to learn, not be spoonfed) can you give me a little bit more of a hint?
EDIT: Sorry Todd, these 5 minute delays in replying (not to 100 posts yet) are killing me...
So if I understand correctly, you have to switch to the dispense1() function every loop, to keep the check going for startTime vs timeNow? Otherwise it goes through the dispense1 function one time, but never comes back. Is that correct?
So if I understand correctly, you have to switch to the dispense1() function every loop, to keep the check going for startTime vs timeNow? Otherwise it goes through the dispense1 function one time, but never comes back. Is that correct?
Haha, yes'sir. Although not so poor anymore lol, done growed up!
I'm from North Carolina, not the "dirty dirty" south, but still hot-as-balls in the summer south.
Thank you for your help, it is greatly appreciated.
poor_red_neck:
Haha, yes'sir. Although not so poor anymore lol, done growed up!
I'm from North Carolina, not the "dirty dirty" south, but still hot-as-balls in the summer south.
Thank you for your help, it is greatly appreciated.