Ir Remote to Stepper

Hello,

I'm trying to write a simple ir remote to motor program but it doesn't continue while I hold the button down. It just runs the stepper.step once and then stops. Only when I let go of the one button on the remote will it do it again. I want the stepper to continue until I let go of that button. I thought it was the delay at the end of the translateIR function but that didn't work. Should I eliminate the delay?

Thanks,

Josh




#include <Stepper.h>
#include "IRremote.h"
int receiver = 5; // Signal Pin of IR receiver to Arduino Digital Pin

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);
decode_results results;
// create instance of 'irrecv'
// create instance of 'decode_results'
// Define motor pins connected to ULN2003 driver
#define MP1  8  // IN1 on the ULN2003
#define MP2  9  // IN2 on the ULN2003
#define MP3  10 // IN3 on the ULN2003
#define MP4  11 // IN4 on the ULN2003

#define SPR 2048  // Steps per revolution (for 28BYJ-48 stepper motor)
int homePos = 1024;
int loc=0;
// Create Stepper object
Stepper stepper(SPR, MP1, MP3, MP2, MP4);
void translateIR() // takes action based on IR code received

// describing Remote IR codes 

{

  switch(results.value)

  {
  case 0xFFA25D: Serial.println("POWER"); break;
  case 0xFFE21D: Serial.println("VOL STOP"); break;
  case 0xFF629D: Serial.println("MODE"); break;
  case 0xFF22DD: Serial.println("PAUSE");    break;
  case 0xFF02FD: Serial.println("FAST BACK");    break;
  case 0xFFC23D: Serial.println("FAST FORWARD");   break;
  case 0xFFE01F: Serial.println("EQ");    break;
  case 0xFFA857: Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("VOL+");    break;
  case 0xFF9867: Serial.println("RETURN");    break;
  case 0xFFB04F: Serial.println("USB SCAN");    break;
  case 0xFF6897: Serial.println("0");    break;
  case 0xFF30CF: Serial.println("1");  stepper.step(1000);  break;
  case 0xFF18E7: Serial.println("2");  stepper.step(-1000) break;
  case 0xFF7A85: Serial.println("3");    break;
  case 0xFF10EF: Serial.println("4");    break;
  case 0xFF38C7: Serial.println("5");    break;
  case 0xFF5AA5: Serial.println("6");    break;
  case 0xFF42BD: Serial.println("7");    break;
  case 0xFF4AB5: Serial.println("8");    break;
  case 0xFF52AD: Serial.println("9");    break;
  case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

  default: 
    Serial.println(" other button   ");

  }// End Case

  delay(10); // Do not get immediate repeat


} //END translateIR


void setup() 
{
  Serial.begin(9600);
  // Set motor speed
  // Set speed to 10 RPM
  
// HAVE TO SET STEPPER SPEED HERE
stepper.setSpeed(10);

  irrecv.enableIRIn();
  Serial.begin(9600);
 }

void loop()  
{
if (irrecv.decode(&results)) // have we received an IR signal?

  {
    translateIR(); 
    irrecv.resume(); // receive the next value
  }  
} 

Just a hunch, but your code looks strange. Try starting with one of the examples for the library.

case 0xFF18E7: Serial.println("2");  stepper.step(-1000) break;
case 0xFF18E7: 
Serial.println("2");  
stepper.step(-1000)      // <-----<<<<< You need a semicolon.
break;

  • Which IR remote are you using ?

  • Do you have the latest IR remote library installed ?
    4.4.0 or newer

This is from a RexQualis kit. I don't think it is using the latest library since the kit is so old. Is there a way to fix this?

I did see it had a repeat case in the switch. I have to check but it seems that will fire if the button is held. I have an update to the code to continue moving fwd or backwards if the repeat is firing. I have to try it.

#include <IRremote.h>

int RECV_PIN = 5; // Pin connected to the IR receiver Signal pin
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX); // Print the hex code of the button
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}
  • What happens when you run the above sketch ?

  • Please show us an image of your remote.




  • The latest IRremote library that the majority of us use is IRremote version 4.4.0 or 4.5.0

A few years ago "repeat" is all "f"s ... 0xffffff... and I remember Shariff was the author.

What happens when you hold down a button is remote dependent. Some controls send a different command to indicate "repeat last command", others resend the command and some just sent the command once.

Build a test program to see what your control does when you hold down a key. There should be library example programs that do this or are close with a few modifications.

If you have a cheap logic analyzer that would be even faster. Search something like 24mhz 8 channel logic analyzer. Should be less than $20 at places like Amazon, Aliexpress, Ebay, etc. Totally worth it if you are having any serial protocol (SPI, I2C, serial, IR) problems.