/*
Forum: https://forum.arduino.cc/t/add-irremote-to-library/1426618/5
Wokwi: https://wokwi.com/projects/453957197937624065
Modified for IRemote.hpp V4.5
ec2021
2026/01/22
*/
#include "Stepper.h"
#include "IRremote.hpp"
enum class CTRL {IDLE, CW, CCW, STOP, WAIT};
CTRL stepperCtrl = CTRL::IDLE;
/*----- Constants Variables, Pins -----*/
constexpr uint8_t leda {7};
constexpr uint8_t ledb {6};
constexpr uint16_t STEPS_PER_REVOLUTION {200};
constexpr uint8_t IR_RECEIVE_PIN {12}; // Signal Pin of IR receiver to Arduino Digital Pin 12
constexpr unsigned long WAIT_TIME {500};
unsigned long lastStarttime;
int stepsToGo = STEPS_PER_REVOLUTION;
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS_PER_REVOLUTION, 8, 10, 9, 11);
void setup()
{
IrReceiver.begin(IR_RECEIVE_PIN);; // Start the receiver
small_stepper.setSpeed(200);
pinMode(leda, OUTPUT);
pinMode(ledb, OUTPUT);
green();
}
void loop()
{
if (IrReceiver.decode()) {
handleIR();
IrReceiver.resume(); // Receive the next value
}
handleStepper();
}
void handleIR()
{
switch (IrReceiver.decodedIRData.command) {
case 2: // PLUS
try2changeStepperCtrl(CTRL::CW);
break;
case 152: // MINUS
try2changeStepperCtrl(CTRL::CCW);
break;
}
}
void try2changeStepperCtrl(CTRL newCtrl)
{
if (stepperCtrl != CTRL::WAIT)
{
stepperCtrl = newCtrl;
}
}
void handleStepper()
{
switch (stepperCtrl)
{
case CTRL::WAIT:
if (millis() - lastStarttime >= WAIT_TIME)
{
// In Wokwi the stepper moves one step
// if we go to CTRL::STOP therefore CTRL:IDLE
stepperCtrl = CTRL::IDLE;
green();
}
break;
case CTRL::IDLE:
// Do nothing
break;
case CTRL::CW:
rotate(-stepsToGo);
break;
case CTRL::CCW:
rotate(stepsToGo);
break;
case CTRL::STOP:
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
stepperCtrl = CTRL::IDLE;
break;
}
}
void rotate(int steps)
{
red();
lastStarttime = millis();
small_stepper.step(steps);
stepperCtrl = CTRL::WAIT;
}
void green()
{
digitalWrite(leda, HIGH);
digitalWrite(ledb, LOW);
}
void red()
{
digitalWrite(leda, LOW);
digitalWrite(ledb, HIGH);
}