#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int receiver_pin = 2;
int btn_value = 0;
IRrecv receiver(receiver_pin);
const int DIR_PIN = 3;
const int STEP_PIN = 4;
const int ENABLE_PIN = 5;
const int speeds[9] = {1000, 900, 800, 700, 600, 500, 400, 300, 200};
int currentSpeed = 1000;
bool motorDirection = true;
bool motorEnabled = false;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
receiver.enableIRIn();
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, HIGH);
lcd.begin(16, 2);
}
void loop() {
if (receiver.decode()) {
translateIR();
receiver.resume();
}
if (motorEnabled) {
stepMotor();
}
}
void translateIR() {
btn_value = receiver.decodedIRData.command;
Serial.print("Button value: ");
Serial.println(btn_value);
if (btn_value == 226) {
Serial.println("Menu");
Serial.println("Press Power (Shortcut: O) to turn on/off the motor");
Serial.println("Press Menu (Shortcut: M) for help");
Serial.println("Press Number keys (Shortcut: Num Keys) for speed");
Serial.println("Press Back (Shortcut: B) to change direction");
Serial.println("That's it!! Enjoy!!!");
}
if (btn_value == 162) {
motorEnabled = !motorEnabled;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(motorEnabled ? "Motor on" : "Motor off");
delay(1000);
lcd.clear();
digitalWrite(ENABLE_PIN, motorEnabled ? LOW : HIGH);
}
if (btn_value == 194) {
motorDirection = !motorDirection;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Toggling motor direction.");
delay(1000); // Add delay before clearing the message
lcd.clear();
digitalWrite(DIR_PIN, motorDirection ? HIGH : LOW);
}
if (btn_value == 48) {
currentSpeed = 1000;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 1");
}
if (btn_value == 24) {
currentSpeed = 900;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 2");
}
if (btn_value == 122) {
currentSpeed = 800;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 3");
}
if (btn_value == 16) {
currentSpeed = 700;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 4");
}
if (btn_value == 56) {
currentSpeed = 600;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 5");
}
if (btn_value == 90) {
currentSpeed = 500;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 6");
}
if (btn_value == 66) {
currentSpeed = 400;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 7");
}
if (btn_value == 74) {
currentSpeed = 300;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 8");
}
if (btn_value == 82) {
currentSpeed = 200;
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Speed set to: 9");
}
}
void stepMotor() {
if (digitalRead(ENABLE_PIN) == LOW) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(currentSpeed);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(currentSpeed);
}
}