Hey all. Here is the current project im working. The goal is to control the rotational position of a shaft using a stepper motor and a 10 turn 10k ohm potentiometer. I have a program that will allow me to control the position of the motor based on my potentiometer input and one that will allow me to input from the serial monitor and rotate so many steps. What i would like to do is combine these two and have an input from the serial monitor that will put the stepper motor to a specific resistance from my potentiometer. Here are the two programs i am trying to combine with no luck any help would be appreciated. Thanks in advance.
Code One: Controlling the stepper based on potentiometer input
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
int analogPin = A0;
int val = 0;
int prepos = 0;
int pos = 0;
byte byteRead;
void setup()
{
AFMS.begin();
myMotor->setSpeed(20);
pinMode(analogPin, INPUT);
Serial.begin(9600);
Serial.println("Rotational Position Calculator");
}
void loop() {
if (Serial.available()) {
byteRead = Serial.read();
if(byteRead==44){
Serial.println();
}else{
Serial.write(byteRead);
}}
prepos = pos;
val = analogRead(analogPin);
pos = map (val, 0, 1023, 0, 200);
float angle = map(val, 0, 1023, 0, 360);
if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
myMotor->step(-diststep, BACKWARD, SINGLE);
}
if (diststep > 0) {
myMotor->step(diststep, FORWARD, SINGLE);
}
Serial.print("Current Position = ");
Serial.println(val);
}
}
Code Two; Controlling Stepper Motor from the Serial Monitor
#include <Wire.h> /* Includes the libaries needed to perform the functions */
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2); /* Indicates the stepper motor location on the board
gives the number of steps per rotation (Steps, Location)*/
int analogPin = A0;
int val = 0;
int prepos = 0;
byte byteRead;
void setup()
{
AFMS.begin();
myMotor->setSpeed(200);
pinMode(analogPin, INPUT);
Serial.begin(9600);
Serial.println(" Select Vial");
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch == 'a' || ch == 'A')
{
myMotor->step(150, FORWARD, SINGLE);
Serial.println("Vial A");
delay(2000);
}
if (ch == 'b'|| ch == 'B')
{
myMotor->step(400, BACKWARD, SINGLE);
Serial.println("Vial B");
delay(2000);
}
if (ch == 'c' || ch == 'C')
{
myMotor->step(250, FORWARD, SINGLE);
Serial.println("Vial C");
delay(2000);
}
if (ch != 'C' && ch != 'c' && ch != 'a' && ch != 'A' && ch != 'B' && ch != 'b')
{
Serial.println("Not Valid Selection");
}
delay(2000);
Serial.println("Vial in Tray Make Next Selection or Press Eject Tray");
}
}
I have a program that will allow me to control the position of the motor based on my potentiometer input and one that will allow me to input from the serial monitor and rotate so many steps.
SITREP :? (Mil-Speak for SITUATION REPORT)
Do you have the hardware setup and running ?
Have you done any testing ?
The goal is to control the rotational position of a shaft using a stepper motor and a 10 turn 10k ohm potentiometer
Do you know how to use stepper motors. (more importantly do you know how to wire them up ?
Do you have a motor driver ?
Do you know how to connect the motor to the motor driver ?
You obviously have already chosen a library so that's out of the way. (can't tell if it installed yet....)
Are you aware of the electrical parameters (rating ) of the motor shield ?
What kind of Power Supply were you planning to use ?( for the motors that is)
What i would like to do is combine these two and have an input from the serial monitor that will put the stepper motor to a specific resistance from my potentiometer
How were you planning to wire the pot ?
The code looks ok so far. Where did you get it ?
Get rid of the delay()s. You won't be able to read any inputs whilst the delays are in use. See the blink without delay example in the IDE and also look at Finite State Machine to overcome the problem.
This is roughly how I would do it. I don't have the AdaFruit library so I can't compile it. There may be mistakes. Perhaps you can learn from it.
One thing to watch out for: if myMotor->step() returns before the stepping is done things will go wrong. The next time through the loop more steps will be added to get to the target position and may overshoot. To fix that you must put in code to wait for the stepping to complete before acting on the analog input. Something like:
if (posStep != targetStep && !myMotor.busy()) {
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
/* 200 step per revolution stepper on connector 2 */
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
const int analogPin = A0;
const int TotalSteps = 2000; // Ten revolutions
void setup()
{
AFMS.begin();
myMotor->setSpeed(200);
Serial.begin(9600);
Serial.println("Rotational Position Calculator");
Serial.println(" Select Vial");
}
void loop() {
static unsigned targetStep = 0;
if (Serial.available()) {
// Pick a position
int byteRead = Serial.read();
// Digits from 0 to 9 select positions from 0% to 99%
if (byteRead >= '0' && byteRead <= '9') {
int percent = (byteRead - '0') * 11;
// Convert from percent full range to step number
// Ten revolutions at 200 steps per revolution
targetStep = TotalSteps * percent / 100;
}
}
// Get the current position
unsigned int pos = analogRead(analogPin);
// Convert from analog input to step number
// Ten revolutions at 200 steps per revolution
int posStep = TotalSteps * pos / 1024;
if (posStep != targetStep) {
Serial.print("Position = ");
Serial.print(posStep);
Serial.print(", Target = ");
Serial.println(targetStep);
int diststep = posStep - targetStep; // Ammount the analog input needs to change
if (diststep < 0) {
myMotor->step(-diststep, BACKWARD, SINGLE);
}
if (diststep > 0) {
myMotor->step(diststep, FORWARD, SINGLE);
}
}
}