accelstepper homing and button control.

2,634 views
Skip to first unread message

Ash P

unread,
Sep 23, 2013, 7:55:35 AM9/23/13
to accels...@googlegroups.com
hello, im new to arduino and c programing 6
 
im looking at making a robotic arm I can control from a hacked playstation controller (im just using the contacts as normal pushbuttons)
 
so my plan is to have 3 stepper motors creating 3 axis of the arm (up and down, left and right and twist)
 
I have an idea of what is required to make this work I am just unsure how to write it using c and accelstepper library.
 
when I power up I want to run a homing sequence which would move up and down axis all the way down till I hit a home switch, left and right would be centred by hitting a switch and the twist would also be centred by hitting a switch.
 
from here I want to be able to drive the up and down stepper up using a up button and back down using the down button, and left and right using left and right buttons, and twist using 2 more buttons.
 
 
I need to limit the movements of each axis as they have physical limits so I need it to stop electrically before crashing. (I assume I can keep track of the amount of steps somehow)
 
eg. upanddown axis once homed in its lowest position can only move a maximum of 1/4 of the steps required to do 1 revolution.
 
can I get some help with how to use a home limit switch to set an initial position then allow me to only move a set number of steps forward? 
 
I've played with a bit of code so far and have come up with this which only works with 1 stepper and 1 direction. it aint pretty and im sure I aint doing it the best way but its working, however I haven't worked out how I can drive the other direction with another button.
 
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1,3,4); //1 means a stepper driver (with Step and Direction pins) step pin = 3 dir pin = 4
const int buttonPin = 5;     // the number of the pushbutton pin.
const int ledPin =  13;      // the number of the LED pin.
int buttonState = 0;         // variable for reading the pushbutton status.
void setup()

  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
  pinMode(ledPin, OUTPUT);      // initialize the LED pin as an output.
  
  pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input.
}
void loop()
{buttonState = digitalRead(buttonPin);    // read the state of the pushbutton value.
  
  
   if (buttonState == HIGH) {     // check if the pushbutton is pressed.
        
     digitalWrite(ledPin, HIGH);  // turn LED on.
     
    stepper.move(-100);
    stepper.run();
   }
   else {
     // turn LED off:
     digitalWrite(ledPin, LOW);
   }
}
 
it aint pretty and im sure I aint doing it the best way but its working, however I haven't worked out how I can drive the other direction with another button. I tried this but I think I am reading the buttons wrong.
 
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1,3,4);              //1 means a stepper driver (with Step and Direction pins) step pin = 3 dir pin = 4
const int forward = 5;                    // the number of the forward pin.
const int backward = 6;                   // the number of the backward pin.
const int ledPin =  13;                   // the number of the LED pin.
int forwardState = 0;                     // variable for reading the forwards button status.
int backwardState = 0;                    //variable for reading the backwards button status.
void setup()

  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(4000);
  stepper.setAcceleration(1000);
  pinMode(ledPin, OUTPUT);     // initialize the LED pin as an output.
  pinMode(forward, INPUT);     // initialize the forward button pin as an input.
  pinMode(backward, INPUT);    // initialize the backward button pin as an input.
}
void loop()
{forwardState = digitalRead(forward);    // read the state of the forward pushbutton value.
  
   if (forwardState == HIGH) {     // check if the forward push button is pressed.
     digitalWrite(ledPin, HIGH);   // turn LED on.
     stepper.move(20);
     stepper.run();
   }
   else {
     digitalWrite(ledPin, LOW);    // turn LED off.
   }
 

{backwardState = digitalRead(backward);  // read the state of the backward pushbutton value
  
  
   if (backwardState == HIGH) {    // check if the backward push button is pressed.
     stepper.move(-20);
     stepper.run();
   }
  }
}
  
 

 

Mike McCauley

unread,
Sep 24, 2013, 7:25:54 PM9/24/13
to accels...@googlegroups.com
Hello,

Your coding strategy for using AccelStepper in incorrect.

The way you should do this is to make sure you call stepper.run() in the main
loop, and then change the target positions only when some external event (like
a button press) occurs:

pseudocode:

void loop()
{
if (an external button is pressed)
{
change stepper target
}}

stepper.run()
}

Its stepper.run that moves the stepper when needed, and it must be called as
often as possible.

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Reply all
Reply to author
Forward
0 new messages