Im fairly new to arduinos and have bought myself an adafruit motorshield to work two stepper motors in a prototype for my dissertation that needs finishing ASAP.
I am trying to control two stepper motors in both directions using 4 of switches, one for each direction. However when I upload the code with no switches attached motor 2 spins. As far as Im aware this should not happen because if there is no switch attached to an input it should read 0.
I'm pretty sure hardware is ok. Also there is a limit switch on both motors labeled "panlimitPin" and "tiltlimitPin" to limit the rotation, I have not had had a chance to test this but have included them in case they are part of the problem. It is meant to move the stepper in the opposite direction for two steps and end loop when pressed.
code:
***********************************
Code: Select all
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
const int buttonPin1 = 1; // the number of the pushbutton pin
const int buttonPin2 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3; // the number of the pushbutton pin
const int buttonPin4 = 4; // the number of the pushbutton pin
const int panlimitPin = 5;
const int tiltlimitPin = 6;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *Motor2 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *Motor1 = AFMS.getStepper(200, 2);
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
int buttonState4 = 0; // variable for reading the pushbutton status
int panlimitState = 0;
int tiltlimitState = 0;
int stop = false;
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(panlimitPin, INPUT);
pinMode(tiltlimitPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
Motor1->setSpeed(40); // 10 rpm
Motor2->setSpeed(40); // 10 rpm
}
void loop(){
while (digitalRead(buttonState1) == HIGH && !stop){
Motor1->step(1, FORWARD, DOUBLE);
if (digitalRead(panlimitPin) == HIGH){
Motor1->step(2, BACKWARD, DOUBLE);
Motor1->release();
}
}
if (digitalRead(buttonState1) == LOW){
stop = true;
Motor1->release();
}
while (digitalRead(buttonState2) == HIGH && !stop){
Motor1->step(1, BACKWARD, DOUBLE);
if (digitalRead(panlimitPin) == HIGH){
Motor1->step(2, FORWARD, DOUBLE);
Motor1->release();
}
}
if (digitalRead(buttonState2) == LOW){
stop = true;
Motor1->release();
}
while (digitalRead(buttonState3) == HIGH && !stop){
Motor2->step(1, FORWARD, DOUBLE);
if (digitalRead(panlimitPin) == HIGH){
Motor2->step(2, BACKWARD, DOUBLE);
Motor2->release();
}
}
if (digitalRead(buttonState3) == LOW){
stop = true;
Motor2->release();
}
while (digitalRead(buttonState4) == HIGH && !stop){
Motor2->step(1, BACKWARD, DOUBLE);
if (digitalRead(panlimitPin) == HIGH){
Motor2->step(2, FORWARD, DOUBLE);
Motor2->release();
}
}
if (digitalRead(buttonState4) == LOW){
stop = true;
Motor2->release();
}
}