Go Down

Topic: Motor Knob Trouble (Read 503 times) previous topic - next topic

siren215

Code: [Select]
Initial Position
================
When it starts up the Arduino has no means of knowing where the stepper motor is positioned - for example somebody might have moved it manually when the power was off.

The usual way to establish a datum for counting steps is with a limit switch. At startup the Arduino will move the motor until it triggers the switch. The Arduino will then regard that step position as step zero for the purpose of future position keeping.


I am not sure how to implement this. I am using a basic motor knob program. Trying to move the motor 45 deg fixed position. I not failing to determine its initial position. It should be 0 deg for 0 V and 45 deg for 5 V.

Can you tell me where am i going wrong.

Code: [Select]
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
#define ANALOG_IN A1

void setup()

  stepper.setMaxSpeed(1000);
}

void loop()
{
  // Read new position
  int analog_in = analogRead(ANALOG_IN);
  stepper.moveTo(analog_in);
  stepper.setSpeed(100);
  stepper.runSpeedToPosition();
}

Coding Badly


Please do not hijack.  Thread split.


Robin2

#2
Apr 16, 2016, 10:59 am Last Edit: Apr 16, 2016, 11:02 am by Robin2
I asked the Moderator to move your Question as it is not relevant to my Stepper Motor Basics Tutorial.

Stepper motors do not have any way to report their position. The usual way to deal with that is to have a function that causes them to move one step at a time until they press a limit switch. The position the motor is at then is taken to be Zero.

Something like this pseudo code

Code: [Select]
void (moveToHome() {
    limitSwitchState = digitalRead(limitSwitchPin);
    while (limitSwitchState == HIGH) { // this assumes it will be LOW when pressed
       // move one step towards switch
       limitSwitchState = digitalRead(limitSwitchPin);
    }
    stepperPosition = 0;
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up