Hi all,
when I decided to buy and Arduino, I set myself the task of automating my garden, and running all the lights pumps and so on from a solar panel, that part I can do but the coding I am not so sure about
I have spent the last few weeks trying to learn coding and putting together circuits, and I have achieved a lot I think but I was wondering if I could get some help making my code a little more stable and concise I have a feeling I have probably made it a little messy.
what's in the circuit :-
1 x temp & humidity sensor
1 x light /dark sensor
1 x moisture sensor (analogue)
1 x PIR (parallax type)
and 4 port relay output
LCD 16x2 display
when LCD displays temp, humidity, index, moisture and a symbol when light sensor picks up light.
if the moisture level falls below 15% turns on relay 4 and off above 45%
if it is day time or its night and there is motion turns on relay's 2 and 3
if its night time and there is motion then relay 1 is turned on.
relays 1,2 and 3 have a transition delay so relays don't flicker.
I have attached the code i wrote
I tried to make sure that delay isn't used and worked with millis but this is the first time for me to write code.
I noticed that every time the sensors refresh (2 seconds) the display flickers, other than that it works great but i think some of it could have been written a little more condensed
any help would be really appreciated.
Garden_auto_triggered_relays.ino (8.42 KB)
Consider the following code fragment:
if ((digitalRead(ltsensorPin) == 1)) {
if (nFountain == true) {
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
else
{
if (currentMillis - previousMillis4 > intervallt) { // Start Test block #1
previousMillis4 = currentMillis;
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
}
}
else
{
if (currentMillis - previousMillis4 > intervallt) { // Start Test block #2
previousMillis4 = currentMillis;
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
}
I'm not really sure what you're trying to do here, but the two test blocks are the same except for setting the relays. It appears that your saying that if ltSensorPin is 1 and nFountain is true, turn off relays 2 and 3. If ltSensorPin is 1 but nFountain is false and we've waited "long enough", turn the relays on. f ltSensorPin is not 1 and there's been enough time passed, turn the relays off.
Could you make this easier to understand by just doing the time check stuff once near the top of the code block and then call a function to turn those relays either on or off based on those conditions?
It would help if you broke out the functional areas into separate functions:
- Initilaization -- most of what you do in setup()
- input -- a function that collects the relevant data
- Process -- a function that takes that data and transforms it to whatever it is you need
- Display -- a function that takes the output data and displays it on the LCD display.
Having all these functional parts as one huge code block inside loop() is very difficult to follow. Break the functional parts out into separate functions. It will be easier to test, debug, and maintain.