Controlling Stepper Motor

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
jfrench
 
Posts: 7
Joined: Tue May 20, 2014 5:20 pm

Controlling Stepper Motor

Post by jfrench »

Hi Everyone,

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();
  }   
}
Last edited by adafruit_support_bill on Thu May 22, 2014 6:42 am, edited 1 time in total.
Reason: please use the code button when submitting code. press [Code], then paste your code between the [code] [/code] tags.

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: Controlling Stepper Motor

Post by Franklin97355 »

As far as Im aware this should not happen because if there is no switch attached to an input it should read 0.
This is a common misconception. Analog inputs, when not connected can "float" Depending on how your switches are wired you need to pull the input either to ground or 5v You can also set the internal pullup using this feature of pinMode
Pins Configured as INPUT_PULLUP

The Atmega chip on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-down resistors, you can use the INPUT_PULLUP argument in pinMode(). This effectively inverts the behavior, where HIGH means the sensor is off, and LOW means the sensor is on. See the Input Pullup Serial tutorial for an example of this in use.

jfrench
 
Posts: 7
Joined: Tue May 20, 2014 5:20 pm

Re: Controlling Stepper Motor

Post by jfrench »

I think your right about the floating input but it is still not behaving how I expected it to so I have stripped the code back to just operate a motor in one direction with a switch. However now no matter what pin I select as my button input it only works with the 0 pin. what do you think?


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

const int buttonPin1 = 1; // the number of the pushbutton pin

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *Motor1 = AFMS.getStepper(200, 2);

int buttonState1 = 0; // variable for reading the pushbutton status
int stop = false;

void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT_PULLUP);
// 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
}


void loop(){

while (digitalRead(buttonState1) == !HIGH){
Motor1->step(1, FORWARD, DOUBLE);
}
if (digitalRead(buttonState1) == !LOW){
Motor1->release();
}
}

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Stepper Motor

Post by adafruit_support_bill »

What kind of Arduino is it? With most Arduinos, you can't use Pin 1 if you are using the serial port. Try a different pin for your button.

jfrench
 
Posts: 7
Joined: Tue May 20, 2014 5:20 pm

Re: Controlling Stepper Motor

Post by jfrench »

It is an Uno rev3 but it doesn't seem to make a difference which pin I specify in the code, it only works when I connect the 0 pin to ground, regardless of what the code says

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Stepper Motor

Post by adafruit_support_bill »

Post some photos showing your soldering and connections.

jfrench
 
Posts: 7
Joined: Tue May 20, 2014 5:20 pm

Re: Controlling Stepper Motor

Post by jfrench »

I think the soldering looks ok, see what you think. I have been trying to avoid soldering to the motor-shield until I know the final configuration of it so to emulate a switch I have been making contact with a wire between ground and the specified pin.

I've got some of the example codes working, before trying this.
Attachments
photo 2.JPG
photo 2.JPG (507.21 KiB) Viewed 285 times
photo 4 (2).JPG
photo 4 (2).JPG (492.25 KiB) Viewed 285 times
photo (6).JPG
photo (6).JPG (521.38 KiB) Viewed 285 times

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Stepper Motor

Post by adafruit_support_bill »

it only works when I connect the 0 pin to ground, regardless of what the code says
But that is exactly what the code says:

Code: Select all

const int buttonPin1 = 1; // the number of the pushbutton pin

...

int buttonState1 = 0; // variable for reading the pushbutton status

...

void loop(){

while (digitalRead(buttonState1) == !HIGH){
Motor1->step(1, FORWARD, DOUBLE);
} 
if (digitalRead(buttonState1) == !LOW){
Motor1->release();
} 
}
I think you mean:

Code: Select all

void loop(){

while (digitalRead(buttonPin1 ) == !HIGH){
Motor1->step(1, FORWARD, DOUBLE);
} 
if (digitalRead(buttonPin1 ) == !LOW){
Motor1->release();
} 
}

jfrench
 
Posts: 7
Joined: Tue May 20, 2014 5:20 pm

Re: Controlling Stepper Motor

Post by jfrench »

Your right, I have the code working now.

I did not realise you could use digitalread directly on a pin, so would you only use variables for information that is not collected by the I/O pins?

I am curious as the next stage for this project is to set up a wifi connection and control the motors from a phone.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Controlling Stepper Motor

Post by adafruit_support_bill »

I did not realise you could use digitalread directly on a pin, so would you only use variables for information that is not collected by the I/O pins?
But buttonPin1 is just a constant that identifies a pin number. It is not a physical pin.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”