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