Code issues, greenhouse project

Hey everyone, I'm a absolute total noob just having trouble getting this program to compile. I'm hoping someone out there can help push me in the right direction. I'm not looking to learn a whole lot but I'm just setting this up for my mother who is not able to do a lot anymore due to some health concerns but she wants to still tend to her greenhouse as much as possible. All I'm trying to do is set up a little bit of automation for her. I got this code off a guy on YouTube who seemed to get his going well. Below is the code and the errors I get after that.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

// Pin definitions
const int soilSensorPin = A0;
const int pumpRelayPin = 2;
const int growLampRelayPin = 3;
const int sensorPowerPin = 6;
const int dht11Pin = 7;

const int soilThreshold = 100; // Adjust according to your desired soil moisture threshold

// Timings
const unsigned long growLampOnDuration = 17 * 60 * 60 * 1000UL; // 17 hours in milliseconds
const unsigned long growLampOffDuration = 7 * 60 * 60 * 1000UL - growLampOnDuration;
unsigned long growLampTimer = 0;
const unsigned long sensorCheckDuration = 2 * 1000UL; // 2 seconds in milliseconds
const unsigned long sensorCheckInterval = 12 * 60 * 60 * 1000UL; // 12 hours in milliseconds
unsigned long sensorCheckTimer = 0;
const unsigned long pumpDuration = 6 * 1000UL; // 6 seconds in milliseconds
unsigned long pumpTimer = 0;
const unsigned long dht11CheckInterval = 1 * 60 * 1000UL; // 1 minute in milliseconds
unsigned long dht11CheckTimer = 0;

// Initialize display
LiquidCrystal_I2C lcd(0x27, 20, 4);

// Initialize DHT11 sensor
DHT dht11(dht11Pin, DHT11);

void setup() {
  Serial.begin(9600);
  pinMode(soilSensorPin, INPUT);
  pinMode(pumpRelayPin, OUTPUT);
  pinMode(growLampRelayPin, OUTPUT);
  pinMode(sensorPowerPin, OUTPUT);
  dht11.begin();
 
  // Initialize LCD display
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Greenhouse System");

  // Turn grow lamp on
  digitalWrite(growLampRelayPin, HIGH);
  growLampTimer = millis();
}

