Created
May 11, 2015 17:05
-
-
Save decebals/b1201f3c24021bdae370 to your computer and use it in GitHub Desktop.
Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AccelStepper.h> | |
const int stepsPerRevolution = 50; // change this to fit the number of steps per revolution for your motor | |
const int cm = stepsPerRevolution * 4.935; // number of steps required to move 1cm | |
const int button1Pin = 10; | |
const int button2Pin = 11; | |
const int button3Pin = 12; | |
int limitSwitch = 1; | |
int button2State = 1; | |
int button3State = 1; | |
// see http://www.airspayce.com/mikem/arduino/AccelStepper/index.html | |
AccelStepper stepper(AccelStepper::FULL4WIRE,5,6,7,8); // defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5 | |
void setup() { | |
// serial settings | |
Serial.begin(9600); // set up Serial library at 9600 bps | |
Serial.println("Stepper test!"); | |
// motor settings | |
stepper.setMaxSpeed(220); | |
stepper.setAcceleration(1000); | |
stepper.setCurrentPosition(0); | |
pinMode(button1Pin, INPUT_PULLUP); | |
pinMode(button2Pin, INPUT_PULLUP); | |
pinMode(button3Pin, INPUT_PULLUP); | |
// attachInterrupt(0, limitStop, CHANGE); | |
} | |
void loop() { | |
limitSwitch = digitalRead(button1Pin); | |
Serial.println(limitSwitch); | |
if (limitSwitch == 0) { | |
return; | |
} | |
button2State = digitalRead(button2Pin); | |
Serial.println(button2State); | |
if (button2State == 0) { | |
// move foreward | |
stepper.moveTo(stepper.currentPosition() + 1); | |
} | |
button3State = digitalRead(button3Pin); | |
Serial.println(button3State); | |
if (button3State == 0) { | |
// move backward | |
stepper.moveTo(stepper.currentPosition() - 1); | |
} | |
stepper.run(); | |
} | |
/* | |
void limitStop() { | |
stepper.stop(); | |
stepper.setCurrentPosition (0); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, for limit switch may be you can use an attachinterrupt, it's easier than read all the time the sate of a pin, maybe with something like this.
I hope it helps!
Kind regards