Hello all,
I am using an arduino uno, with an adafruit motor shield v1.2, (2) 200 step bi polar stepper motors (http://www.pololu.com/catalog/product/1209) and 4 momentary push buttons with leds (http://www.exp-tech.de/accessories/Button-Switch/Waterproof-Metal-Pushbutton-with-White-LED-Ring-16mm-White-Momentary.html)
I am trying to control 2 stepper motors with 4 different push buttons. Currently, i have written a code that will spin both motors simultaneously (or alternating one step at a time) a certain amount of steps when each button is pressed (code below). However, i need the position the motors spin to, to be more precise. The motors will be attached to a model i am making for an architecture firm, where a smaller ring spins inside of a larger ring in opposite directions, at the same time. There are 4 main positions in which i would like to have the rings move to by pressing a corresponding button. If i were to always press the buttons in order (first button one, then button two, etc) i could set the stepper motors to move a certain number of steps to reach the next position correctly. However, if one would press button 2 and then button 4, the rings won't reach the correct position and from there everything is a mess. I am wondering if there is a way one can set the stepper motor to a specific step so when the button is pressed it returns to that step, or if the buttons can be coded in a way where it recognises the last button that was pressed, and then moves accordingly to the next correct position.
Any and all help is much appreciated. Thanks in advance!!
#include <AccelStepper.h>
#include <AFMotor.h>
const int buttonPin1 = 9;
const int buttonPin2 = 10;
const int buttonPin3 = 13;
const int buttonPin4 = 2;
const int ledPin1 = A1;
const int ledPin2 = A2;
const int ledPin3 = A3;
const int ledPin4 = A4;
AF_Stepper motor1(200,1);
AF_Stepper motor2(200,2);
int x = 0;
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the serial port:
Serial.begin(9600);
motor1.setSpeed(300);
motor2.setSpeed(300);
}
void loop()
{
if (digitalRead(buttonPin1) == HIGH)
{
for(int x = 0; x < 200; x++)
{
motor1.step(1,FORWARD,SINGLE);
digitalWrite(ledPin1, HIGH);
motor2.step(1,BACKWARD,SINGLE);
}
}
else if (digitalRead(buttonPin2) == HIGH)
{
for(int x = 0; x < 400; x++)
{
motor1.step(1,FORWARD,SINGLE);
digitalWrite(ledPin2, HIGH);
motor2.step(1,BACKWARD,SINGLE);
}
}
else if (digitalRead(buttonPin3) == HIGH)
{
for(int x = 0; x < 600; x++)
{
motor1.step(1,FORWARD,SINGLE);
digitalWrite(ledPin3, HIGH);
motor2.step(1,BACKWARD,SINGLE);
}
}
else if (digitalRead(buttonPin4) == HIGH)
{
for(int x = 0; x < 800; x++)
{
motor1.step(1,FORWARD,SINGLE);
digitalWrite(ledPin4, HIGH);
motor2.step(1,BACKWARD,SINGLE);
}
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}