// UPDATED AS OF "Friday 10/02/2023" @ "16:06"
#include <LiquidCrystal.h>
#include <Servo.h>
#include <EEPROM.h>
#include <Stepper.h>
#include <dht.h> //CHANGE ONCE ASSEM
#include <RTClib.h> //CHANGE ONCE ASSEM
const int StepsPerRevolution = 3600; //CHANGE ONCE ASSEM
Stepper myStepper(StepsPerRevolution, 29, 27, 25, 23); //CHANGE ONCE ASSEM
RTC_DS1307 rtc; //CHANGE ONCE ASSEM
dht DHT; //CHANGE ONCE ASSEM
LiquidCrystal lcd (12,11,5,4,3,2);
#define trigPin 14
#define echoPin 15
#define BUTTON_PIN 33 //CHANGE ONCE ASSEM
#define DHT22_PIN 13 //CHANGE ONCE ASSEM
String state, initial_s;
long initial;
float time, dist;
bool flag = false; //True=open
bool serial_flag = false;
int flagger = 0;
float box_size = 400; //cm
int percentage;
int act_perc;
int readData;
float hum,tum;
int hum_tak, tum_tak;
int clock = 0, c_clock, a_clock;
long timer = 0;
Servo servo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo.attach(16);
lcd.begin(16,2);
lcd.print("Welcome");
delay(1000);
lcd.clear();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); //CHANGE ONCE ASSEM
lcd.setCursor(0,0);
lcd.print("Capacity: ");
lcd.setCursor(13,0);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("T: ");
lcd.setCursor(8,1);
lcd.print("H: ");
lcd.setCursor(5,1);
lcd.print("%");
lcd.setCursor(13,1);
lcd.print("%");
myStepper.setSpeed(9); //CHANGE ONCE ASSEM
rtc.begin();
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
a_clock = now.second();
Serial.println(a_clock);
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
time = pulseIn(echoPin, HIGH);
dist = ((time/2)*(340))/10000;
percentage = (dist/box_size)*100;
act_perc = percentage;
lcd.setCursor(10,0);
lcd.print(act_perc);
if (digitalRead(BUTTON_PIN)==0){
if (EEPROM.read(1)==255){
Serial.println("counterclockwise");
myStepper.step(-StepsPerRevolution);
flag = true;
EEPROM.write(1,0);
delay(500);
}
else if (EEPROM.read(1)==0){
Serial.println("clockwise");
myStepper.step(StepsPerRevolution);
EEPROM.write(1,255);
flag = false;
delay(500);
}
}
readData = DHT.read22(DHT22_PIN); // Reads the data from the sensor
tum = floor(DHT.temperature); // Gets the values of the temperature
hum = floor(DHT.humidity); // Gets the values of the humidity
tum_tak = int(tum);
hum_tak = int(hum);
lcd.setCursor(3,1);
lcd.print(tum_tak);
lcd.setCursor(11,1);
lcd.print(hum_tak);
if ((hum < 40) && (timer%200 == 0)){
servo.write(180);
delay(200);
servo.write(0);
delay(200);
servo.write(180);
delay(200);
servo.write(0);
delay(200);
servo.write(180);
delay(200);
servo.write(0);
delay(200);
}
timer = timer+1;
if ((hum > 60) && (flag == false)){
myStepper.step(-StepsPerRevolution);
flag = true;
}
if ((hum < 60) && (flag == true)){
myStepper.step(StepsPerRevolution);
flag = false;
}
if (timer>=10000){
timer = 0;
}
}