Hi there,
I'm very close to finishing, but I'm having some problems with using millis() inside a function. This code has been posted in a different thread but I'm posting it here as it is now better suited for Project guidance.
Project overview:
Watering system for my palm.
- Check battery level and water level every 5 minutes. (Right now I have it checking every 5 seconds for testing).
If the battery level is below 7 volts or if the water level (reed_val) is below a certain point, the LED will blink continuously until water reservoir is filled or battery changed. - The function "Water" (checking the moisture value) will be called after it hits a certain amount of Counts (Counter ==5). Right now I have it set to 25 seconds (run Battery every 5 seconds [increment Counter after each loop]) for testing purposes.
- When the "Water" function is running (moisture value is < 900 && reed_val ==LOW), I want it to turn on pump for 5 seconds, then shut it off and read the moisture and reed value again, then start pump again, and continue this sequence until either the moisture value is > 900 or the reed value is HIGH.
The problem that I'm having is only in the "Water" function. If I use
Alarm.delay(5000);
the "Water" function will continually turn on the pump, while (moisture_val < 900 && reed_val ==LOW).
If I add
digitalWrite(waterpump,LOW);
Alarm.delay(5000);
it's inconsistent, waterpump turning on and staying on without delay, or waterpump turning on and then turning off after 5 seconds without restarting until the next time it calls the Water function.
I think that using millis() would help, and it seems so simple - but I keep hitting a brick wall and getting frustrated. Any help would be greatly appreciated.
Here is the entire code so far:
/*
* Gardening Project v1.2.3
* Combining the capacitive sensor, moisture sensor, pump and led indicator.
* Reading how much water is in the container.
* Reading how much moisture is in the ground.
* Pumping water if moisture is dry.
* LED
* Red - low battery , blink
*/
#include <Bounce.h>
#include <Time.h>
#include <TimeAlarms.h>
#define LED 9 // LED on pin 9
#define SWITCH 10 // Defines the Reed switch on digital pin 10
int reed_val = 0; // used to store the input of the reed switch
Bounce bouncer = Bounce( SWITCH,10 );
int Counter = 0; // Set counter
// Volt monitoring
int batteryPin = 1;
float vout = 0;
int value = 0;
float R1 = 991; // !! resistance of R1 !!
float R2 = 995; // !! resistance of R2 !!
float vin = 0;
// Moisture Sensor
int moistureSensor = 5; // Sensor pin 5, other pin connected to GRN through a 10k resistor
int moisture_val; // Storing the current value of the Sensor pin
int waterpump = 7; // Initializing pin 7 for waterpump
void setup() {
Serial.begin(9600);
pinMode(SWITCH, INPUT); // Defines the switch as an input
digitalWrite(SWITCH, HIGH);
pinMode(LED,OUTPUT); // LED as an output
pinMode(waterpump,OUTPUT); // Setting waterpump to output
digitalWrite(waterpump,LOW); // waterpump off
// Time
setTime(0,0,0,13,12,11);
Alarm.timerRepeat(5,Battery);
}
void loop() {
// Display the countdown timer
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
if (Counter == 5) {
Water();
Counter = 0;
}
}
// Start of Functions
void Water() {
analogReference(EXTERNAL);
bouncer.update();
int reed_val= bouncer.read();
moisture_val = analogRead(moistureSensor);
Serial.print("M: ");
Serial.println(moisture_val);
// Check whether input is LOW (switch closed)
while (moisture_val < 900 && reed_val==LOW) {
Serial.println ("Turning on pump"); // Debugging or adding add to LCD
digitalWrite(waterpump,HIGH); // waterpump on
Alarm.delay(5000);
// digitalWrite(waterpump,LOW); // waterpump on
// Alarm.delay(5000);
bouncer.update();
int reed_val= bouncer.read();
moisture_val = analogRead(moistureSensor);
}
if (reed_val==HIGH) {
return;
}
}
void Battery() {
// Volt Monitoring
analogReference(EXTERNAL);
value = analogRead(batteryPin);
vout= (value * 5)/1024.0; //voltage coming out of the voltage divider
vin = vout / (R2/(R1+R2)); //voltage to display
while (vin < 7.0) {
digitalWrite(waterpump,LOW);
digitalWrite(LED,HIGH); // LED turns on
Alarm.delay(200);
digitalWrite(LED,LOW);
Alarm.delay(200);
}
// Debugging with LCD
Serial.print("V: "); // Print out V:
Serial.println(vin); // This is where the number of volts are displayed
// Reed Switch monitoring
bouncer.update();
int reed_val= bouncer.read();
while (reed_val==HIGH) {
Serial.println("Fill Water");
digitalWrite(waterpump,LOW);
digitalWrite(LED,HIGH);
Alarm.delay(200);
digitalWrite(LED,LOW);
Alarm.delay(200);
bouncer.update();
int reed_val= bouncer.read();
if (reed_val==LOW) {
return;
}
}
Counter++;
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}