How to select different loop programs to control Stepper motor with IR remote

I need suggestions on how to implement the following use cases for controlling a Stepper motor with IR remote.
Run the stepper motor continuously back and forth until stopped by the remote.
Run the Stepper motor continuously CW until stopped by the remote.
Run the Stepper motor continuously CCW until stopped by the remote.

I am using the Accelstepper library so I can utilize the acceleration feature. I have placed the [blocking] commands in the loop() and then want to change the distance and speed with the remote. This part of the code works as desired. The motor runs back and forth in a continuous loop until the ‘OK’ button stops the program by disabling the motor pins. I can change the distance and speed using ‘if’ statements with the remote.
My problem is how to implement the additional use case where I can enter into a different loop that either runs the motor CW or CCW until stopped by user. The attached code shows the commands I want to implement with the Up/Down buttons and I realize that this only advances the motor 1 step and then goes back to running the back and forth loop.
I have tried if/else statements and used the remote to set a variable to select which code would loop but was not successful.

// Livewire Bait Ripper
// Copyright (C) 2022 Mike Donovan

// v 1.1 2022/01/05 //arduino IDE v1.9.0-beta
#include <AccelStepper.h> //version 1.61 
#include<IRremote.h> //version 3.2.0
// Define a stepper motor and the pins it will use 
AccelStepper stepper(1, 6, 9);
// Define IR
int IRPIN = 21; // IRreceiver Pin 21
IRrecv irrecv(IRPIN); //Reads the signals from receiver 
decode_results results; //saves decoded results
// Define variables 
int p3 = 7200;
int s1 = 2800;
int s2 = 2400;
void(* resetFunc) (void) = 0;
void setup() {
stepper.setPinsInverted(false, false, true);
stepper.setMaxSpeed(2800); stepper.setAcceleration(6400); 
irrecv.enableIRIn(); //Starts receiving Serial.begin(9600);
void loop() {

stepper.runToNewPosition(0); stepper.setMaxSpeed(s1); stepper.runToNewPosition(2400); stepper.runToNewPosition(4800); stepper.runToNewPosition(p3); stepper.setMaxSpeed(2400.0);

if (irrecv.decode(&results)) {

if (results.value == 0xFF629D) //Power button stepper.enableOutputs();

if (results.value == 0xFF22DD) //A button
p3 = 7200;
if (results.value == 0xFF02FD)  //B button
p3 = 9600;
if (results.value == 0xFFC23D)  //C button
p3 = 12000;
if (results.value == 0xFF30CF)  //Left button
s1 = 2000;
if (results.value == 0xFF7A85)  //Right button
s1 = 4000;
if (results.value == 0xFF9867) //Up button

if (results.value == 0xFF38C7) //Down button

if (results.value == 0xFF18E7) // OK button
resetFunc(); }

irrecv.resume(); // Receive the next IR value Serial.println(results.value, HEX);
} }

The attached document has the details and code.

Use_Case_IRremote.pdf (72.2 KB)

Post code here. I, among others, will not download code. It is a PITA. Read the forum guidelines to see how to properly post code and information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Take some time and study how to design FSM.
Have a nice day and enjoy coding in C++.

State machine is the way to go:

State Machine

Thanks! I will look into it.

Thx for the link

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.