#include <IRremote.h>
#include <Stepper.h> // Include the Stepper library
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal_I2C library
#define PIN_RECEIVER 4
#define STEPS_PER_REVOLUTION 200 // Change this value according to your stepper motor
IRrecv receiver(PIN_RECEIVER);
// Define the pins connected to the stepper motor
const int STEPPER_PIN1 = 8;
const int STEPPER_PIN2 = 9;
const int STEPPER_PIN3 = 10;
const int STEPPER_PIN4 = 11;
Stepper stepper(STEPS_PER_REVOLUTION, STEPPER_PIN1, STEPPER_PIN2, STEPPER_PIN3, STEPPER_PIN4);
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if necessary
int stepperPosition = 0; // Variable to store the current position of the stepper motor
void setup() {
Serial.begin(115200);
receiver.enableIRIn();
stepper.setSpeed(60); // Set the speed (RPM) of the stepper motor
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Setup completed."); // Display setup completion message
lcd.setCursor(0, 1);
lcd.print("Ready for IR & LCD.");
Serial.println("Setup completed. Ready to receive IR commands and display on LCD.");
}
void loop() {
if (receiver.decode()) {
long int deCode = receiver.decodedIRData.command;
// Print the received IR code
Serial.print("Received IR code: ");
Serial.println(deCode);
// Button actions for controlling stepper motor
switch (deCode) {
case 2: // Button for moving stepper motor clockwise (+1)
if (stepperPosition < 200) {
stepper.step(1);
stepperPosition++; // Increment stepper position
updateLCD();
Serial.print("Stepper position: ");
Serial.println(stepperPosition);
}
break;
case 152: // Button for moving stepper motor counterclockwise (-1)
if (stepperPosition > 0) {
stepper.step(-1);
stepperPosition--; // Decrement stepper position
updateLCD();
Serial.print("Stepper position: ");
Serial.println(stepperPosition);
}
break;
}
receiver.resume();
}
}
// Function to update the LCD display with stepper motor position information
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stepper position: ");
lcd.print(stepperPosition);
lcd.print(" steps");
}