Pages: [1]   Go Down
Author Topic: Rewersing directionof stepper motor with limit switch  (Read 1427 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody,
I am very new in this field,
can somebody help me with a sample code how to reverse direction of a stepper motor  when 5v is applied through limit switch?


I have managed to switch of an on LED, with switch, but Stepper are out of my range.

Thanks In advance
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is your stepper wired up?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is 6  wire unipolar motor,
Controlled over arduino uno,

And i am trying to use accel stepper library
« Last Edit: February 20, 2013, 11:43:16 am by websonic » Logged

0
Online Online
Shannon Member
****
Karma: 162
Posts: 10515
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Existing code?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hardly any,

I want to use random example from accelstepper

any ideas hot to incorporate two limit switches which would only reverse reverse direction if activated

Thanks in advance

// Random.pde
// -*- mode: C++ -*-
//
// Make a single stepper perform random changes in speed, position and acceleration
//
// Copyright (C) 2009 Mike McCauley
// $Id: Random.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; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()

}

void loop()
{
    if (stepper.distanceToGo() == 0)
    {
   // Random change to speed, position and acceleration
   // Make sure we dont get 0 speed or accelerations
   delay(1000);
   stepper.moveTo(rand() % 200);
   stepper.setMaxSpeed((rand() % 200) + 1);
   stepper.setAcceleration((rand() % 200) + 1);
    }
    stepper.run();
}
Logged

United States
Offline Offline
Newbie
*
Karma: 2
Posts: 49
Yes. Thats my donkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

top of file add: (replace A0 & A1 with your selection of pins.)
#define switchPinA A0
#define switchPinB A1

Add to void setup()

  pinMode(switchPinA,INPUT);
  pinMode(switchPinB,INPUT);
  digitalWrite(switchPinA,HIGH);
  digitalWrite(switchPinB,HIGH);

in void loop() replace:
//stepper.moveTo(rand() % 200);
*with*
long someRandomPosition=rand() % 200;
if(digitalRead(switchPinA)==HIGH || digitalRead(switchPinB)==HIGH)
{
  someRandomPosition=someRandomPosition * -1; // just invert data
}
stepper.moveTo(someRandomPosition);



PS: According to  if (stepper.distanceToGo() == 0)
it will change direction based on switches, ONLY after it completes the previous move.

If you wish to have it switch directions instantly, then you just need to put the blue code outside of the if (stepper.distanceToGo() == 0) statement.
« Last Edit: February 22, 2013, 11:28:29 pm by dewy721 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Dewy,

Thanks for advice and your time.

I am still having trouble

when I include long someRandomPosition=rand() % 200;
Stepper just gets blocked.

With out that change motor always runs in one direction.

Pls take >look at the code, What I have done wrong?

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::FULL4WIRE, 8, 9, 10,11);

#define switchPinA 2
#define switchPinB 3

void setup()

  pinMode(switchPinA,INPUT);
  pinMode(switchPinB,INPUT);
  digitalWrite(switchPinA,HIGH);
  digitalWrite(switchPinB,HIGH);
}

void loop()
{
    if (stepper.distanceToGo() == 0)
    {
   // Random change to speed, position and acceleration
   // Make sure we dont get 0 speed or accelerations
   delay(1000);
   long someRandomPosition=rand() % 200;
   stepper.setMaxSpeed((rand() % 200) + 1);
   stepper.setAcceleration((rand() % 200) + 1);
    if(digitalRead(switchPinA)==HIGH || digitalRead(switchPinB)==HIGH)
{
        //someRandomPosition=someRandomPosition * -1; // just invert data
}
        stepper.moveTo(-1);
    }
    stepper.run();
}
Logged

United States
Offline Offline
Newbie
*
Karma: 2
Posts: 49
Yes. Thats my donkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this instead:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::FULL4WIRE, 8, 9, 10,11);

#define switchPinA 2
#define switchPinB 3
#define lowestRandomNum -100
#define highestRandomNum 100

void setup()
{
  pinMode(switchPinA,INPUT);
  pinMode(switchPinB,INPUT);
  digitalWrite(switchPinA,HIGH);
  digitalWrite(switchPinB,HIGH);
}

void loop()
{
   if (stepper.distanceToGo() == 0)
   {
     // Random change to speed, position and acceleration
     // Make sure we dont get 0 speed or accelerations
     delay(1000);
     long someRandomPosition=random(lowestRandomNum, highestRandomNum);
     stepper.setMaxSpeed(random(0, highestRandomNum) + 1);
     stepper.setAcceleration(random(1, highestRandomNum) + 1);
     if(digitalRead(switchPinA)==HIGH || digitalRead(switchPinB)==HIGH)
     {
       someRandomPosition=someRandomPosition * -1; // just invert data
     }
     stepper.moveTo(someRandomPosition);
   }
   stepper.run();
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Dewy,

This time it is working smiley thanks, thanks ,thanks smiley

If you do have more time for me,

1) I do not think that when limit switch is activated motor changes direction straight away.
2) and I would greatly appreciated if you can advise me how to set minimum speed in random behavior.

I have checked limit switches with simple sketch to activate 2 LEDs, and I am sure that part is ok.

Thanks in advance
Logged

United States
Offline Offline
Newbie
*
Karma: 2
Posts: 49
Yes. Thats my donkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok for that, let's take a look at the AccelStepper command reference:

First, lets fix the brakes:
From:
    Quickstop.pde.

We'll need to add the use of:
stop() "Sets a new target position that causes the stepper to stop as quickly as possible..."
runToPosition() "Moves the motor at the currently selected constant speed (forward or reverse) to the target position..."

Now lets rev up the minimum speed:
setMaxSpeed() "Sets the maximum permitted speed."

void loop()
{
   if (stepper.distanceToGo() == 0)
   {
     // Random change to speed, position and acceleration
     // Make sure we dont get 0 speed or accelerations
     delay(1000);
     long someRandomPosition=random(lowestRandomNum, highestRandomNum);
     stepper.setMaxSpeed(random(10, highestRandomNum) + 1);
     stepper.setAcceleration(random(1, highestRandomNum) + 1);
     if(digitalRead(switchPinA)==HIGH || digitalRead(switchPinB)==HIGH)
     {
       someRandomPosition=someRandomPosition * -1; // just invert data
       stepper.stop(); // Stop as fast as possible: sets new target
       stepper.runToPosition();
     }
     stepper.moveTo(someRandomPosition);
   }
   stepper.run();
}

Ok, that's my limit of two freebies per customer. Its your turn to hack at it. smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

God Bless  my friend smiley

I am greatfull and for this much
Logged

Pages: [1]   Go Up
Jump to: