#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial HC06(10,11); // tx = 10, rx = 11
int trig = 13;
int echo = 12;
int times = 0;
int enable = 1;
int SPEAKER_PIN = 3;
int NOTE_G5 = 784;
int ldrGlass = A0;
int enableGlass = 1;
int timesGlass = 0;
int ledGlass = 8;
int ldrPaper = A1;
int enablePaper = 1;
int timesPaper = 0;
int ledPaper = 7;
int ldrPlastic = A2;
int enablePlastic = 1;
int timesPlastic = 0;
int ledPlastic = 4;
int ldrIR = A3;
int enableIR = 1;
int timesIR = 0;
int ledIR = 2;
Servo myservo;
Servo myservo2;
void setup() {
lcd.init();
lcd.backlight();
HC06.begin(115200);
Serial.begin(9600);
Serial.println("PROJECT_INITIALIZED");
Serial.println("___________________");
Serial.println("");
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(ldrGlass, INPUT);
pinMode(ldrPaper, INPUT);
pinMode(ldrPlastic, INPUT);
pinMode(ldrIR, INPUT);
pinMode(ledGlass, OUTPUT);
pinMode(ledPaper, OUTPUT);
pinMode(ledPlastic, OUTPUT);
pinMode(ledIR, OUTPUT);
myservo.attach(11);
myservo2.attach(10);
}
void loop() {
// classification code
if (Serial.available() > 0) {
String result = Serial.readString();
result.trim();
result.toLowerCase();
Serial.println(result);
if (result == "glass"){
myservo2.write(0);
lcdPrint("Glass");
} else if (result == "paper") {
myservo2.write(90);
lcdPrint("Paper");
} else if (result == "plastic"){
myservo2.write(180);
lcdPrint("Plastic");
} else if (result == "ir"){
myservo2.write("270");
lcdPrint("IR");
} else {
lcdPrint("Invalid");
}
}
// if (HC06.available() > 0) {
// char receive = HC06.read();
// if(receive == '1') {
// lcdprint("paper");
// } else if (receive == '2') {
// lcdprint("plastic");
// } else {
// lcdprint("glass");
// }
// }
// ultrasonic code
digitalWrite(trig, HIGH);
delayMicroseconds(2);
digitalWrite(trig, LOW);
int distance = pulseIn(echo, HIGH);
int cm = distance * 0.034 / 2;
// Serial.print("Cm: ");
// Serial.println(cm);
// -----------------------------------
// Servo with ultrasonic code
if (cm <= 20) {
times++;
if (enable != 0) {
delay(1000);}
} else {
times = 0;
enable = 1;
} if (times >= 2 && cm <= 20) {
myservo.write(90);
enable = 0;
} else {
myservo.write(0);
}
// -----------------------------------
// LDR Part
int luxGlass = analogRead(ldrGlass);
if (luxGlass >= 519) {
timesGlass++;
if (enableGlass != 0) {
delay(1000);
}
} else {
timesGlass = 0;
enableGlass = 1;
} if (timesGlass >= 2 && luxGlass >= 519){
tone(SPEAKER_PIN, NOTE_G5);
digitalWrite(ledGlass, HIGH);
enableGlass = 0;
} else {
noTone(SPEAKER_PIN);
digitalWrite(ledGlass, LOW);
}
//___________________________________
int luxPaper = analogRead(ldrPaper);
if (luxPaper >= 519) {
timesPaper++;
if (enablePaper != 0) {
delay(1000);
}
} else {
timesPaper = 0;
enablePaper = 1;
} if (timesPaper >= 2 && luxPaper >= 519){
tone(SPEAKER_PIN, NOTE_G5);
digitalWrite(ledPaper, HIGH);
enablePaper = 0;
} else {
noTone(SPEAKER_PIN);
digitalWrite(ledPaper, LOW);
}
//___________________________________
int luxPlastic = analogRead(ldrPlastic);
if (luxPlastic >= 519) {
timesPlastic++;
if (enablePlastic != 0) {
delay(1000);
}
} else {
timesPlastic = 0;
enablePlastic = 1;
} if (timesPlastic >= 2 && luxPlastic >= 519){
tone(SPEAKER_PIN, NOTE_G5);
digitalWrite(ledPlastic, HIGH);
enablePlastic = 0;
} else {
noTone(SPEAKER_PIN);
digitalWrite(ledPlastic, LOW);
}
//___________________________________
int luxIR = analogRead(ldrIR);
if (luxIR >= 519) {
timesIR++;
if (enableIR != 0) {
delay(1000);
}
} else {
timesIR = 0;
enableIR = 1;
} if (timesIR >= 2 && luxIR >= 519){
tone(SPEAKER_PIN, 255);
digitalWrite(ledIR, HIGH);
enableIR = 0;
} else {
noTone(SPEAKER_PIN);
digitalWrite(ledIR, LOW);
}
}
void lcdPrint(String material) {
lcd.home();
lcd.clear();
lcd.print(material);
Serial.print("Selected: ");
Serial.println(material);
Serial.println("");
}