Blockquote
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include “Ultrasonic.h”
#include “max6675.h”
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>
// WiFi network info.
char ssid = “xxi”;
char wifiPassword = “xxi”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = xxx
char password = xxx
char clientID = xxx
// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial1
ESP8266 wifi(&EspSerial);
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
Ultrasonic ultrasonic(11,12);
Ultrasonic ultrasonic2 (40,42);
Servo myservo1;
Servo myservo2;
int sensorpin1 = 5; //ca
int sensorpin2 = 13; //ind
#define in1 2
#define in2 3
#define r1 46
#define r2 44
#define L1 38
#define L2 34
#define L3 36
bool roll = false;
bool temp = false;
float suhu_lectrobin;
int terima = 0;
int tolak = 0;
int GIu_Ultrasonic_Dist_CM=0;
int jarak_kompresi = 0;
int var_persen_kompresi;
float volumeharian;
int tinggi;
int var_tinggi;
int persen_kapasitas;
int persen_kompresi;
int pos1 = 92;
int pos2 = 92;
int buzzer = 4;
int soPin = 52;// SO=Serial Out
int csPin = 50;// CS = chip select CS pin
int sckPin = 48;// SCK = Serial Clock pin
MAX6675 lectrobin(sckPin, csPin, soPin);// create instance object of MAX6675
//Virtual Pin Channel Cayenne
#define proximityca 1
#define proximityind 2
#define kapasitas_sampah 7
#define heater 4
#define volume 5
#define tinggi_sampah 6
#define silinder_turun 14
#define silinder_naik 8
#define silinderoff 12
#define relay 10
//MultiTask System
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
const long eventTime_piority1 = 1000; //in ms
const long eventTime_piority2 = 2000; //in ms
const long eventTime_piority3 = 3000; //in ms
void setup()
{
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(L1,OUTPUT);
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(sensorpin1, INPUT);
pinMode(sensorpin2, INPUT);
pinMode (buzzer, OUTPUT);
myservo1.attach(8);
myservo2.attach(7);
lcd.begin (16,2);
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop()
{
Cayenne.loop();
unsigned long currentTime = millis();
int sensorca = digitalRead(sensorpin1);
int sensorind = digitalRead(sensorpin2);
if (sensorca == 1 && sensorind == 1 && GIu_Ultrasonic_Dist_CM >= 14) {
lcd_terima();
terima = terima + 1 ;
servo_buka();
servo_buka2();
delay (3000);
servo_tutup();
servo_tutup2();
}
else if (sensorca == 1 && sensorind == 0) {
lcd_tolak();
tolak = tolak + 1;
delay (1000);
}
else if (sensorca == 0 && sensorind == 1 && GIu_Ultrasonic_Dist_CM > 14 && roll == false && temp == false) {
lcd_awal();
delay (100);
}
if ( currentTime - previousTime_3 >= eventTime_piority3){
// Membaca suhu heater
suhu_lectrobin = lectrobin.readCelsius();
Serial.print( "Suhu Heater = ");
Serial.print(suhu_lectrobin));
Serial.print( "C || ");
//Membaca sensor ultrasonik kompresi
jarak_kompresi = ultrasonic2.Ranging(CM);
persen_kompresi = (jarak_kompresi - 3) 4.34782609;
if ( persen_kompresi < 0 ){
var_persen_kompresi = 0;
}
else if ( persen_kompresi > 100){
var_persen_kompresi = 100;
}
else{
var_persen_kompresi = persen_kompresi;
}
Serial.print("Jarak Kompresi = ");
Serial.print(jarak_kompresi);
Serial.print(“cm || “);
Serial.println (” **”);
previousTime_3 = currentTime;
}
if ( currentTime - previousTime_2 >= eventTime_piority2){
//Membaca sensor ultrasonik kapasitas
GIu_Ultrasonic_Dist_CM = ultrasonic.Ranging(CM);
tinggi = 33 - GIu_Ultrasonic_Dist_CM ;
if (tinggi < 0){
var_tinggi = 0;
}
else{
var_tinggi = tinggi;
}
persen_kapasitas = var_tinggi 5;
volumeharian = ((var_tinggi * 15 15*3.14)/1000);
Serial.print("Tinggi = ");
Serial.print(var_tinggi);
Serial.print("cm || " );
Serial.print("Kapasitas Penampungan = “);
Serial.print(persen_kapasitas);
Serial.print(”% || " );
previousTime_2 = currentTime;
}
if ( currentTime - previousTime_1 >= eventTime_piority1){
Cayenne.virtualWrite(proximityca, terima);
Cayenne.virtualWrite(proximityind, tolak);
Cayenne.virtualWrite(heater, suhu_lectrobin, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(volume, volumeharian);
Cayenne.virtualWrite(tinggi_sampah, var_tinggi, TYPE_PROXIMITY, UNIT_CENTIMETER);
Cayenne.virtualWrite(kapasitas_sampah, persen_kapasitas, TYPE_PROXIMITY, UNIT_PERCENT);
Cayenne.virtualWrite(silinderoff, var_persen_kompresi, TYPE_PROXIMITY, UNIT_PERCENT);
previousTime_1 = currentTime;
}
//Alert ketika kapasitas penuh
if ( GIu_Ultrasonic_Dist_CM <= 14 && roll==false && temp== false ){ //&& counter_buzzer < 5
lcd_penuh();
bunyi_buzzer();
}
//alert kompresi
kompresi();
//konfigurasi relay 2
if (temp == true || roll == true){
digitalWrite(r2, HIGH);
}
else if (temp == false && roll == false){
digitalWrite(r2, LOW);
}
}
//fungsi custom LCD
void lcd_awal (){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (3,1);
lcd.print(“Lectro BIN”);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
}
void lcd_terima(){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (3,1);
lcd.print(“Terimakasih”);
}
void lcd_tolak(){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (1,1);
lcd.print(“Sampah Ditolak”);
}
void lcd_penuh(){
lcd.clear();
lcd.setCursor (3,0);
lcd.print(“Lectro BIN”);
lcd.setCursor (1,1);
lcd.print(“Kapasitas penuh”);
digitalWrite(L2, HIGH);
digitalWrite(L1, LOW);
digitalWrite(L3, LOW);
}
void lcd_kompresi (){
lcd.clear();
lcd.setCursor (3,0);
lcd.print(“Lectro BIN”);
lcd.setCursor (1,1);
lcd.print(“Proses Kompresi”);
digitalWrite(L3, HIGH);
digitalWrite(L2, LOW);
digitalWrite(L1, LOW);
}
//fugnsi aktivasi buzzer
void bunyi_buzzer (){
for ( int i=0; i < 6; i++){
tone(buzzer, 1000); // Send 1KHz sound signal…
delay(500); // …for 1 sec
noTone(buzzer); // Stop sound…
delay(500); // …for 1sec
}
}
//fungsi aktivasi batas kompresi
void kompresi(){
if ( jarak_kompresi > 25 && roll == false ){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
//fungsi aktivasi motor servo
void servo_buka (){
for (pos1 = 90; pos1 >= 1; pos1 -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos1);
//myservo2.write(pos1);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}
void servo_tutup (){
for (pos1 = 0; pos1 <= 92; pos1 += 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos1);
//myservo2.write(pos1);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}
void servo_buka2 (){
for (pos2 = 90; pos2 <= 179; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
// myservo1.write(pos1);
myservo2.write(pos2);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}
void servo_tutup2 (){
for (pos2 = 180; pos2 >= 92; pos2 -= 1) { // goes from 180 degrees to 0 degrees
//myservo1.write(pos1);
myservo2.write(pos2);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}
CAYENNE_IN(silinder_turun)
{
// get value sent from dashboard
int currentValue_turun = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue_turun == 1) {
lcd_kompresi ();
roll=true;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else {
roll = false;
}
}
CAYENNE_IN(silinder_naik)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 1) {
lcd_kompresi ();
temp=true;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
else {
temp=false;
}
}
CAYENNE_IN(relay)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue == 1) {
digitalWrite(r1, HIGH);
}
else {
digitalWrite(r1, LOW);
}
}