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);
}