#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>
#include <DallasTemperature.h>
#include <OneWire.h>
LiquidCrystal_I2C lcd(0X27, 16, 2);
LiquidCrystal_I2C lcd2(0X26, 16, 2);
const int trigpin = 5;
const int echopin = 4;
int pb = 12;
Servo servo1;
Servo servo2;
int state = 0;
int receiver_pin = 9;
bool work = false;
int step = 2;
int dir = 3;
int delay_time = 0;
int speed = 0;
int key = 0;
int red = 6;
int green = 7;
int blinkCounter = 0;
long safetyCounter = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pb, INPUT_PULLUP);
servo1.attach(11);
servo2.attach(10);
servo1.write(0);
servo2.write(180);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Mixer Panel");
lcd2.init();
lcd2.backlight();
IrReceiver.begin(receiver_pin, ENABLE_LED_FEEDBACK);
pinMode(step, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
}
void loop() {
if (work == false) {
safetyCounter++;
if (safetyCounter > 500) {
float dist = getDistance();
if (dist < 20) { // Fingers detected closer than 10cm!
lcd2.setCursor(0, 0);
lcd2.print(" WARNING");
lcd2.setCursor(0, 1);
lcd2.print("Be aware of your fingers!!");
close_cover();
work = true; // Set state to closed
speed = 0; // Ensure motor is stopped for safety
}
else{
lcd2.setCursor(0, 0);
lcd2.print("Safe distance");
lcd2.setCursor(0, 1);
lcd2.print(" ");
}
safetyCounter = 0;
}
}
state = digitalRead(pb);
if (state == LOW) {
work = !work; // Toggle the state
// ACTION: Only move the cover when the button is pressed
if (work == true) {
close_cover();
} else {
open_cover();
speed = 0; // Reset speed when opening
}
while (digitalRead(pb) == LOW); // Wait for release
return;
}
// REPETITIVE TASKS: Only if work is true
if (work == true) {
if (IrReceiver.decode()) {
key = IrReceiver.decodedIRData.command;
IrReceiver.resume();
// if (overheatLockout == false) {
speed = mapKeyToDigit(key);
message(0, 1, String("speed: ") + speed);
}
mixer(speed);
}
else {
//blink(green);
}
}
void close_cover() {
int count = 0;
while (count < 90)
{
count++;
servo1.write(count);
servo2.write(180 - count);
delay(10);
}
message(0, 1, "cover is closed");
}
void message(int column, int row, String message) {
lcd.setCursor(column, row);
lcd.print(" ");
lcd.setCursor(column, row);
lcd.print(message);
}
int mapKeyToDigit(int key) {
if (key == 48) return 1;
if (key == 24) return 2;
if (key == 122) return 3;
if (key == 16) return 4;
if (key == 56) return 5;
if (key == 90) return 6;
if (key == 66) return 7;
if (key == 74) return 8;
if (key == 82) return 9;
if (key == 104) return 0;
return -1;
}
// || overheatLockout == true
void mixer(int speed) {
if (speed <= 0) {
digitalWrite(step, LOW);
return;
}
// Map speed 1-9 to 2000 - 500 microseconds of delay
int us_delay = map(speed, 1, 9, 2000, 500);
digitalWrite(dir, HIGH);
digitalWrite(step, HIGH);
delayMicroseconds(us_delay);
digitalWrite(step, LOW);
delayMicroseconds(us_delay);
blinkCounter++;
if (blinkCounter > 200) { // Adjust this number to change blink speed
if (digitalRead(red) == LOW) {
digitalWrite(red, HIGH);
}
else {
digitalWrite(red, LOW);
}
blinkCounter = 0; // Reset the count
}
}
void open_cover() {
int count = 0;
while (count < 90)
{
count++;
servo1.write(90 - count);
servo2.write(90 + count);
delay(10);
}
message(0, 1, "cover is open");
}
void blink(int led) {
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
float getDistance() {
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
// pulseIn with a 20000 microsecond timeout (approx 3 meters)
long duration = pulseIn(echopin, HIGH);
float distance = duration * 0.034 / 2;
if (duration == 0) return 400; // If no echo, return a safe distance
return distance;
}