Hi to all, I'm new to adruino, just got my board and trying to make a "system" to control my garden pump.
I wrote some code but it's not working as I expected to work.
What code needs to do:
start the pump (push button) for max 30min (must start if water sensor is low)
read the water sensor - if no water for more than 5 sec - pump off
if sensor detect no water for less than 5s (say 3s) reset the 5sec timer and leave the pump to run
if 30min elapsed stop the pump and wait for button to start it again
if I press the button in any time start/stop
Sorry for my bad english.
Here is my code so far:
int pump = 13; // the number of the pump pin
int button = 2; // the number of the button pin
int sensor = 7; // the number of the sensor pin (SENSOR IS DIGITAL)
boolean lastState;
boolean state = LOW; // variable for first status
unsigned long maxTime = 1800000; //max time to run the pump
unsigned long dryRunTimer = 5000; //max time to run the pump if no water
unsigned long past, pastTimer;
int off = 0;
unsigned long valTime;
void setup() {
pinMode (button, INPUT); // initialize the pushbutton pin as an input:
pinMode (pump, OUTPUT); // initialize the pump pin as an output:
pinMode(sensor, INPUT); // initialize the sensor pin as an input:
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(button);
if ( reading == LOW && lastState == HIGH ) {
delay (10); // solve the switch bounce
if (digitalRead(button) == LOW) {
state = !state;
}
}
digitalWrite(pump, state);
lastState = reading;
if (state == LOW) {
}
if (state == HIGH) {
unsigned long current = millis();
/////////////////// timer for max run
if ( current - past >= maxTime) {
off = 1;
Serial.println(" time is up");
past = current;
}
/////////////////// timer for sensor
if (digitalRead(sensor) == LOW) {
valTime = millis();
if ( valTime - pastTimer >= dryRunTimer) {
off = 1;
pastTimer = valTime;
}
Serial.println(" no water");
}
if (digitalRead(sensor) == HIGH) {
valTime = millis();
Serial.println("yes water");
pastTimer = valTime;
valTime = 0;
}
/////////////////// turn the pump off
if (off == 1) {
Serial.println("switch off");
digitalWrite(pump, LOW);
state = !state;
off = 0;
}
}
}
Hmm... I'll try to explain. When I push the button to start the pump (led now just for testing)
1.pump statrs dry - as it should be
2.within 5sec sensor is high - as it should be
3. pump running - as it should be
4. pump running but sensor is low for more than 5sec - it' possible if there is no water to suck
5.pump off - as it should be
6.if I press the button once pump is off - here is the problem
My sensor timer is not working correctly, each timer must be reset if pump is not running.
5.pump off - as it should be
6.if I press the button once pump is off - here is the problem
No, that is not clear. What should happen when you press the button once, and what does happen?
Hint: put serial.print statements in your code, to determine if the variables are what you expect them to be.
Please explain how you have wired your switches. The switch logic may be incorrect.
I really can't follow your logic. For example, what is this supposed to do:
The button itself is working correctly, If I press it goes from high to low, next press from low to high and so on.
State variable represent pump status, If is low-do nothing, If is high start timers and logic. State will go low/off one way or another - 30min elapsed or sensor is low more than 5 sec or button is press again.
I thing I have to reset timers some how in this statement:
if (state == LOW) {
//reset all timers here?
}
Test the code in this sequence mentioned above from 1 to 6.
This kind of explanation of "when I do this, so-and-so should happen, but actually such-and-such happens" hurts my brain.
You should perhaps look at drawing a very simple state diagram like Figure 1 here (kis: doesn't have to make your brain hurt) which will make things a lot clearer. Easy to see what causes it get from one state to the next, and to what stimuli it may and must respond to in each state.
Finally I made it to work as I want! The code needs to be optimised (remove junk lines...)
Here is the code:
#include "Countimer.h"
Countimer timer;
Countimer dryRunTimer;
int pump = 13; // the number of the pump pin
int button = 2; // the number of the button pin
int sensor = 7; // the number of the sensor pin (SENSOR IS DIGITAL)
int timerOff = 0;
int is_started;
boolean lastState;
boolean state = LOW; // variable for first status
void setup() {
timer.setCounter(0, 30, 0, timer.COUNT_UP, onComplete);//max time to run the pump
dryRunTimer.setCounter(0, 0, 5, dryRunTimer.COUNT_UP, onWaterComplete);//max time to run the pump dry
pinMode (button, INPUT); // initialize the pushbutton pin as an input:
pinMode (pump, OUTPUT); // initialize the pump pin as an output:
pinMode(sensor, INPUT); // initialize the sensor pin as an input:
Serial.begin(9600);
}
////////////
void onComplete() {// do when max running timer elapsed
timerOff = 0;
is_started = 0;
timer.start();
timer.stop();
dryRunTimer.start();
dryRunTimer.stop();
digitalWrite(pump, LOW);
state = !state;
Serial.println("switch off max running time elapsed 30 min");
}
void onWaterComplete() {// do when max dry running timer elapsed
timerOff = 0;
is_started = 0;
timer.start();
timer.stop();
dryRunTimer.start();
dryRunTimer.stop();
digitalWrite(pump, LOW);
state = !state;
Serial.println("switch off no water for more than 5 sec");
}
////////////
void loop() {
timer.run();
dryRunTimer.run();
int reading = digitalRead(button);
if ( reading == LOW && lastState == HIGH ) {
delay (10); // solve the switch bounce
if (digitalRead(button) == LOW) {
state = !state;
}
}
digitalWrite(pump, state);
lastState = reading;
//////////
if (state == LOW) {
timerOff = 0;
timer.start();
timer.stop();
dryRunTimer.start();
dryRunTimer.stop();
}
if (state == HIGH) {
if (timerOff == 0) {
timer.start();
timerOff++;
Serial.println("start the main timer once");
}
/////////////////// timer for sensor
if (digitalRead(sensor) == LOW) {
if (is_started == 0) {
dryRunTimer.start();
is_started++;
Serial.println("start the dry timer once");
}
// Serial.println("water is not leaking - it's not OK");
}
if (digitalRead(sensor) == HIGH) {//interupt restart sensor timer
dryRunTimer.restart();
is_started = 0;
// Serial.println("water is leaking - all OK");
}
/////////////////// end timer for sensor
}
}