I have 2 switches each going to a seperate input pin ( Each switch is on a voltage divider network and im reading the analog value).
When i press the prime button i just want the pump to run for the duration of the button press.
When i press the calibrate button, the pump runs for my set time delay of 1 minute.
Now they both work fine if each button is in its own sketch, but when i put them both into one sketch i get problems with the OUTPUT LOW function.
I know that of of them is keeping the pin low and not allowing the other function to run, Im just struggling to find a way to allow them both to run independantly and not conflict one another.
Any help would be very greatly appreciated as always!
VR
int cal_button = 8;
int prime_button = 9;
int pump_1 = 4;
int val_c = 0;
int val_p = 0;
byte state1 = 0;
long end1;
void setup() {
pinMode(pump_1, OUTPUT);
Serial.begin(9600);
}
void loop() {
//*************************************** Prime Button Pump 1 ***************************************************************
val_p = analogRead(prime_button);
{
if (val_p <200) digitalWrite (pump_1, HIGH);
else digitalWrite (pump_1, LOW);
}
// **************************************** Calibrate Button Pump 1 *************************************************************
switch (state1) {
case 0:
val_c = analogRead(cal_button);
Serial.print( val_c );
if (val_c <200){ // Activate dose pump 1 if analog value of switch is below 200
digitalWrite (pump_1, HIGH);
end1 = millis () + 60000; // Activate for 60 seconds
state1 = 1;
}
break;
case 1:
if (end1 <= millis ()) {
digitalWrite (pump_1, LOW); // After time delay, turn dose pump 1 off
state1 = 0;
}
break;
}
}
I have 2 switches each going to a seperate input pin
do you mean
I have 2 switches each going to the same input pin
?
AWOL was trying to point out to you that you have some formatting wrong. I'm not sure If you actually do, your style of code is a little different from the Arduino "standard", but I'm not a "real" c++ programmer so yours maybe valid too.
Still you could make it clearer, and this is how I would use the curly brackets - they are in a different popsition to yours.
In regards to the other issue, why are you limiting yourself to both switches on one pin? If you are not using up all of your other pins, you could make it easier and cleaner by using another button for the switches.
Another idea would be to skip the microcontroller all together for the "prime" button. Since you only want it to run while it's pushed, you could connect one side of the switch to GND, and the other side of the switch straight to the output pin. This way when you push the switch the output pin goes low.
OK here is the background for you. I have 4 pumps, each pump has a prime button and a calibrate button.
I only have 2 inputs available so each set of prime buttons go into one input pin and each set of calibrate buttons go into the other output pin. The buttons are seperate by voltage dividers to allow multiple buttons on a single output. I have just given the one pump as an example to make it easier to work on. Once i have it working for one pump i will just multiply the code for the other 3 pumps.
Here you can see the voltage divider idea im using:
I never thought about just driving the pumps directly with the prime button. I think you just solved all my problems by suggesting that!!!!