Skip to content

Instantly share code, notes, and snippets.

@mushonnip
Created July 27, 2020 19:15
Show Gist options
  • Save mushonnip/72364ef1c0a51bfa725ad4fb37f5880f to your computer and use it in GitHub Desktop.
Save mushonnip/72364ef1c0a51bfa725ad4fb37f5880f to your computer and use it in GitHub Desktop.
#include <Ultrasonic.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MLX90614.h>
Ultrasonic front_ultrasonic(10, 9);
Ultrasonic back_ultrasonic(12, 11);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
//limit switch for door "A"
#define A_SWITCH_CLOSE 51
#define A_SWITCH_OPEN 53
//limit switch for door "B"
#define B_SWITCH_CLOSE 47
#define B_SWITCH_OPEN 49
//motor for door "A"
#define A_LPWM 2
#define A_RPWM 3
//motor for door "B"
#define B_LPWM 6
#define B_RPWM 7
#define MOTOR_SPEED 150
int incomingByte = 0;
bool door_open = false;
bool door_a_opened = false;
bool door_b_opened = false;
bool thermo_passed = false;
byte check[8] = {
0x00,
0x01,
0x03,
0x16,
0x1C,
0x08,
0x00,
0x00
};
byte degree[8] = {
0x07,
0x05,
0x07,
0x00,
0x00,
0x00,
0x00,
0x00
};
void setup()
{
Serial.begin(9600);
lcd.begin();
mlx.begin();
pinMode(A_SWITCH_CLOSE, INPUT_PULLUP);
pinMode(A_SWITCH_OPEN, INPUT_PULLUP);
pinMode(B_SWITCH_CLOSE, INPUT_PULLUP);
pinMode(B_SWITCH_OPEN, INPUT_PULLUP);
pinMode(A_LPWM, OUTPUT);
pinMode(A_RPWM, OUTPUT);
pinMode(B_LPWM, OUTPUT);
pinMode(B_RPWM, OUTPUT);
lcd.createChar(0, check);
lcd.createChar(1, degree);
}
void loop()
{
if (!thermo_passed) {
lcd.setCursor(0, 0);
if (mlx.readObjectTempC() > 37) {
lcd.print("Suhu anda tinggi");
} else {
lcd.print("Dekatkan Kepala ");
}
lcd.setCursor(0, 1);
lcd.print(mlx.readObjectTempC());
lcd.write(223);
lcd.print("C");
lcd.setCursor(11, 1);
lcd.print(front_ultrasonic.read());
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.print("cm");
delay(500);
}
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == 49 && thermo_passed)
{
door_open = true;
!thermo_passed;
lcd.setCursor(0,0);
lcd.print("Sukses ");
lcd.write(0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Silakan masuk ");
lcd.write(126);
lcd.print(" ");
}
}
if (door_open) {
open_door();
}
if (digitalRead(A_SWITCH_OPEN) == HIGH) {
door_a_opened = true;
}
if (digitalRead(A_SWITCH_CLOSE) == HIGH) {
door_a_opened = false;
}
if (digitalRead(B_SWITCH_OPEN) == HIGH) {
door_b_opened = true;
}
if (digitalRead(B_SWITCH_CLOSE) == HIGH) {
door_b_opened = false;
}
if (front_ultrasonic.read() <= 20 && mlx.readObjectTempC() <= 37 && !door_a_opened) {
thermo_passed = true;
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Suhu Aman");
}
Serial.print("Open: ");
Serial.print(digitalRead(A_SWITCH_OPEN));
Serial.print("\tClose: ");
Serial.print(digitalRead(A_SWITCH_CLOSE));
if (door_a_opened) {
Serial.println("\tPintu A terbuka");
} else {
Serial.println("\tPintu tertutup");
}
// Serial.println(front_ultrasonic.read());
}
void open_door() {
analogWrite(A_LPWM, 0);
analogWrite(A_RPWM, MOTOR_SPEED);
if (door_a_opened) {
analogWrite(A_LPWM, 0);
analogWrite(A_RPWM, 0);
}
analogWrite(B_LPWM, 0);
analogWrite(B_RPWM, MOTOR_SPEED);
if (door_b_opened) {
analogWrite(B_LPWM, 0);
analogWrite(B_RPWM, 0);
}
}
void close_door() {
analogWrite(A_LPWM, MOTOR_SPEED);
analogWrite(A_RPWM, 0);
if (!door_a_opened) {
analogWrite(A_LPWM, 0);
analogWrite(A_RPWM, 0);
}
analogWrite(B_LPWM, MOTOR_SPEED);
analogWrite(B_RPWM, 0);
if (!door_b_opened) {
analogWrite(B_LPWM, 0);
analogWrite(B_RPWM, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment