Accelstepper homing every time?

I'm a (mostly) clueless farmer trying to make an automatic egg incubator using arduinos and cunning, everything has been going (relatively) swimmingly so far, but my stepper motor automatic turner is causing some stress.

The setup is an old computer PSU, a DRV8825, and this stepper motor.

Goal is on startup, the turner turns all the way CCW, hits home switch, sets 0, turns to 35 turns (level) then to 70 turns, then back to 0. looping between 70 and 0 every hour (or so)

Currently it seems to work for a few hours, then loses position and tries to pass a limit either top or bottom, shearing a coupler and making me sad.

Code is pretty simple.

// Blocking.pde
// -*- mode: C++ -*-
//
// Shows how to use the blocking call runToNewPosition
// Which sets a new target position and then waits until the stepper has 
// achieved it.
//
// Copyright (C) 2009 Mike McCauley
// $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 8, 9);//1 means a stepper driver (with Step and Direction pins)
#define home_switch 13 // Pin 9 connected to Home Switch (MicroSwitch)
long initial_homing=-1;  // Used to Home Stepper at startup

void setup()
{  
    pinMode(home_switch, INPUT_PULLUP);
  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    delay(1);
}

  stepper.setCurrentPosition(0);  // Set the current position as zero for now
  stepper.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepper.setAcceleration(1000.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepper.moveTo(initial_homing);  
    stepper.run();
    initial_homing++;
    delay(1);
  }
  
  stepper.setCurrentPosition(0);
  stepper.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepper.setAcceleration(1000.0);  // Set Acceleration of Stepper
  stepper.runToNewPosition(357000);
  delay (1800000);   
}

void loop()
{    
    stepper.runToNewPosition(714000);
    delay (3600000);
    stepper.runToNewPosition(0);
    delay (3600000);
}

Is there a mistake somewhere causing it to lose position, or is there a way I can force it to home off the home switch every loop, so it only has the run to 70 turns to go wrong?

Thanks!

  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepper.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepper.run();  // Start moving the stepper
    delay(1);
}

There is no excuse for the delay() in there.

You do NOT want to change the value of initial_homing until you KNOW that the stepper has stepped.

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated

Wouldn't it be better to just count how many steps is needed to do that, and step away from the limit switch that number of steps every time?

is there a way I can force it to home off the home switch every loop, so it only has the run to 70 turns to go wrong?

Of course you can do that.

PaulS:
You do NOT want to change the value of initial_homing until you KNOW that the stepper has stepped.

I don't think the OP is using the variable the way you think.

AFAIK he is just using it as a moving target (the carrot dangling before the donkey) to keep the motor moving one step at a time until it triggers the home switch.

@DrBerk, you seem to take the trouble in setup() to identify the HOME and the OUT limits but your code in loop() makes no use of the OUT limit.

Are you sure your motor is not missing steps when it is following the instructions in loop(). Missing a few steps on each iteration might accumulate to a big position error after a longer period of time.

If the motor is not missing steps then there does not seem to be a programming reason for the problems and I would look for a power supply problem that may, for example, be causing the Arduino to restart.

...R

It's a bit difficult to explain if you have no context.

Basically, I need this to go to 3 positions.

On startup it should go to 0

Then as part of the startup, run to 35 full turns and wait for 30 minutes. (This allows me to add or remove eggs without adding extra cost or code)

Then it should run to 70 turns, wait an hour, go to 0 turns, then loop forever.

This seems to work for a number of hours, then goes a bit wrong, hits hardware limits and shears couplers.

The PSU I'm using has 7A & 13A +12v rails (stepper is rated 1.68 12-24v) so I suppose there's room for more power there, but there's not a lot of heat coming out of anything, so it doesn't seem especially likely to be that.

I can't see it being an arduino power issue, as it'll self home on startup.

I mentioned two possible reasons for the problem in the last two paragraphs in Reply #2 and you do not seem to have responded to either of them.

The power supply issue is not whether sufficient power is available, but whether it is occasionally very briefly interrupted - perhaps when some other device is turned on. A glitch for less than a microsecond will be enough to upset an Arduino.

A work around would be to include in your program code that checks the state of the limit switches between every step.

...R

There should be no power interruption as the 300w PSU is powering 2 arduinos, a bme280, a 16x2 display and the stepper motor.

Everything else is 240vac, and powered independently.

However, everything is possible, so I will have a look to see if I can see any issues there.

If it's missing steps, it's missing a lot in a short period, and giving up the goat mid way through a move cycle, there's 80 full turns between my physical limits, I'm using approximately 70 turns, and a stepper motor that due to gearing has 10200 steps per rev.

I realize my electrical system is atypical but when my fridge starts it drops the voltage sufficiently to cause my radio to turn off.

Maybe try powering the Arduino from a large lead-acid battery to see what happens.

...R

(deleted)

You should have limit switches at both extremes of travel to stop the motor BEFORE mechanical damage PLUS a third switch for homing, the homing routine should be a function that is called from setup() at startup or any time a manual homing is needed.

Yup, I think that's the plan, can't rely on guesswork and hope.

Following from Reply #8, and probably simpler than what I suggested in Reply #4, would be to have the limit switches wired so that they just cut power to the motor and signal an error to Arduino program.

On the other hand, something is causing the system to misbehave and it could be fixed.

...R

Have ordered more microswitches.

Is perfectly feasible mechanically to have a home, 2 soft limit and 2 failsafe switches, I've got plenty of space to work with in that respect.

I'll get there, it's just a pain to work around the necessity to use the bloody machine as a whole while trying to troubleshoot