Stepper and remote

129 views
Skip to first unread message

Paul

unread,
Apr 2, 2021, 5:10:05 PM4/2/21
to accelstepper
Hello, I need your help guys, I want to control 2 stepper motors with the +/- button of a IR Remote, but it looks like the steppers are moving just one step at the time when I push the + or - of the remote, do you know why?

thank youuu

#include <AccelStepper.h>
#include <IRremote.h>

AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);

int receiver = 12;    //IR reciever pin

IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'

void setup()
{
 Serial.begin(9600);
 
 stepper1.setMaxSpeed(300.0);
 stepper1.setAcceleration(100.0);
 stepper1.moveTo(2048); 
 
 stepper2.setMaxSpeed(300.0);
 stepper2.setAcceleration(100.0);
 stepper2.moveTo(-2048); 
 

 irrecv.enableIRIn(); // Start the receiver
}

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

     case 0xFF629D: // UP button pressed
                   stepper1.run();
                   stepper2.run();
                   break;
                 
     case 0xFFA857: // DOWN button pressed           
                   stepper1.run();
                   stepper2.run();
                   break;
   }  
     irrecv.resume(); // receive the next value
 }  
}

gjgsm...@gmail.com

unread,
Apr 2, 2021, 10:23:03 PM4/2/21
to accelstepper
What exactly are the motors supposed to do when you press the buttons. eg When you press the + button, do you want both motors to go to their targets without stopping? Are you pressing the button momentarily or are you holding the button down while the motor(s) move and then letting it up when you want the motor(s) to stop?

Geoff

Paul

unread,
Apr 3, 2021, 3:54:02 AM4/3/21
to accelstepper
Thank you for you answer.
Yes exactly, so for exemple when I press the + button (momentarily) the 2 motors do 2048 steps without stoping (one clockwise and the other counterclockwise)   

Paul

roxy_s...@hotmail.com

unread,
Apr 3, 2021, 8:49:39 AM4/3/21
to accelstepper
hey paul,

the  stepper1.run(); function you have within an switch case is only called when you recieve the ir signal.where as the .run() needs to be called once every 100ms or so.
so it will not continue to run.
if you put your run function to run in the loop always it will only move once given a position.
so changing your code to put the move to pos inside the case and then the run function outside in the loop it will work.

#include <AccelStepper.h>
#include <IRremote.h>
AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 4, 5, 6, 7);
int receiver = 12; //IR reciever pin
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void setup()
{
Serial.begin(9600);

stepper1.setMaxSpeed(300.0);
stepper1.setAcceleration(100.0);
stepper2.setMaxSpeed(300.0);
stepper2.setAcceleration(100.0);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch (results.value)
{
case 0xFF629D: // UP button pressed
stepper1.moveTo(2048);       
stepper2.moveTo(-2048);
break;
case 0xFFA857: // DOWN button pressed
stepper1.moveTo(-2048);
stepper2.moveTo(2048);
break;
}
irrecv.resume(); // receive the next value
}
stepper1.run();         //stepper 1 and 2 will now run untill there pos, and will only change again when they recieve a different pos from within the ir recieve case.
stepper2.run();
}


to solve the problem in future put the .run() function somewhere it is called  all the time,and only manipulate the moveto() positions and you will be fine.
hope it helps

gjgsm...@gmail.com

unread,
Apr 4, 2021, 12:29:09 AM4/4/21
to accelstepper
Can I also add...

After the first movement to (2048) of one stepper, the next movement to (-2048) will be a total of (4096) steps. If you only want a total of (2048) steps only from the end of a '+ve button' command to the end of a '-ve button' command, then use (1024) steps in your code.

If you want the steppers to reach their targets before anything else happens then try this code....

------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
      switch (results.value)
      {
        case 0xFF629D: // UP button pressed
        stepper1.moveTo(2048);       
        stepper2.moveTo(-2048);
        break;
        case 0xFFA857: // DOWN button pressed
        stepper1.moveTo(-2048);
        stepper2.moveTo(2048);
        break;
       }
      irrecv.resume(); // receive the next value
    }

while(stepper1.isRunning() || stepper2.isRunning)
  {
   stepper1.run();         //stepper 1 and 2 will now run until they reach their targets.
   stepper2.run();
   }
}
-----------------------------------------------------------------------------------------------------------------------------

Geoff

Reply all
Reply to author
Forward
0 new messages