Loop for stepper motor with delay

Hello everyone.

For my project, I need to program my stepper motor to perform 3 movements ( to press a button), wait a couple of hours, and perform the same 3 movements again. I've successfully coded the 3 movements with delays between movements. However, I am unsure how to continuously loop them and add a long delay after the 3 movements.

It would mean a lot if someone could help me out

Best,
FL

Push_Button_32019.ino (1.6 KB)

However, I am unsure how to continuously loop them

That's what the loop() function is for.

and add a long delay after the 3 movements.

delay(aReallyLongTime);

For some value of aReallyLongTime.

I've attempted to code a for loop. However, it doesn't iterate more than once. As well, if I place a delay at the end of the loop, it will compile, but the stepper motor won't move.

 for (int i=1;i>=1;i++) // infinite loop?

No, that is not an infinite loop. When i gets to 32767, it will increment to -1, and the loop will end.

      delay(3000); // pushes button

That is NOT what that code does.

You appear to be trying to implement a state machine using 4 boolean variables. An enum and one instance would be much clearer. That way, it would be impossible to be in more than one state at a time.

There is no reason to have a for loop in the loop() function. The loop() function is already called in a loop. Let it do ALL of the looping.

If you want to continue with the approach you have, use nested ifs, rather than compound ifs. There is no reason to test that the stepper is done 4 times.

Super beginner over here, do you mind explaining how I could use enum instead of boolean variables?

do you mind explaining how I could use enum instead of boolean variables?

enum State { ThumbTwiddling, Eating, Sleeping, Showering };

State currentState = ThumbTwiddling;

void loop()
{
   switch(currentState)
   {
      case ThumbTwiddling:
         DoNothing();
         currState = Eating;
         break;
      case Eating:
         Eat();
         currState = Sleeping;
         break;
      case Sleeping:
         Nap();
         currState = Showering ;
         break;
      case Showering :
         GetWetAllOver();
         currState = ThumbTwiddling;
         break;
   }
}

As you can see, you can't be in more than one state at a time.

thanks for the suggestions! I tried to follow a similar format. It compiles but my stepper doesn't move. Any suggestions?

Push_Button_320.ino (1.38 KB)

Does your motor move properly when you use one of the examples that comes with the library?

It is not
at all clear
why your code
marches across the
screen the way
it does.

It is not clear when the call to run() happens only after you tell the motor to stop.

nevermind I got it... posting it here for others/ future me


#include <CustomStepper.h>

CustomStepper stepper(8, 9, 10, 11, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
// talk to which pin numbers

//variables = false
boolean rotate_ON = false;
boolean rotate_GO = false;
boolean rotate_HOME = false;
boolean endthis = false;
boolean initialCycle = false;

void setup()
{
//sets the RPM
stepper.setRPM(12); //17
//sets the Steps Per Rotation, in this case it is 64 * the 283712/4455 annoying ger ratio
//for my motor (it works with float to be able to deal with these non-integer gear ratios)
stepper.setSPR(4075.7728395);
}

void loop()
{

// TURN ON
if (stepper.isDone() && rotate_ON == false)
{
delay(1000);
stepper.setDirection(CW); //counterclock wise
stepper.rotateDegrees(45.0);
rotate_ON = true;
endthis = false;
}

// TURN GO
if (stepper.isDone() && rotate_ON == true && rotate_GO == false)
{
delay(4000);
stepper.setDirection(CCW); //clockwise
stepper.rotateDegrees(90.0);
rotate_GO = true;
rotate_HOME = false;
}

// CENTER
if (stepper.isDone() && /rotate_ON == true && rotate_GO == true &&/ rotate_HOME == false)
{
delay(1000);
stepper.setDirection(CW); //counterclock wise
stepper.rotateDegrees(35.0);
rotate_HOME = true;
}

// // TURN OFF
if (stepper.isDone() && rotate_GO == true && rotate_HOME == true && endthis == false)
{
//stepper.setDirection(STOP);
//stepper.rotate();
endthis = true;
rotate_ON = false;
rotate_GO = false;
if(initialCycle != false)
{
delay(28800005); // 8 hours
}
else
{
initialCycle = true; // 72 hours
delay(259200005);
}
}

stepper.run();

}