Stepper motor attached with 3 micro switches to do 3 different commands

Hi all,

Im having trouble telling my stepper motor to go clockwise continually until it hits a micro switch that then turns it anticlockwise to a home micro switch which turns it off.

3 different micro switches are attached to the Arduino along with the STEP and DIR pin from the easy driver. The easy driver has 12V going through it also the 4 cables from the motor.

Im trying to use the micros switches as a toggle switch which I believe is achieved with the first 2 lines of code under void loop.

I am able to run the motor continually with no switches on the circuit or the code that is attached now but when I try to put switches on and the code it doesn't want to play ball

The first of the micro switches i'm using is to start the motor rotating clockwise. The second is to stop the motor for 1 second and then is to revers the motor rotating anticlockwise. Finally the third is to stop the motor.

If anyone knows where my code is going wrong or what I need to do in order to make this system function as I need it would be greatly appreciated.

components:

stepper motor JK42hm34-1334

A3967 easy driver

Micro switches Report to moderator QuoteQuick EditMore... Scampy001 avatar_Scampy001 * Newbie Posts: 3 Karma: 0 [add] 86.172.85.107

Re: Stepper motor attached with 3 micro switches to do 3 different commands

1

Jan 28, 2019, 09:06 pm This is the code. Can anyone help?

define RPMS 104.0

define STEP_PIN 9

define DIRECTION_PIN 8

define STEPS_PER_REV 400

define MICROSTEPS_PER_STEP 8

define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))

uint32_t LastStepTime = 0; uint32_t CurrentTime = 0;

const int onPin = 7; const int endPin = 6; const int homePin = 5; const int stepPin = 9; const int directionPin = 8; //goPin boolean lastButton = LOW; boolean motorOn = false;

void setup() { pinMode (onPin, INPUT); pinMode (endPin, INPUT); pinMode (homePin, INPUT); pinMode (stepPin, OUTPUT); pinMode (directionPin, OUTPUT); pinMode (stepPin, LOW); pinMode (directionPin, LOW); // goPin }

void loop() { // motor start if (digitalRead (onPin) == HIGH && lastButton == LOW) { CurrentTime = micros(); if ((CurrentTime - LastStepTime) > 1) { LastStepTime = CurrentTime; digitalWrite(STEP_PIN, HIGH); delayMicroseconds((100)); digitalWrite(STEP_PIN, LOW); delayMicroseconds((100)); } else

lastButton = digitalRead(onPin); } digitalWrite(onPin, motorOn);

// motor reverse { delay (1000); if (digitalRead (onPin) == HIGH && lastButton == LOW) CurrentTime = micros(); if ((CurrentTime - LastStepTime) > 1) { LastStepTime = CurrentTime; digitalWrite(STEP_PIN, LOW); delayMicroseconds((100)); digitalWrite(STEP_PIN, HIGH); delayMicroseconds((100)); } else { lastButton = digitalRead(onPin); } digitalWrite(onPin, motorOn);

// motor stop { if (digitalRead (homePin) == HIGH && lastButton == LOW); } } }

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code for easier reading.

The usual way to move to a limit switch is one step at a time, checking the switch between moves.

…R
Stepper Motor Basics
Simple Stepper Code

How are the microswitches connected to the input pins? Are they connected through the NO contacts to +5 volts and have no pulldown resistors?

For sure, these two lines need to be using "digital.Write"

pinMode (stepPin, LOW); pinMode (directionPin, LOW);

Paul