Controlling stepper motor with IR remote

Hi there,

I am relatively new to using Arduino and my coding isn't the greatest. I have started a project with the use of a stepper motor and an IR remote. I am turning a circle in 8 steps by using an IR remote buttons E.g. from starting position button 1 turns it 45 degrees, button 2 turns it 90 degrees, button 3 turns it 135 degrees etc. I want there to be a button that can reverse the number of steps made so that it goes back to its original position e.g. if button 3 is pressed then the stepper rotates 135 degrees then the button (i am needing) will reverse 135 degrees to original position. But i need the button to work for any number of steps taken so the code would be : [minus the previous steps]. I understand that a stepper motor can't remember its homing position and a switch is normally advised. Although I plan to ensure the stepper motor is in the correct position before I switch it on.

it would be greatly appreciated if anyone could help?

You have 3 projects.

  1. Mastering a stepper.
  2. Mastering IR remote.
  3. Make the code doing what You want.

To be sure it's worth the time assisting You I would like:

  1. Schematics.
  2. The present code.

All according to the first topic telling how to get the best from this forum. You did read it of course?

#include "Stepper.h"
#include "IRremote.h"

/----- Variables, Pins -----/
#define STEPS 32 // Number of steps per revolution of Internal shaft
int Steps2Take; // 2048 = 1 Revolution
int receiver = 4 ; // Signal Pin of IR receiver to Arduino Digital Pin 6

/-----( 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, 8, 10, 9, 11);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

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

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

{
switch(results.value)

{

  case 0xFFA857: // 1 button pressed
                  small_stepper.setSpeed(500); //Max seems to be 500
                  Steps2Take  =  256 ;  // Rotate CW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;

  case 0xFF629D: // 2 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  512 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;
  case 0xFF229D: // 3 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  768 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;

  case 0xFF449D: // 4 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  1,024 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;

  case 0xFF666D: // 5 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  1,280 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;
  case 0xFF385D: // 6 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  1536 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;

  case 0xFF764D: // 7 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  1,792 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;

  case 0xFF843D: // 8 button pressed
                  small_stepper.setSpeed(500);
                  Steps2Take  =  2,048 ;  // Rotate CCW
                  small_stepper.step(Steps2Take);
                  delay(2000); 
                  break;                                                
}

  irrecv.resume(); // receive the next value
             digitalWrite(8, LOW);
             digitalWrite(9, LOW);
             digitalWrite(10, LOW);
             digitalWrite(11, LOW);       

}

}/* --end main loop -- */

elegoo

Thank you for your reply, I appreciate it greatly.

I have attached both the code and schematics below.

thank you

You made a good try.

What happened to the code? Some is in code tags but the beginning is like copied text.
Don't use Fritzings. They are more or less useless toys. That design will not work at all. There is no power supply.
Don't run any motor power through a bredboard. Trouble is to be expected.

What does the presented code do and what differs from what You want?

think there was an error when I copied the code through to this forum. Everything is running perfectly, I just need to find out how I can make a button on the remote that will reverse the previous steps that the arduino took. Basically like [button 9 : minus previous number of steps ]

if that makes sense

Edit -> Copy for Forum

1 Like

Okey. Keep the current position, number of steps, stored in a variable. Make the switch case code for button 9 to step backwards the number of steps stored.

Especially when they are communicated as thumbnail images...

Yes. They blurr out when zooming...

//www.elegoo.com
//2016.12.12

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 4 ; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( 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, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

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

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

  {
    switch(results.value)

    {

      case 0xFFA857: // 1 button pressed
                      small_stepper.setSpeed(500); //Max seems to be 500
                      Steps2Take  =  256 ;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF629D: // 2 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  512 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
      case 0xFF229D: // 3 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  768 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF449D: // 4 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,024 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF666D: // 5 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,280 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
      case 0xFF385D: // 6 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1536 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF764D: // 7 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,792 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF843D: // 8 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  2,048 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;                                                
    }
    
      irrecv.resume(); // receive the next value
                 digitalWrite(8, LOW);
                 digitalWrite(9, LOW);
                 digitalWrite(10, LOW);
                 digitalWrite(11, LOW);       
  }  


}/* --end main loop -- */

I have lost you here, would you be able to add it to the code I uploaded?

For every move You make accumulate the steps made. + for CW, - for CCW moves. Your comments telling CW or CCW looks wrong to me.This is done in every switch case where the stepper is move. Name it currentPosition, declared as unsigned long.
Any help?

