Need help whit code for ir controlled step motor

Hi everyone,
i'm trying to build my first arduino project:

i need to control a stepper motor using an infrared remote.
I setted up my arduino nano with an a4988 cabled to a stepper motor (5v), everything seem to be correctly wired (all components are working trying with some sample sketchs)
but i'm having some trouble with the code i need.

I'd run my motor forward with one button and reverse with another one.
My input pin from the ir is 9

Can anyone help me?

Have you compiled and run any of the example code for the IR remote library?
Have you compiled and run any of the example code for your motors?

Yes, i can read from serial port the ir signal.

The motor works with the example code i used.

I don't know how to combine the codes to control the stepper.

I used this for the ir remote reading:
#include <IRremote.h>

int RECV_PIN = 9;
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);
irrecv.resume(); // Receive the next value
}
}

and this one to test the stepper with a4899:

/* Simple Stepper Motor Control Exaple Code
*

*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay

digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
delay(1000);
}

How am i suppose to merge the for just simply move it forward with one button and reverse with another one?

How you merge it all depends on how you want the motor to move, and how responsive to new inputs you want the code to be.

I think that the behaviour of the movement should be setted by variables.
I'm a noob about coding and i can't find something that i can easily edit to make my project work.

tried to wrote this but i receive a compilation error about the results

#include <IRremote.h>

#include <Servo.h>

//defines input pin for the ir receiver
int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;

// defines pins numbers about stepper
const int stepPin = 3;
const int dirPin = 4;

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

// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

}

void loop() {
if (results==E0E0E01F)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
if (results==E0E0D02F)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);

}

}

Note the difference

Please remember to use code tags when posting code.

It also helps you to get help if instead of just saying "I got an error", try posting the error message.

Thank you for your help Shannon and sorry for haven't use tags for code.

[ code ] void loop() {
if (results==0xE0E0E01F)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
if (results==0xE0E0D02F)
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
[ \code ]

I still obtain an error:

C:\Users\ALESSA~1\AppData\Local\Temp\arduino_modified_sketch_412538\sketch_aug20b.ino: In function 'void loop()':

sketch_aug20b:29:14: error: no match for 'operator==' (operand types are 'decode_results' and 'long unsigned int')

if (results==0xE0E0E01F)

   ~~~~~~~^~~~~~~~~~~~

sketch_aug20b:32:14: error: no match for 'operator==' (operand types are 'decode_results' and 'long unsigned int')

if (results==0xE0E0D02F)

   ~~~~~~~^~~~~~~~~~~~

C:\Users\ALESSA~1\AppData\Local\Temp\arduino_modified_sketch_412538\sketch_aug20b.ino: At global scope:

sketch_aug20b:38:1: error: expected declaration before '}' token

}

^

Più di una libreria trovata per "Servo.h"

Usata: C:\Users\Alessandro\Documents\Arduino\libraries\Servo

Non usata: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.49.0_x86__mdqgnx93n4wtt\libraries\Servo

exit status 1

no match for 'operator==' (operand types are 'decode_results' and 'long unsigned int')

Take a close look at reply #6.

Don't forget to update results.

Thanks, i corrected the code like you suggested in post #6 and it finished the compilation without errors. Btw it still don't work :pensive:

The stepper won't move when i press the remote. Any suggestions? ( I haven't understand how i am supposed to update the results...)

Here's the modified code

#include <IRremote.h>

#include <Servo.h>

//defines input pin for the ir receiver
int RECV_PIN = 9;
IRrecv irrecv(RECV_PIN);
decode_results results;

// defines pins numbers about stepper
const int stepPin = 3; 
const int dirPin = 4;



void setup() {
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}
 
 // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);  

}

void loop() {
  if (results.value==0xE0E0E01F)
   digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000); 
  if (results.value==0xE0E0D02F)
  digitalWrite(stepPin,HIGH); 
    delayMicroseconds(2000);

 }

Don't forget to update results.

You may wish to use the IDE's auto format tool to see that your code needs some more of these { }

no match for 'operator==' (operand types are 'decode_results' and 'long unsigned int')

The IR code that you posted is for an older version of the IRremote library and may not work with the new version. I suggest that you use example code from the new version of the IRremote library since that is the version that you have installed.

Here is a demo code that may help. It uses the new version of the IRremote library. I changed the pins and remote key codes to suit my hardware so that I can test the code. To find your codes just run the program, open serial monitor at 115200 baud and press the keys that you want for fwd and reverse. Note the codes and replace them in my code with your key codes.

#include <IRremote.h>
#include <Stepper.h>

const byte IR_RECEIVE_PIN = 14; // pin A0 
const byte enablePin = 8; // for my CNC shield setup

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 2, 5);

void setup()
{
   Serial.begin(115200);

   Serial.println("IR receive test");
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

   myStepper.setSpeed(600);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}

void loop()
{
   if (IrReceiver.decode())
   {
      unsigned long keycode = IrReceiver.decodedIRData.command;
      Serial.println(keycode, HEX);
      if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
      {
         IrReceiver.resume();
         return;
      }
      IrReceiver.resume();
      switch (keycode)
      {
         case 1:
            myStepper.step(5 * stepsPerRevolution);
            break;
         case 2:
            myStepper.step(-5 * stepsPerRevolution);
            break;
      }
   }
}

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags

I thought that you are using a stepper. Is it a stepper or a servo?

stepper i'm trying to controlling it via an a4988 module. i'm trying your code

My key are "7" and "B", I can't find where i have to make the substitution in your code. I'm sorry

         case 7:    //1:    ************* put code for forward here
            myStepper.step(5 * stepsPerRevolution);
            break;
         case B:   //2:   ************* put code for reverse here
            myStepper.step(-5 * stepsPerRevolution);
            break;

Your codes are 0xE0E0E01F and 0xE0E0E02F, I thought?

yes, when i tried whit the first code those were the results.
Whit the second one at 115200 baud i received 7 and B

The new IRremote library can return raw codes (like 0xE0E0E01F) or decoded address and command. I use the commands because they are shorter and common between remotes of the same type (NEC in this case).

#include <IRremote.h>
#include <Stepper.h>

const byte IR_RECEIVE_PIN = 9; // pin D9 
const byte enablePin = 8; // for my CNC shield setup

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 2, B);

void setup()
{
   Serial.begin(115200);

   Serial.println("IR receive test");
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

   myStepper.setSpeed(600);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}

void loop()
{
   if (IrReceiver.decode())
   {
      unsigned long keycode = IrReceiver.decodedIRData.command;
      Serial.println(keycode, HEX);
      if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
      {
         IrReceiver.resume();
         return;
      }
      IrReceiver.resume();
      switch (keycode)
      {
         case 1:
            myStepper.step(7 * stepsPerRevolution);
            break;
         case 2:
            myStepper.step(B * stepsPerRevolution);
            break;
      }
   }
}

compilation error 'B' was not declared in this scope

Really, my reply #15 was not clear enough?

Wrong place. I changed reply #15, take a look at it.