MotorShield 2.3 controlling stepper motor speed using pot an

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
User avatar
175345
 
Posts: 2
Joined: Thu Apr 30, 2015 6:06 pm

MotorShield 2.3 controlling stepper motor speed using pot an

Post by 175345 »

Hello all..

I´m having some issues controlling my stepper motor speed.
The case is that I use a potmeter input and mapping it to 0-100.
I thought this could be put directly into the "setSpeed" function, but obviously I´m wrong.

The direction control of the stepper is working flawlessly.

Can any of you please have a look at my code?
Any input would be greatly appreciated.

PS: I´m sorry for all the comments in my code, I´m still trying to learn the syntax :)

BR

Code: Select all


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

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);




// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;                           // the number of the pushbutton pin
const int buttonPin2 = 3;                           // the number of the pushbutton pin
const int ledPin1 =  13;                            // the number of the LED pin
const int ledPin2 =  12;                            // the number of the LED pin
int sensorPin = A0;                                 // select the input pin for the potentiometer


// variables will change:
int buttonState1 = 0;                               // initial variable for reading the pushbutton status
int buttonState2 = 0;                               // intial variable for reading the pushbutton status
int sensorValue = 0;                                // variable to store the value coming from the sensor
int pot = 0;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
                                                   // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
                                                   // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  
}

void loop() {
  
                                                  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  sensorValue = analogRead(sensorPin); 
  pot = map(sensorValue, 0, 1023, 0, 101);        // read in the value from potmeter to sensorValue
  
  //myMotor->setSpeed(sensorValue);  // 10 rpm  
  
 if (pot > 0) {
    myMotor->setSpeed(pot);
     }
     
 else (pot = 0); {
    myMotor->setSpeed(0);
     }
                                                  // check if the pushbutton is pressed.
                                                  // if it is, the buttonState1 is HIGH:
 if (buttonState1 == HIGH) 
  {
    // turn LED on:
    //digitalWrite(ledPin1, HIGH);
    //Serial.print("Double coil steps FWD     sensorValue =");Serial.print(sensorValue); Serial.print("   Pot =");Serial.println(pot);
    myMotor->onestep(FORWARD, DOUBLE); 
  }
  
  
             //else {
                                                  // turn LED off:
             // digitalWrite(ledPin1, LOW);
             //}
 
 
 
                                                 // check if the pushbutton2 is pressed.
                                                 // if it is, the buttonState1 is HIGH:
 if (buttonState2 == HIGH) 
  {
    // turn LED on:
    //digitalWrite(ledPin2, HIGH);
    //Serial.print("Double coil steps BWD     sensorValue =");Serial.print(sensorValue); Serial.print("   Pot =");Serial.println(pot);
    myMotor->onestep(BACKWARD, DOUBLE); 
  }
  
  
             //else {
                                                // turn LED off:
             // digitalWrite(ledPin2, LOW);
             //}
 
 
 
 
 
 //Serial.println("Single coil steps");
  //myMotor->step(100, FORWARD, SINGLE); 
  //myMotor->step(100, BACKWARD, SINGLE); 

  //Serial.println("Double coil steps");
  //myMotor->step(100, FORWARD, DOUBLE); 
  //myMotor->step(100, BACKWARD, DOUBLE);
  
  //Serial.println("Interleave coil steps");
  //myMotor->step(100, FORWARD, INTERLEAVE); 
  //myMotor->step(100, BACKWARD, INTERLEAVE); 
  
  //Serial.println("Microstep steps");
  //myMotor->step(50, FORWARD, MICROSTEP); 
  //myMotor->step(50, BACKWARD, MICROSTEP);
}

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

Re: MotorShield 2.3 controlling stepper motor speed using po

Post by adafruit_support_bill »

Using onestep(), you are bypassing the speed setting for the motor.
You can add a delay after onestep to control the speed, something like this:

Code: Select all

   myMotor->onestep(FORWARD, DOUBLE); 
   delay(pot);  // the delay between steps is proportional to the pot reading

User avatar
175345
 
Posts: 2
Joined: Thu Apr 30, 2015 6:06 pm

Re: MotorShield 2.3 controlling stepper motor speed using po

Post by 175345 »

@adafruit_support_bill

Thank you so much for the swift reply.

Your tip worked just fine, but as I am craving more understanding, I would like to know more about the setSpeed, onestep, and the other functions for the library.

Do you have any information or learning portals you can supply?

Again, than you very much for your reply.

BR

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

Re: MotorShield 2.3 controlling stepper motor speed using po

Post by adafruit_support_bill »

The Library Reference is part of the tutorial. Here is a link to the stepper motor section:
https://learn.adafruit.com/adafruit-mot ... eppermotor

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

Return to “Arduino Shields from Adafruit”