You can make your life a lot simpler by using one of the many DS1307 libraries to take the hard work out of reading/writing time. There are many versions and everyone has their favourite so I will suggest this one as mine. Download, rename and install the library in the correct place and then load it's example to get an idea of how the library works.
These don't need to be float values so would be better changed to 'int'
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
It makes your life (and others helping you) easier if you give the pins meaningful names. You have done this with A0 sensorPin.
You have 'void setup()' where you put initialization commands but you have no 'void loop()' in your code and this is the main core of your program (the bit you want to keep repeating).
Changed it on your recommendations, think it must be something like this. Later on I would like to be able to put a fan control and a webcam control added to the code. Does someone has recommendations already?
#include <RTCDS1307.h>
RTCDS1307 myRTC; // giving it a name
// named constant for the pin the sensor is connected to
const int moistSensorPin = A0;
const int waterPin = 2;
const int lightPin = 3;
int baselineMoist = 20.0; // Minimal moist level
int LightOn = 12; // Hour to put Lights on: 12:00
int LightOff= 22; // Hour to put Lights off: 22:00
void setup(){
// open a serial connection to display values
Serial.begin(9600);
// pinmode set up for the irrigation and the lighting both begin LOW
pinMode(waterPin, OUTPUT);
pinMode(lightPin, OUTPUT);
digitalWrite(waterPin, LOW);
digitalWrite(lightPin, LOW);
int sensorVal = analogRead(moistSensorPin);
myRTC.attach(9); // attaches the RTC to one or more pins... still un clear
float moistValue = (sensorValue); // convert the ADC reading to moist level still needs an equation, I did not choose a moist sensor to work with
[code]
void loop(){
myRTC readClock;
Serial.print(" RTC Time: "(readClock)); // to check if the RTC gives the right time
// LIGHT REGULATION LIGHT ON/OFF
myRTC getHours;
//starts to turn on light at 12:00 and turns of light at 22:00
if (getHours > LightOn && getHours < LightOff ){
digitalWrite(3,HIGH);
else (getHours < LightOn && getHours > LightOff ){
digitalWrite(3,LOW);
// WATERING OPEN A VALVE >>>>>>>>>
// Send the voltage level out the Serial port, possibly print for check up
Serial.print(", : ");
Serial.print(moistvalue);
if(moistValue > baselineMoist){
digitalWrite(2, LOW);
}
// if the moist level is above sensorVal xx actuator 1 turns on for five minutes or turns of when BaseMoist is reached.
else if(moistValue <= baselineMoist){
digitalWrite(2, HIGH);
for (float moistValue => baslineMoist); // loop as long as the moist value is lower than baselineMoist
digitalWrite(2, LOW);
{
}
}
}
To OP: If you're interested, my first Arduino project is a touch-screen Polytunnel controller (very remote 28ft x 8ft PT with a solar/battery set-up), incorporating a side-vent height control (motor driven) coupled to the temperature/humidity sensor, water level monitor (1000l IBC) with pre-dawn irrigation, temperature/water/vent position trending over day, month, year, all controlled with a single mega. I'm watching your thread with interest, by all means PM with specifics comments, advice or cunning ideas.
michiel1984:
Changed it on your recommendations, think it must be something like this. Later on I would like to be able to put a fan control and a webcam control added to the code. Does someone has recommendations already?
If you have everything installed correctly there is no reason your code should not at least compile using the verify button even if you don't have any/all the hardware. There are several errors that prevented compilation though. Unfortunately I may be off grid for a week or more so have posted some changes that should allow the code to compile but will need additions from you/someone to finish the job.
#include <Wire.h>
#include <RealTimeClockDS1307.h>
RealTimeClockDS1307 myRTC;
char formatted[] = "00-00-00 00:00:00x";
// named constant for the pin the sensor is connected to
const int moistSensorPin = A0;
const int wateringPin = 2;
const int lightPin = 3;
int baselineMoist = 20; // Minimal moist level
int LightOn = 12; // Hour to put Lights on: 12:00
int LightOff= 22; // Hour to put Lights off: 22:00
void setup(){
myRTC.set24h();
// open a serial connection to display values
Serial.begin(9600);
// pinmode set up for the irrigation and the lighting both begin LOW
pinMode(wateringPin, OUTPUT);
digitalWrite(wateringPin, LOW);
pinMode(lightPin, OUTPUT);
digitalWrite(lightPin, LOW);
}
void loop(){
myRTC.readClock();
Serial.print(" RTC Time: "); // to check if the RTC gives the right time
RTC.getFormatted(formatted);
Serial.println(formatted);
int h = myRTC.getHours();
//starts to turn on light at 12:00 and turns of light at 22:00
if (h >= LightOn && h < LightOff ){
digitalWrite(lightPin,HIGH);
}
else {
digitalWrite(lightPin,LOW);
}
int moistvalue = analogRead(moistSensorPin);
// Send the voltage level out the Serial port, possibly print for check up
// Serial.print(", : ");
// Serial.println(moistvalue);
if(moistvalue > baselineMoist){
digitalWrite(wateringPin, LOW);
}
else {
digitalWrite(wateringPin, HIGH);
}
delay(5000);
}
So the code is in the attachement. I know the pins are not valid in this manner.
So is it too long for an arduino uno? Or should I work with another controller.
I adjusted the parts from Billy's hydrophonics code. Thanks for that!
For coding corrections I am only interested in the parts dealing with light and soil humidity which I adapted. This is Setup, and at the end of the code. So do not review the whole file in detail. (for your own benefit)
I did not compile it yet.
Neither do I have the parts here for building.
unsigned int -- 16 bits, holds 0 - 65535
int -------------- 16 bits, holds -32768 - 32767
If you use a 16 bit variable to hold what an 8 bit variable will, you lose a byte right there.
Thanks, very clarifying. Can you get any smaller than the byte or shore variable?
For instance I am using a 24 hour clock this can be expressed in 2 4
4 bits can count 0-15 and you can 'pack' 2 4-bit values into a single byte. Unless you are doing many of those it's not worth the code. You won't fit 23 or 24 into 4 bits anyway.
I don't see why you bother running off a clock except possibly to pump water up to a tank during off-peak hours to save on electric and that's it. When the plants need water or lights turned on or shading from too much sun or venting, etc, should depend on conditions, not simply time. A cloudy day will differ from a sunny day but for both time is the same. You can monitor conditions and be more efficiently fit the needs of your plants.