#include <LiquidCrystal.h> // adds lcd library
#include <LedControl.h> // adds lc library
#include <Servo.h> // add servo library
#include <dht.h> // adds dht library
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); //sets lcd pins (can be altered for ease of construction)
LedControl lc(7,5,6,1); //sets dot matrix pins (can be altered for ease of construction)
Servo myServo; //sets up servo sintax
dht DHT; //sets up dht sintax
uint8_t trigPin = A1; //sets trig pin (needs to be analog)
uint8_t echoPin = A2; //sets echo pin (needs to be analog)
uint8_t servoPin = A0; //sets servo pin (needs to be analog)
int distance; //creats distance variable
int threshold = 12; // sets the threshold distance to (12cm)
int duration; //sets the duration for the calculation
int msensor = (4); // stets the motiondetector pin (can ba canged for ease of construction)
int val; // sets a variable for the motion
int DHT22 = (3); // sets the dht pin (can be changed for ease of construction)
float hum; // creates humidity variable
int chk; //creats a variable for reading dht sensor
byte smile[8]= {0x3C,0x42,0xA5,0x81,0xA5,0x99,0x42,0x3C}; // happy face
byte sad[8]= {0x3C,0x42,0xA5,0x81,0x99,0xA5,0x42,0x3C}; // frowny face
void setup() { // setup section of code
lcdstart(); // calls the lcd startup loop
myServo.attach(servoPin); // sets the servo laibrary to use the servo pin
myServo.write(0); // sets the initial position of the servo to 0deg or "closed"
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
delay(1000); // adds a delay
lc.shutdown(0,false); // keeps the dot matrix on
lc.setIntensity(0,0); // ititializes the intensity
lcclear(); // calls the "clear dot matrix" function
distancedetect(); // calls the distance detection function
faces(); // calls the faces function
}
void loop() {
motiondetect(); // calls the motion detect function
faces(); // calls the faces function
humiditydetect(); // calls the function to detect humidity
}
void lcclear() { // function to clear the dot matrix
lc.clearDisplay(0); // clears the dot matrix
}
void lcdstart() { // function to st
lcd.begin(16,2); // turns on the 16 by 2 screen
lcd.print("Hello World"); // displays in an intro message
delay(500); // stes a delay
lcd.clear(); // clears the lcd
}
void motiondetect() { // a function to detect motion
val = digitalRead(msensor); // reads the sensors output for motion
if (val == HIGH) { // if (motion == true)
myServo.write(180); // opens the bin
delay(500);// adds a delay to prevent the code from running while lid is in motion
}
if (val == LOW){ // if (motion == false)
myServo.write(0); // closes the lid
delay(500); // adds a delay to close the lid (if the distance is inacruate extend this delay)
distancedetect(); // calls the distance detection function
delay(700); // adds a delay
}
}
void faces() { // displays the correct face on the dot matrix depending on the depth of the compost in the bin
if (distance>12) { //print happy face
happy();} // calls the function to print a happy face
if (distance<12) { //print frowny face
frown();} // calls the function to print frowny faces
}
void distancedetect() { // detects the depth of the compost in the bin
digitalWrite(trigPin, LOW); //Ultrasonic trigger pin set up
delayMicroseconds(2); // adds delay
digitalWrite(trigPin, HIGH); // sends the ultrasonic signal
delayMicroseconds(10); // adds a delay
digitalWrite(trigPin, LOW); // stops sending the signal
duration = pulseIn(echoPin, HIGH); //Echo pin set up
distance = (duration * 0.0343) / 2; // conversion of time to distance
}
void humiditydetect() { // function to detect the humidity
chk = DHT.read22(DHT22); // reads the sensors output
hum = DHT.humidity; // determines the humidity using the library
humiditytest(); // calls the humidity test function
}
void humiditytest() { // determines if the bag is okay baced off the humidity
if (hum<=75) { // for humidity les than 75 but grater than 65
lcd.clear(); // clears the lcd
lcd.setCursor(0,0); // sets teh cursor to the top spot
lcd.print("bag is moot :|"); // prints the "bag is questionable" message
lcd.setCursor(0,1); // sets the cursor to the lower position
lcd.print("humidity:" + String(hum) + "%"); // prints the humidity
if (hum<=65) { // for humidity less that 65 (calgary average is 60)
lcd.clear(); // clears the lcd
lcd.setCursor(0,0); // sets the cursor to the top spot
lcd.print("bag is fine :)"); // prints the message that says the bag is good
lcd.setCursor(0,1); // sets the cursto to the lowwer spot
lcd.print("humidity:" + String(hum) + "%"); // prints the humidity
}
}
if (hum>75) { // for humidity grater than 75
lcd.clear(); // clears the lcd
lcd.setCursor(0,0); // sets the cursor to the top spot
lcd.print("check bag! :("); // prints the message to check the bag
lcd.setCursor(0,1); // sets the cursor to the lowwer position
lcd.print("humidity:" + String(hum) + "%"); // prints the humidity
}
}
void happy() {//from https://earthbondhon.com/smiley-faces-with-8x8-matrix-display/
lcclear(); // clears the dot matrix
printByte(smile); // displays the byte for the happy face
}
void frown() {//from https://earthbondhon.com/smiley-faces-with-8x8-matrix-display/
lcclear(); // clears the dot matrix
printByte(sad); // displays the byte for the frowny face
}
void printByte(byte character []) {//from https://earthbondhon.com/smiley-faces-with-8x8-matrix-display/
int i = 0; // sets i foor the loop
for(i=0;i<8;i++) // sets the loop
{
lc.setRow(0,i,character[i]); // runs to display the input byte
}
}