void loop() {
  unsigned long currentTime = millis();
  int soilMoisture = 0;
 
  // Check soil moisture every 12 hours
  if (currentTime - sensorCheckTimer >= sensorCheckInterval) {
    digitalWrite(sensorPowerPin, HIGH);
    delay(sensorCheckDuration);
    soilMoisture = analogRead(soilSensorPin);
    digitalWrite(sensorPowerPin, LOW);
    //reset sensor check timer
    sensorCheckTimer=currentTime;

    bool needsWatering = soilMoisture < soilThreshold;
    if (needsWatering) {
      digitalWrite(pumpRelayPin, HIGH);
      pumpTimer = currentTime;
      delay(pumpDuration);
      digitalWrite(pumpRelayPin, LOW);
    }

    lcd.setCursor(0, 0);
    lcd.print("Moisture: ");
    lcd.print(soilMoisture);
    lcd.print(" units");

    lcd.setCursor(0, 1);
    lcd.print(needsWatering ? "Watering..." : "Healthy");
  }

  // Check temperature and humidity every minute
  if (currentTime - dht11CheckTimer >= dht11CheckInterval) {
    float humidity = dht11.readHumidity();
    float temperature = dht11.readTemperature();

    // Convert temperature to Fahrenheit
    temperature = temperature * 1.8 + 32;

    lcd.setCursor(0, 2);
    lcd.print("T: ");
    lcd.print(temperature);
    lcd.print((char)223); // degree symbol
    lcd.print("F H: ");
    lcd.print(humidity);
    lcd.print("%");

    dht11CheckTimer = currentTime;
  }

  // Control grow lamp: on for 17 hours, off for 7 hours
  if (currentTime - growLampTimer >= growLampOnDuration && digitalRead(growLampRelayPin) == HIGH) {
    digitalWrite(growLampRelayPin, LOW);
  } else if (currentTime - growLampTimer >= growLampOffDuration && digitalRead(growLampRelayPin) == LOW) {
    digitalWrite(growLampRelayPin, HIGH);
    growLampTimer = currentTime;
}
````Use code tags to format code for the forum`


Thanks in advance and please don't punish me too much if I am a absolute idiot.
Cheers

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

I suspect that you copied the code from a web page and hence it has HTML code within it, albeit invisible

Copy the code from your first post into the IDE, select "copy for forum" and paste it back here. The code tags will be added. In the IDE try compiling the code again and post any remaining errors, using code tags. Copying back from the forum usually removes most, if not all, of the HTML characters

1 Like

That sounds like you have some junk characters in your file: \302 would be a character with Octal code 3028 - that's 0xC2.
\240 is 0xA0

You seem to have that pair of characters at the start of every line

Have you tried looking at this file in a text editor?

How did you download it? It looks like it's corrupt...

It looks like a copy/paste from a Web page to me

1 Like

Yes, I think so.

(didn't see your earlier reply until after posting)

hi yes it was a copy and paste from a website. Should i try rewrite it or find the original?

Start by doing as I suggested in reply #3

Basically, copy the code back into the IDE from the forum

1 Like

@junglesponge

PLEASE corral the error report into a < CODE > box. : )

Paste into IDE. Use find/replace tool (CTRL-H) to mass-replace.


Arduino: 1.8.18 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_145298\sketch_sep27a.ino:15:50: warning: integer overflow in expression [-Woverflow]

 const unsigned long growLampOnDuration = 17 * 60 * 60 * 1000UL; // 17 hours in milliseconds

                                          ~~~~~~~~^~~~

C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_145298\sketch_sep27a.ino:19:51: warning: integer overflow in expression [-Woverflow]

 const unsigned long sensorCheckInterval = 12 * 60 * 60 * 1000UL; // 12 hours in milliseconds

                                           ~~~~~~~~^~~~

C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_145298\sketch_sep27a.ino: In function 'void loop()':

sketch_sep27a:106:1: error: expected '}' at end of input

 }

 ^

exit status 1

expected '}' at end of input



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

so heaps better just a couple more left

expected '}' at end of input

You have a missing } at the end of the code

1 Like

Line 106 is missing a close-brace... probably lost in the copy/paste... so refer to the original sketch to find where the close-brace belongs.

so it seems to have worked, and it has compiled it. but i do have these two lines still in the error page.

C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_376101\sketch_sep27a.ino:15:50: warning: integer overflow in expression [-Woverflow]
 const unsigned long growLampOnDuration = 17 * 60 * 60 * 1000UL; // 17 hours in milliseconds
                                          ~~~~~~~~^~~~
C:\Users\admin\AppData\Local\Temp\arduino_modified_sketch_376101\sketch_sep27a.ino:19:51: warning: integer overflow in expression [-Woverflow]
 const unsigned long sensorCheckInterval = 12 * 60 * 60 * 1000UL; // 12 hours in milliseconds
                                           ~~~~~~~~^~~~
Sketch uses 8822 bytes (3%) of program storage space. Maximum is 253952 bytes.
Global variables use 602 bytes (7%) of dynamic memory, leaving 7590 bytes for local variables. Maximum is 8192 bytes.

Change to

const unsigned long growLampOnDuration = 17L * 60 * 60 * 1000;
1 Like

Awesome guys, thank you so much. I will test when i get back from work today. I cant wait to get it going for my mum. She's gonna be stoked.

Thanks again guys, i know this stuff is simple for you most the time and you must roll your eyes at my type of gumpy knowledge but I'm super thankful. I might need to ask more questions in regards to this.

Also what is a good source for learning this type of code? even on a basics level so I don't need to bother this forum too much.

I found the original sketch compiles without error (as opposed to "with warning").

You can get rid of the warning, but the result is still wrong.

Try this:

const unsigned long a = 17 * 60 * 60 * 1000UL;
const unsigned long b = 17L * 60 * 60 * 1000;

void setup() {
  Serial.begin(115200);
  Serial.print("a = "); Serial.println(a);
  Serial.print("b = "); Serial.println(b);
}

void loop() {
}
1 Like

Yes, verified "L" type is needed on every long calculation in the sketch.

Check your time. This will be a negative time.

Once this works, consider using a real time clock (GBP 2,00), rather than waiting 17 hours... here is an example of using RTC to schedule events.

ok guys so those settings you gave me helped a lot but i am having a new issue. I am having so much trouble with the soil moisture sensor. I originally got this code because I was having trouble with the first code I had with the soil sensor. I just cant seem to get the soil moisture to work properly, when the soil is wet it is turning on the pump to add more water and even with the humidity sensor I feel it is working with the soil sensor in a way that the grow lamp is on at the wrong time.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

// Pin definitions
const int soilSensorPin = A0;
const int pumpRelayPin = 2;
const int growLampRelayPin = 3;
const int sensorPowerPin = 6;
const int dht11Pin = 7;

const int soilThreshold = 100; // Adjust according to your desired soil moisture threshold

// Timings
const unsigned long growLampOnDuration = 0 * 60 * 60 * 1000UL; // 17 hours in milliseconds
const unsigned long growLampOffDuration = 0 * 60 * 60 * 1000UL - growLampOnDuration;
unsigned long growLampTimer = 0;
const unsigned long sensorCheckDuration = 1 * 1000UL; // 2 seconds in milliseconds
const unsigned long sensorCheckInterval = 0.001 * 60 * 60 * 1000UL; // 12 hours in milliseconds
unsigned long sensorCheckTimer = 0;
const unsigned long pumpDuration = 6 * 1000UL; // 6 seconds in milliseconds
unsigned long pumpTimer = 0;
const unsigned long dht11CheckInterval = .5 * 60 * 1000UL; // 1 minute in milliseconds
unsigned long dht11CheckTimer = 0;

// Initialize display
LiquidCrystal_I2C lcd(0x27, 20, 21);

// Initialize DHT11 sensor
DHT dht11(dht11Pin, DHT11);

I have changed the timings so i can test in a quicker manner so if you go to the top of this thread you will see original code. I'm not even certain i have the timing setting right.

void loop()
{
  unsigned long currentTime = millis();
  int soilMoisture = 0;

  // Check soil moisture every 12 hours
  if (currentTime - sensorCheckTimer >= sensorCheckInterval)
  {
    digitalWrite(sensorPowerPin, HIGH);
    delay(sensorCheckDuration);
    soilMoisture = analogRead(soilSensorPin);
    digitalWrite(sensorPowerPin, LOW);
    //reset sensor check timer
    sensorCheckTimer = currentTime;

    bool needsWatering = soilMoisture < soilThreshold;
    if (needsWatering)
    {
      digitalWrite(pumpRelayPin, HIGH);
      pumpTimer = currentTime;
      delay(pumpDuration);
      digitalWrite(pumpRelayPin, LOW);
    }

    lcd.setCursor(0, 0);
    lcd.print("Moisture: ");
    lcd.print(soilMoisture);
    lcd.print(" units");

    lcd.setCursor(0, 1);
    lcd.print(needsWatering ? "Watering..." : "Healthy");
  }

  // Check temperature and humidity every minute
  if (currentTime - dht11CheckTimer >= dht11CheckInterval)
  {
    float humidity = dht11.readHumidity();
    float temperature = dht11.readTemperature();

    // Convert temperature to Fahrenheit
    temperature = temperature * 1.8 + 32;

What I want is that when soil gets dry it turns on water pump relay for a certain amount of time like 3mins and when the heat is to hot it will turn on a fan I have installed in the green house to keep temp at a certain level.



this is the pin layout that I am running

and this is the led but i am waiting on a part to connect this up

I'm not sure if this is the right ino for what I want to achieve but the ino I had before did the temp beautifully but the soil sensor wouldn't work. I have tried 3 soil sensors on both. I have added the ino link below for the other ino code if someone might like to look at it as it might be the better choice for me.

https://content.instructables.com/FT0/WU7T/KBUST015/FT0WU7TKBUST015.ino

thanks everyone hope that you can help me. Its driving me mad hahahaha