Components and supplies
Arduino UNO Wifi Rev.2
Project description
Code
Automated gardening kit
c_cpp
1<p>//import the library to control LCD display, this library has been added manually through ZIP file<br>#include 2</p><p>//import library for DHT sensor 3#include "dht.h" 4#define dht_apin A3 // Analog Pin sensor is connected to</p><p>//create DHT object 5dht DHT;</p><p>// create LCD object 6LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display</p><p>//declaration of variable for buttond 7int ButtonOnePin = 10; //pin for button 1 8int ButtonOneState =0 ; //one variable to store current button state 9int ButtonOnePreviousState = 0; //one variable to store previous button state 10int ButtonTwoPin = 9; // same for button 2 11int ButtonTwoState =0 ; 12int ButtonTwoPreviousState = 0;</p><p>unsigned long timeBeginDisplay=0; 13int timeDisplayPeriod = 6000; //period the 2nd and 3rd screen are displayed 14int valToDisplay = 0; //variable to decide what information is to be displayed</p><p>int airTemp; //variable to store air temperature measured by DHT11 sensor 15int airHumidity; //variable to store air humidity measured by DHT11 sensor</p><p>int LedPin = 13; </p><p>int waterPumpPin=7; // pin on which is connected the relay that command water pump 16int sprayNum=0; //variable to store number of spraying 17int pumpOn =false; //variable used for spraying, it stays ON during the spray timing</p><p>int MoistureHigh = 800; 18int MoistureGood = 710; 19int MoistureLow = 630; //threshold that should launch water spray of the plant 20int valSoil ; //variable to store value measured by moisture sensor 21int soilPin = A0; // pin on wich is connected moisture sensor 22int soilPower = 12;</p><p>int timePeriod = 1000; //variable to store timing in ms, period between each display refresh, a clear is called each time display is called 23int humMeasurePeriod=0; 24unsigned long currentMillis; //variable to store current timing 25unsigned long debutMillis=0; //variable used to store timing at the debut of a period 26int timePumpPeriod = 3000; //timing for water spraying 27unsigned long timePumpDebut = 0; //variable used to store timing value for the debut of a spraying period</p><p>int gazPin = 1; //pin on which is connected gaz sensor 28int valGaz ; //variable on which is stored gaz sensor value</p><p>int lightPin = 2; //pin on which is connected the photoresistor 29int valLight ; //variable on which light value is stored</p><p>//function to read moisture sensor value 30int readSoil() 31{</p><p> digitalWrite(soilPower, HIGH);//turn D7 "On" 32 delay(10);//wait 10 milliseconds 33 valSoil = analogRead(soilPin);//Read the SIG value form sensor 34 digitalWrite(soilPower, LOW);//turn D7 "Off" 35 return valSoil;//send current moisture value 36}</p><p>//function called to refresh LCD screen, displaying wanted screen regarding value of variable "valToDisplay" 37int toDisplay(){</p><p> lcd.clear(); //clear the screen to remove character that might not be overwritten by new message 38 39 if (valToDisplay==0){ 40 lcd.setCursor(0,0); //to put cursor on 1rst character of 1rst line 41 lcd.print("Temp:"); 42 lcd.setCursor(6,0); 43 lcd.print("Hum:"); 44 lcd.setCursor(11,0); 45 lcd.print("Mois:"); 46 lcd.setCursor(0,1); //to put cursor on 1rst character of 2nd line 47 lcd.print(airTemp); 48 lcd.setCursor(2,1); 49 lcd.print((char)223); //special character for temperature 50 lcd.print("C"); 51 lcd.setCursor(6,1); 52 lcd.print(airHumidity); 53 lcd.setCursor(8,1); 54 lcd.print("%"); 55 lcd.setCursor(11,1); 56 lcd.print(valSoil); 57 } 58 else if (valToDisplay==1 && (currentMillis-timeBeginDisplay)</p><p> if((currentMillis-timeBeginDisplay)>timeDisplayPeriod){ //display original screen when display period is over 59 valToDisplay=0; 60 //we display in this IF condition the 1rst screen because before there was a blank screen after the timing 61 lcd.setCursor(0,0); //to put cursor on 1rst character of 1rst line 62 lcd.print("Temp:"); 63 lcd.setCursor(6,0); 64 lcd.print("Hum:"); 65 lcd.setCursor(11,0); 66 lcd.print("Mois:"); 67 lcd.setCursor(0,1); //to put cursor on 1rst character of 2nd line 68 lcd.print(airTemp); 69 lcd.setCursor(2,1); 70 lcd.print((char)223); //special character for temperature 71 lcd.print("C"); 72 lcd.setCursor(6,1); 73 lcd.print(airHumidity); 74 lcd.setCursor(8,1); 75 lcd.print("%"); 76 lcd.setCursor(11,1); 77 lcd.print(valSoil); 78 } 79 80}</p><p>int readTempHum (){</p><p> DHT.read11(dht_apin); //read value from DHT sensor</p><p> airTemp = DHT.temperature; //store temperature value from sensor 81 airHumidity = DHT.humidity; // store humidity value from sensor 82 83 //delay(3000);//Wait 3 seconds before accessing sensor again, without timing, sensor measurement is faulty. we commented this delay because this function isn't called often 84}</p><p>int waterPump(){</p><p> //the condition below to command or not the water pump 85 if (valSoil</p><p> //once the waterpump cycle is launched, this if condition will turn ON the pump on the third of the timePumpPeriod defined, the other 2 third, the water pump will be turned OFF 86 if (currentMillis-timePumpDebut=timePumpPeriod && pumpOn==true){ 87 pumpOn=false; //once pump cycle is over, variable value change to make water spraying dependant on moisture value again 88 sprayNum++; //add +1 to the number of spraying performed 89 } 90}</p><p>int waterTankAlarm(){ 91 if (sprayNum>10){ //when number of spraying is above limit, turn on led to warn user 92 digitalWrite(LedPin,HIGH); 93 } 94 else { 95 digitalWrite(LedPin,LOW); 96 } 97}</p><p>void setup() { 98 // put your setup code here, to run once: 99 Serial.begin(9600); 100 //defining wich pin are IN or OUT 101 pinMode(LedPin, OUTPUT); 102 pinMode(ButtonOnePin, INPUT); 103 pinMode(ButtonTwoPin, INPUT); 104 pinMode(waterPumpPin, OUTPUT); </p><p> // initialize the lcd 105 lcd.init(); 106 lcd.backlight(); 107 lcd.setCursor(0,0); 108 lcd.print("Waking up");</p><p> //ready first temperature and humidity from DHT sensor to store value (since this function isn't called often) 109 readTempHum(); 110 111 delay(2000); 112 lcd.clear();</p><p> 113}</p><p>void loop() {</p><p> //reading of sensor value, not DHT due to the delay needed to its measurement 114 readSoil(); 115 valGaz = analogRead(gazPin); 116 valLight = analogRead(lightPin);</p><p> currentMillis=millis(); //measurement of current time</p><p> //small function to avoid button rebound, when pressing once on the button, arduino reads only one pressing... 117 ButtonOneState=digitalRead(ButtonOnePin); //reading of button value 118 if (ButtonOneState !=ButtonOnePreviousState){ //comparing with previous state 119 if (ButtonOneState == HIGH){ //if previous state was 0 and current is 1, then take button pressing in account 120 delay(200); 121 valToDisplay=1; 122 timeBeginDisplay=currentMillis; 123 } 124 ButtonOnePreviousState=ButtonOneState; 125 } 126 //same function for button 2 as button 1 127 ButtonTwoState=digitalRead(ButtonTwoPin); 128 if (ButtonTwoState !=ButtonTwoPreviousState){ 129 if (ButtonTwoState == HIGH){ 130 delay(200); 131 valToDisplay=2; //particular value for "valToDisplay" 132 timeBeginDisplay=currentMillis; 133 } 134 ButtonTwoPreviousState=ButtonTwoState; 135 } 136 137 waterPump(); //calling for water pump function which launch spray cycle if needed</p><p> waterTankAlarm(); //calling function to warn user about water tank level</p><p> //small condition for screen refreshing and measurement of DHT sensor 138 if (currentMillis-debutMillis>timePeriod){ //end of a period 139 debutMillis=currentMillis; //define the begining of a new period 140 toDisplay(); //refresh of the screen regarding "valToDisplay" value 141 humMeasurePeriod++; //DHT sensor values are measured every 20 times the screens is refreshed 142 if (humMeasurePeriod>20){ //go to measure DHT values 143 readTempHum(); 144 humMeasurePeriod=0; 145 }</p><p> } </p><p>}</p>
Automated gardening kit
c_cpp
1<p>//import the library to control LCD display, this library has been added manually through ZIP file<br>#include 2</p><p>//import library for DHT sensor 3#include "dht.h" 4#define dht_apin A3 // Analog Pin sensor is connected to</p><p>//create DHT object 5dht DHT;</p><p>// create LCD object 6LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display</p><p>//declaration of variable for buttond 7int ButtonOnePin = 10; //pin for button 1 8int ButtonOneState =0 ; //one variable to store current button state 9int ButtonOnePreviousState = 0; //one variable to store previous button state 10int ButtonTwoPin = 9; // same for button 2 11int ButtonTwoState =0 ; 12int ButtonTwoPreviousState = 0;</p><p>unsigned long timeBeginDisplay=0; 13int timeDisplayPeriod = 6000; //period the 2nd and 3rd screen are displayed 14int valToDisplay = 0; //variable to decide what information is to be displayed</p><p>int airTemp; //variable to store air temperature measured by DHT11 sensor 15int airHumidity; //variable to store air humidity measured by DHT11 sensor</p><p>int LedPin = 13; </p><p>int waterPumpPin=7; // pin on which is connected the relay that command water pump 16int sprayNum=0; //variable to store number of spraying 17int pumpOn =false; //variable used for spraying, it stays ON during the spray timing</p><p>int MoistureHigh = 800; 18int MoistureGood = 710; 19int MoistureLow = 630; //threshold that should launch water spray of the plant 20int valSoil ; //variable to store value measured by moisture sensor 21int soilPin = A0; // pin on wich is connected moisture sensor 22int soilPower = 12;</p><p>int timePeriod = 1000; //variable to store timing in ms, period between each display refresh, a clear is called each time display is called 23int humMeasurePeriod=0; 24unsigned long currentMillis; //variable to store current timing 25unsigned long debutMillis=0; //variable used to store timing at the debut of a period 26int timePumpPeriod = 3000; //timing for water spraying 27unsigned long timePumpDebut = 0; //variable used to store timing value for the debut of a spraying period</p><p>int gazPin = 1; //pin on which is connected gaz sensor 28int valGaz ; //variable on which is stored gaz sensor value</p><p>int lightPin = 2; //pin on which is connected the photoresistor 29int valLight ; //variable on which light value is stored</p><p>//function to read moisture sensor value 30int readSoil() 31{</p><p> digitalWrite(soilPower, HIGH);//turn D7 "On" 32 delay(10);//wait 10 milliseconds 33 valSoil = analogRead(soilPin);//Read the SIG value form sensor 34 digitalWrite(soilPower, LOW);//turn D7 "Off" 35 return valSoil;//send current moisture value 36}</p><p>//function called to refresh LCD screen, displaying wanted screen regarding value of variable "valToDisplay" 37int toDisplay(){</p><p> lcd.clear(); //clear the screen to remove character that might not be overwritten by new message 38 39 if (valToDisplay==0){ 40 lcd.setCursor(0,0); //to put cursor on 1rst character of 1rst line 41 lcd.print("Temp:"); 42 lcd.setCursor(6,0); 43 lcd.print("Hum:"); 44 lcd.setCursor(11,0); 45 lcd.print("Mois:"); 46 lcd.setCursor(0,1); //to put cursor on 1rst character of 2nd line 47 lcd.print(airTemp); 48 lcd.setCursor(2,1); 49 lcd.print((char)223); //special character for temperature 50 lcd.print("C"); 51 lcd.setCursor(6,1); 52 lcd.print(airHumidity); 53 lcd.setCursor(8,1); 54 lcd.print("%"); 55 lcd.setCursor(11,1); 56 lcd.print(valSoil); 57 } 58 else if (valToDisplay==1 && (currentMillis-timeBeginDisplay)</p><p> if((currentMillis-timeBeginDisplay)>timeDisplayPeriod){ //display original screen when display period is over 59 valToDisplay=0; 60 //we display in this IF condition the 1rst screen because before there was a blank screen after the timing 61 lcd.setCursor(0,0); //to put cursor on 1rst character of 1rst line 62 lcd.print("Temp:"); 63 lcd.setCursor(6,0); 64 lcd.print("Hum:"); 65 lcd.setCursor(11,0); 66 lcd.print("Mois:"); 67 lcd.setCursor(0,1); //to put cursor on 1rst character of 2nd line 68 lcd.print(airTemp); 69 lcd.setCursor(2,1); 70 lcd.print((char)223); //special character for temperature 71 lcd.print("C"); 72 lcd.setCursor(6,1); 73 lcd.print(airHumidity); 74 lcd.setCursor(8,1); 75 lcd.print("%"); 76 lcd.setCursor(11,1); 77 lcd.print(valSoil); 78 } 79 80}</p><p>int readTempHum (){</p><p> DHT.read11(dht_apin); //read value from DHT sensor</p><p> airTemp = DHT.temperature; //store temperature value from sensor 81 airHumidity = DHT.humidity; // store humidity value from sensor 82 83 //delay(3000);//Wait 3 seconds before accessing sensor again, without timing, sensor measurement is faulty. we commented this delay because this function isn't called often 84}</p><p>int waterPump(){</p><p> //the condition below to command or not the water pump 85 if (valSoil</p><p> //once the waterpump cycle is launched, this if condition will turn ON the pump on the third of the timePumpPeriod defined, the other 2 third, the water pump will be turned OFF 86 if (currentMillis-timePumpDebut=timePumpPeriod && pumpOn==true){ 87 pumpOn=false; //once pump cycle is over, variable value change to make water spraying dependant on moisture value again 88 sprayNum++; //add +1 to the number of spraying performed 89 } 90}</p><p>int waterTankAlarm(){ 91 if (sprayNum>10){ //when number of spraying is above limit, turn on led to warn user 92 digitalWrite(LedPin,HIGH); 93 } 94 else { 95 digitalWrite(LedPin,LOW); 96 } 97}</p><p>void setup() { 98 // put your setup code here, to run once: 99 Serial.begin(9600); 100 //defining wich pin are IN or OUT 101 pinMode(LedPin, OUTPUT); 102 pinMode(ButtonOnePin, INPUT); 103 pinMode(ButtonTwoPin, INPUT); 104 pinMode(waterPumpPin, OUTPUT); </p><p> // initialize the lcd 105 lcd.init(); 106 lcd.backlight(); 107 lcd.setCursor(0,0); 108 lcd.print("Waking up");</p><p> //ready first temperature and humidity from DHT sensor to store value (since this function isn't called often) 109 readTempHum(); 110 111 delay(2000); 112 lcd.clear();</p><p> 113}</p><p>void loop() {</p><p> //reading of sensor value, not DHT due to the delay needed to its measurement 114 readSoil(); 115 valGaz = analogRead(gazPin); 116 valLight = analogRead(lightPin);</p><p> currentMillis=millis(); //measurement of current time</p><p> //small function to avoid button rebound, when pressing once on the button, arduino reads only one pressing... 117 ButtonOneState=digitalRead(ButtonOnePin); //reading of button value 118 if (ButtonOneState !=ButtonOnePreviousState){ //comparing with previous state 119 if (ButtonOneState == HIGH){ //if previous state was 0 and current is 1, then take button pressing in account 120 delay(200); 121 valToDisplay=1; 122 timeBeginDisplay=currentMillis; 123 } 124 ButtonOnePreviousState=ButtonOneState; 125 } 126 //same function for button 2 as button 1 127 ButtonTwoState=digitalRead(ButtonTwoPin); 128 if (ButtonTwoState !=ButtonTwoPreviousState){ 129 if (ButtonTwoState == HIGH){ 130 delay(200); 131 valToDisplay=2; //particular value for "valToDisplay" 132 timeBeginDisplay=currentMillis; 133 } 134 ButtonTwoPreviousState=ButtonTwoState; 135 } 136 137 waterPump(); //calling for water pump function which launch spray cycle if needed</p><p> waterTankAlarm(); //calling function to warn user about water tank level</p><p> //small condition for screen refreshing and measurement of DHT sensor 138 if (currentMillis-debutMillis>timePeriod){ //end of a period 139 debutMillis=currentMillis; //define the begining of a new period 140 toDisplay(); //refresh of the screen regarding "valToDisplay" value 141 humMeasurePeriod++; //DHT sensor values are measured every 20 times the screens is refreshed 142 if (humMeasurePeriod>20){ //go to measure DHT values 143 readTempHum(); 144 humMeasurePeriod=0; 145 }</p><p> } </p><p>}</p>
Downloadable files
Automated gardening -Fritzing
Automated gardening -Fritzing
Comments
Only logged in users can leave comments