/*---Libraries---*/
#include "IRremote.h" //Library for IR sensor
#include <Wire.h> //Library for I2C
#include <LiquidCrystal_I2C.h> //Library for LCD
#include <SR04.h> //Library for distance sensor
/*---PINs declaration---*/
int PIN_receiver = 10; //PIN for IR signal
int PIN_moisture = A0; //PIN for soild moisture sensor
//PINs for distance sensor
int TRIG_PIN = 12;
int ECHO_PIN = 11;
/*-----Object declaration-----*/
IRrecv receiver(PIN_receiver); //Object for 'IRrecv'
decode_results result; //Object for 'decode_results'
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); //Object for LCD
// You may have a different I2C adress (0x27 is the one that worked for me) for the LCD
// If this code doesn't work try to change the I2C adress with this one 0x3F
// Also you can run a code to see the adress of your I2C
// https://create.arduino.cc/projecthub/abdularbi17/how-to-scan-i2c-address-in-arduino-eaadda
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); //Object for distance sensor
void setup()
{
receiver.enableIRIn(); //Open the IR receiver
lcd.init(); //Open LCD
lcd.backlight(); //'Turn on the light'
lcd.setCursor(0, 0); //Setting the cursor on the first row, first column
lcd.print("Moisture->0"); //Showing the option on LCD, printing 'Moisture->0'
lcd.setCursor(0,1); //Setting the cursor on the second row, first column
lcd.print("Distance->1"); //Showing the option on LCD, printing 'Distance->0'
//If you press '0' (zero) you will get the value of the soil moisture
//If you press '1' (one) you will get the value of the distance till the nearest object
}
//Function that returns the number of the pressed button
int IRCommand()
{
switch(result.value)
{
// If the adress is '0xFF6897' the button is '0',
case 0xFF6897: return 0; // If the adress is '0xFF6897' the button is '0'
case 0xFF30CF: return 1; // If the adress is '0xFF30CF' the button is '1'
}
}
void loop()
{
if(receiver.decode(&result)) //Checking if a button was pressed
{
if(IRCommand()==0) //if the pressed button is '0'
{
lcd.clear(); //clear the text on the LCD
lcd.setCursor(0,0);
lcd.print("Moisture[%]:");
//Reading the soil moisture value from the sensor
//(It can be between 415 (wet) and 995(dry). I decided to make a map (dictionary) to show the value as a percentage
//The value returned by your sensor may be different but you can calibrate it by showing the value (returned from the sensor)
//on the LCD while you keep your sensor in air (soil moisture->0%) or in water (soil moisture->100%)
int percentage = map(analogRead(PIN_moisture),995,415,0,100); //995 -> 0% and 415 -> 100%
//Checking if the value is over 100 or under 0 and corecting it (can happen sometimes, the sensor is crappy)
if(percentage<0)
{
percentage = 0;
}
if(percentage>100)
{
percentage = 100;
}
//Printing the percentage
lcd.setCursor(0, 1);
lcd.print(percentage);
delay(500);//Take a break ~500ms
}
if(IRCommand()==1) //if the pressed button is '1'
{
lcd.clear(); //clear the text on the LCD
lcd.setCursor(0, 0);
lcd.print("Distance[cm]:");
lcd.setCursor(0, 1);
lcd.print(sr04.Distance()); //Reading and pinting the distance value from the sensor
delay(500); //Take a break ~500ms
}
receiver.resume();//Reset the current IR command and waiting for a new one
}
}
Comments