//www.elegoo.com
//2016.12.12

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 4 ; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( 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, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'
unsigned long currentPosition; // Added by Railroader

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

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

  {
    switch (results.value)

    {

      case 0xFFA857: // 1 button pressed
        small_stepper.setSpeed(500); //Max seems to be 500
        Steps2Take  =  256 ;  // Rotate CW
        small_stepper.step(Steps2Take);
        currentPosition += 256;//Added by railroader
        delay(2000);
        break;

      case 0xFF629D: // 2 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  512 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 512;//Added by railroader
        delay(2000);
        break;
      case 0xFF229D: // 3 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  768 ;  // Rotate CCW
        currentPosition += 768;//Added by railroader
        small_stepper.step(Steps2Take);
        delay(2000);
        break;

      case 0xFF449D: // 4 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1024 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1024;//Added by railroader
        delay(2000);
        break;

      case 0xFF666D: // 5 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1280 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1280;//Added by railroader
        delay(2000);
        break;
      case 0xFF385D: // 6 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1536 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1536;//Added by railroader
        delay(2000);
        break;

      case 0xFF764D: // 7 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1792 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1792;//Added by railroader
        delay(2000);
        break;

      case 0xFF843D: // 8 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  2048 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += ;//Added by railroader
        delay(2000);
        break;
      case 0x?????? : // 9 button pressed
          small_stepper.setSpeed(500);
        Steps2Take  =  -currentPosition;
        currentPosition = 0;
        small_stepper.step(Steps2Take);
        delay(2000);
        break;


        irrecv.resume(); // receive the next value
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(11, LOW);
    }


  }/* --end main loop -- */


thank you for that, I understand the basis of what you are saying current position does. I forgot to change the ccw to cw, they are all turning cw. I have uploaded the sketch to the arduino but now the stepper motor isn't turning at all. Been changing some things to get it working but getting no where. I will attach updated sketch below. thanks again

//www.elegoo.com
//2016.12.12

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 4 ; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( 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, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

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

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

  {
    switch(results.value)

    {

      case 0xFFA857: // 1 button pressed
                      small_stepper.setSpeed(500); //Max seems to be 500
                      Steps2Take  =  256 ;  // Rotate CW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF629D: // 2 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  512 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
      case 0xFF229D: // 3 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  768 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF449D: // 4 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,024 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF666D: // 5 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,280 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;
      case 0xFF385D: // 6 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1536 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF764D: // 7 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  1,792 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;

      case 0xFF843D: // 8 button pressed
                      small_stepper.setSpeed(500);
                      Steps2Take  =  2,048 ;  // Rotate CCW
                      small_stepper.step(Steps2Take);
                      delay(2000); 
                      break;                                                
    }
    
      irrecv.resume(); // receive the next value
                 digitalWrite(8, LOW);
                 digitalWrite(9, LOW);
                 digitalWrite(10, LOW);
                 digitalWrite(11, LOW);       
  }  


}/* --end main loop -- */

You have not used the code I sent. It looks like the same code as first posting. What is the misunderstanding? What changes have You made?

Why the coding 2,048? Use 2048, nothing else.

//www.elegoo.com
//2016.12.12

#include "Stepper.h"
#include "IRremote.h"

/*----- Variables, Pins -----*/
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 4 ; // Signal Pin of IR receiver to Arduino Digital Pin 6

/*-----( 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, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'
unsigned long currentPosition; // Added by Railroader

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

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

  {
    switch (results.value)

    {

      case 0xFFA857: // 1 button pressed
        small_stepper.setSpeed(500); //Max seems to be 500
        Steps2Take  =  256 ;  // Rotate CW
        small_stepper.step(Steps2Take);
        currentPosition += 256;//Added by railroader
        delay(2000);
        break;

      case 0xFF659D: // 2 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  512 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 512;//Added by railroader
        delay(2000);
        break;
      case 0xFF229D: // 3 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  768 ;  // Rotate CCW
        currentPosition += 768;//Added by railroader
        small_stepper.step(Steps2Take);
        delay(2000);
        break;

      case 0xFF449D: // 4 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1024 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1024;//Added by railroader
        delay(2000);
        break;

      case 0xFF666D: // 5 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1280 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1280;//Added by railroader
        delay(2000);
        break;
      case 0xFF385D: // 6 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1536 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1536;//Added by railroader
        delay(2000);
        break;

      case 0xFF764D: // 7 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  1792 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 1792;//Added by railroader
        delay(2000);
        break;

      case 0xFF843D: // 8 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  2048 ;  // Rotate CCW
        small_stepper.step(Steps2Take);
        currentPosition += 2048 ;//Added by railroader
        delay(2000);
        break;
      case 0xFF629D : // 9 button pressed
          small_stepper.setSpeed(500);
        Steps2Take  =  -currentPosition;
        currentPosition = 0;
        small_stepper.step(Steps2Take);
        delay(2000);
        break;


        irrecv.resume(); // receive the next value
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        digitalWrite(11, LOW);
    }


  }/*
}--end main loop -- */
}

Hi there, sorry I uploaded the old code by mistake. This is the new code, the stepper isn't picking up any signal from the ir remote. Even when i press the buttons that worked in the old code. Do you know what the problem is? thanks again