#include <Adafruit_MotorShield.h>
#include <Wire.h>
int speedvalue = 0;
int givendispvalue = 0;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Motor port #2 (M3 and M4) or #1 (M1 and M2)
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 2);
void setup()
{
Serial.begin(115200); // set up Serial library at 115200 bps
AFMS.begin();
myStepper->setSpeed(speedvalue);
}
void loop()
{
while (Serial.available()) { //Only runs when serial input is available
speedvalue = int(Serial.parseInt());
}
myStepper->setSpeed(speedvalue);
if (speedvalue > 0){
myStepper->step(10, FORWARD, SINGLE);
} else if (speedvalue < 0 ){
myStepper->step(10,BACKWARD, SINGLE);
} else {
myStepper->release();
}
}
When I switch steps to micro there is no change in RPM, double follows the same trend as single, but different RPM values, interleave seems to be working ok.
Not using those libraries I post some suggestions.
Dig down in the documentation and find out about the limits of the stepper motor as well as the library range of speed.
Posting schematics could be valuable. What voltage are You applying to the stepper driver?
Myself, I'm fighting a stepper moving more than it should.....
Read this quote from the driver board: Instead of using a latch and the Arduino's PWM pins, we have a fully-dedicated PWM driver chip onboard. This chip handles all the motor and speed controls over I2C. Only two data pins (SDA & SCL in addition to the power pins GND & 5V) are required
Is that library using I2C to command the driver or is it using PWM?
Schematics please.
Railroader: The stepper motor can handle 12V and 350mA/phase, as far as I can tell (I'm fairly inexperienced) the library doesn't seem to limit the range. The motorshield has an input of 12V, schematic of the motorshield is attached(is this what you were looking for?). In this setup, it should be using I2C I think, but there is a way to PWM the stepper and drive it that way.
Paul: I am using a hall effect sensor and magnet to measure RPM. The standalone code:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Create the motor shield object with the default I2C address
const byte hallsensor = 2; //The Hall effect sensor (HES) output connected to pin no 7 of Arduino due
volatile byte revolutions; //To measure RPM
unsigned long timeold; //To measure RPM
float measuredrpm; //RPM value measured from HES
void setup() {
Serial.begin(115200); //Enables Serial Port
timeold = 0; //Sets time storage to zero
revolutions = 0; //Set revolution count to zero
AFMS.begin(); //Create with the default frequency 1.6KHz
Wire.begin(); //Enables I2C Port
}
void loop () {
RPM();
}
void isr() {
revolutions++; //Each rotation, this interrupt function is run twice
}
void RPM(){
attachInterrupt(digitalPinToInterrupt(hallsensor), isr, FALLING); //Attaches interupt to HES signal
pinMode(hallsensor, INPUT); //Recieve data from HES
while (revolutions >= 1) { //Calculation for the RPM measured by HES
measuredrpm = abs((float)60*1000/(millis() - timeold)*revolutions);
timeold = millis();
Serial.println(measuredrpm);
revolutions = 0;
}
}
The RPM measurements are accurate, I've been verifying them by calculating by hand and have used this code on a few other project and haven't had any issues with it so far.
Thanks for both of your responses. I'm thinking I will just have to stay above 20 and then just the interleave setting if I need to go slower.
Looking at your code, I do not see where you tell the software to move the motor more than just a few steps. If you want to motor to turn a full turn, you need to tell it that many steps to take.
What is the purpose of using a stepper if you need to treat it like a regular DC motor?
For the PWM, changing out SINGLE with MICROSTEP will PWM the coils according to Adafruit's guide on the motor shield.
Eventually I will be pulsing the motor forwards and backwards and need better control over the distance traveled than what a DC motor can provide (I am trying to measure piezoelectric properties of PVDF nanofibers), but for now I am just troubleshooting some simplified code.