IR Remote control my stepper motor

Hi there all,

I am using an

  • Uno,
  • Remote control Ir reciever
  • and a 28byj-48 and driver

I can get the motor to turn when I press a button and at the right speed. However I can't select any other buttons\speeds after I have pressed the first button.

/*
   Created by DIYables

   This example code is in the public domain

   Product page: https://diyables.io/products/infrared-ir-remote-control-kit-with-21-key-controller-and-receiver
*/

#include <DIYables_IRcontroller.h> // DIYables_IRcontroller library
#include <Stepper.h>
#define IR_RECEIVER_PIN 2 // The Arduino pin connected to IR controller
#define STEPS 32 
DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
Stepper stepper(STEPS, 8, 10, 9, 11);
void setup() {
  Serial.begin(115200);
  irController.begin();
}

void loop() {
  Key21 command = irController.getKey();
  if (command != Key21::NONE) {
    switch (command) {
      case Key21::KEY_CH_MINUS:
        Serial.println("CH-");
          stepper.setSpeed(0); 
          stepper.step(2000);  
          delay(200);
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_CH:
        Serial.println("CH");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_CH_PLUS:
        Serial.println("CH+");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_PREV:
        Serial.println("<<");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_NEXT:
        Serial.println(">>");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_PLAY_PAUSE:
        Serial.println(">||");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_VOL_MINUS:
        Serial.println("–");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_VOL_PLUS:
        Serial.println("+");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_EQ:
        Serial.println("EQ");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_100_PLUS:
        Serial.println("100+");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_200_PLUS:
        Serial.println("200+");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_0:
        Serial.println("0");
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_1:
        Serial.println("1");
          stepper.setSpeed(50); 
          stepper.step(2000);  
          delay(200);
        // TODO: YOUR CONTROL
        break;

      case Key21::KEY_2:
        Serial.println("2");
          stepper.setSpeed(60); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_3:
        Serial.println("3");
          stepper.setSpeed(70); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_4:
        Serial.println("4");
          stepper.setSpeed(80); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_5:
        Serial.println("5");
          stepper.setSpeed(90); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_6:
        Serial.println("6");
          stepper.setSpeed(100); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_7:
        Serial.println("7");
          stepper.setSpeed(110); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_8:
        Serial.println("8");
          stepper.setSpeed(120); 
          stepper.step(32608);  
          delay(200);
        break;

      case Key21::KEY_9:
        Serial.println("9");
          stepper.setSpeed(130); 
          stepper.step(32608);  
          delay(200);
        break;

      default:
        Serial.println("WARNING: undefined command:");
        break;
    }
  irController.getKey();
  }
}
1 Like

You used a irController.getKey(); twice - at the beginning and at the finish of the loop(). These two calls interfere with each other, nullifying the commands. Remove the line at the end of the loop

1 Like

Removed but it didn't fix my problem any other ideas?

1 Like

The problem is in the use of stepper.xyz()... if you comment-out all the stepper.xyz() commands, you do not have problems with pressing more buttons.

1 Like

I'm unfamiliar with your library, but I'll ignore it and look at logic, math, loops, and statements instead.

okay... so the delay();s are unnecessary, the second irController.getKey();
at the bottom is also unnecessary.

Yes but if I do that then my motor wont turn

1 Like

So I commented out all of the delays and removed the second get key and still no luck

1 Like

The motor commands you use are incorrect. Get an example from the library.

Your issue was pressing a number stopped you from pressing any number. I solved that. You need to fix the broken part (the motor commands).

1 Like

stepper.step() is blocking, and will move at the exact same speed set before it, and block code, until it reaches it's destination in steps.

here it will never reach it's destination, and thus block the code until reset.

Do you have any ideas on what I should do. Sorry this is all new to me. I simply want to use the remote so that when I Press button one its at one speed, button 2 the next speed etc?

1 Like

1: the 28byj-48 takes 2024 4048 4096 steps to complete 1 rotation, so STEPS should be 2024 4048 4096.
2: the 28byj-48 can do a max of 17 RPM, so stepper.setSpeed(1...130); should be stepper.setSpeed(1...17);.
3: NEVER use stepper.setSpeed(0); it will block your code "forever" or until next reset.
4: do not make the stepper "step" though large amounts of steps, it will take "forever" to finish, and will block the code while it's rotating, so the main code literally can't run anything else, so use something like stepper.step(8096); //2 360° rotations. instead of stepper.step(32608); //16.11 360° rotations.

Will that not mean that the motor will stop after two rotations when I actually want it to stop when I either press a different speed or turn it off. hence the reason for the (0)?

1 Like

stepper.setSpeed(0); is pointless, stepper.step(); is blocking, so you can't stop the motor anytime, the motor will only stop if it's done "stepping", you reset the Arduino, or if you unplug the Arduino.

you might what to try the Unistep2 library for this project, it's supposed to be non-blocking, that's why I'm downloading it.

So basically what you are saying is this code sucks as it is built ground up to stop unless I unplug to reset the Arduino.

1 Like

kinda, yes, sorry that it's a blocking library. :slightly_frowning_face:

It's good to know. I could for the likes of me get any of the other libraries to work and think it may be due the IR sensor and remote I am using. (IRremore, IRLremote and IRSremote)

1 Like

This is what I have

1 Like

1 small issue, Unistep2 allows you to set speed once, but we have "CheapStepper", I'll try this one too.