Hello, I am making a green house by using Arduino mega. It include a LCD display, DHT sensor, light sensor and moisture sensor. Here is my code.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
LiquidCrystal_I2C LCD(0x3F,20,4);
#define dht_dpin 8
dht DHT;
int dht_dpin8;
int tempR;
int Fan=2;
int HeatFan=6;
const int sensorPin=A4;
int pump = 3;
int photocellPin = A5; //
int photocellVal = 0;
int LED1 = 7;
void setup()
{
Serial.begin(9600);
delay(1000);
LCD.init();
LCD.backlight();
pinMode(Fan,OUTPUT);
pinMode(HeatFan,OUTPUT);
pinMode(pump, OUTPUT);
pinMode(sensorPin,INPUT);
pinMode(LED1, OUTPUT);
LCD.setCursor(0, 0);
LCD.print("GreenHouse");
delay(1000);
LCD.setCursor(0, 1);
LCD.print("Monitor Start");
delay(3000);
LCD.clear();
}
void loop()
{
DHT.read11(dht_dpin);
tempR= DHT.temperature;
LCD.setCursor(0,0);
LCD.print("Temp:");
LCD.print(DHT.temperature);
LCD.print("C");
LCD.setCursor(0,1);
LCD.print("Humi:");
LCD.print(DHT.humidity);
LCD.print("%");
delay(3000);
if(tempR >= 26)
{
digitalWrite(Fan,LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Temp High ");
LCD.setCursor(0,1);
LCD.print("Fan On ");
delay(2000);
}
else
{
digitalWrite(Fan,HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Temp Nor ");
LCD.setCursor(0,1);
LCD.print("Fan Off ");
delay(2000);
}
if(tempR < 24)
{
digitalWrite(HeatFan,LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Temp Low ");
LCD.setCursor(0,1);
LCD.print("HeatFan On ");
delay(2000);
}
else {
digitalWrite(HeatFan,HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Temp Nor ");
LCD.setCursor(0,1);
LCD.print("HeatFan Off ");
delay(2000);
}
{
int moist;
moist = analogRead(sensorPin);
Serial.println(moist);
// 乾燥程度大於 700 時,亮燈
if (moist > 700) {
digitalWrite(pump, LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Soil Dry ");
LCD.setCursor(0,1);
LCD.print("Pump On ");
delay(2000);}
else {
digitalWrite(pump, HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Soil Nor ");
LCD.setCursor(0,1);
LCD.print("Pump Off ");
}
delay(2000);
}
photocellVal = analogRead(photocellPin);
Serial.println(photocellVal);
delay(100);
if (photocellVal<1000)
{
digitalWrite(LED1,HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Light Nor ");
LCD.setCursor(0,1);
LCD.print("Light Off ");
delay(2000);}
else {
digitalWrite(LED1, LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("Light Dim ");
LCD.setCursor(0,1);
LCD.print("Light On ");}
delay(2000);
}
This code is working. Then I want to add a PH senor. I use this code and it run normal.
#include <LiquidCrystal_I2C.h>
#define SensorPin 0
LiquidCrystal_I2C LCD(0x3F,20,4);
unsigned long int avgValue;
float b;
int buf[10],temp;
int pump = 3;
int pump1 =4;
void setup()
{
LCD.init(); //初始化 LCD 模組
LCD.backlight(); //設定 LCD 背光模式
pinMode(SensorPin, INPUT);
pinMode(pump, OUTPUT);
pinMode(pump1, OUTPUT);
Serial.begin(9600);
Serial.println("Ready");
LCD.setCursor(0, 0);
LCD.print("GreenHouse");
delay(1000);
LCD.setCursor(0, 1);
LCD.print("Monitor Start");
delay(3000);
LCD.clear(); //
}
void loop()
{
for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i]=analogRead(SensorPin);
delay(10);
}
for(int i=0;i<9;i++) //sort the analog from small to large
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++) //take the average value of 6 center sample
avgValue+=buf[i];
float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt
phValue=3.5*phValue; //convert the millivolt into pH value
Serial.print(" pH:");
Serial.print(phValue,2);
Serial.println(" ");
{
if (phValue > 11)
{
digitalWrite(pump, LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("PH High ");
LCD.setCursor(0,1);
LCD.print("pump1 On ");
delay(2000);}
else {
digitalWrite(pump, HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("PH Nor ");
LCD.setCursor(0,1);
LCD.print("pump1 Off ");
delay(2000);
}
if (phValue < 10) {
digitalWrite(pump1, LOW);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("PH High ");
LCD.setCursor(0,1);
LCD.print("pump2 On ");
delay(2000);}
else {
digitalWrite(pump1, HIGH);
LCD.clear();
LCD.setCursor(0,0);
LCD.print("PH Nor ");
LCD.setCursor(0,1);
LCD.print("pump2 Off ");
delay(2000);
}
}
}
Then I put the second code into my first code. It run abnormal and show the following message.
"Arduino: 1.8.7 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
111:142:1: error: expected unqualified-id before '{' token
{
^
exit status 1
expected unqualified-id before '{' token
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences."
Can anyone help me to fix this, thank you!!!
