Go Down

Topic: Prompt user for Stepper motor action (Read 677 times) previous topic - next topic

Aaronkoe

I want to prompt the user for stepper action. i am using an Adafruit motor shield and the subsequent libraries. I think the code is right but I'm having trouble calling the user input as a parameter inside the function step() command

here is more info :

void step(uint16_t steps, uint8_t dir, uint8_t style = SINGLE);
The step() function controls stepper motion.
The first parameter specifies how many steps to move.
The second parameter specifies the direction: FORWARD or BACKWARD
The last parameter specifies the stepping style: SINGLE, DOUBLE, INTERLEAVED or MICROSTEP

code:

Code: [Select]
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2
---->  http://www.adafruit.com/products/1438
*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_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 *stepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *stepper2 = AFMS.getStepper(200, 2);

int Orientation;
int Distance;
int Speed;
 
void setup() {
  Serial.begin(9600);                           // set up Serial library at 9600 bps
  AFMS.begin();                                // create with the default frequency 1.6KHz
                                             //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
String Welcome="Welcome to my Program!";     //Welcome Message
  Serial.println(Welcome);
  Serial.println(" ");
  delay(3000);                              //Wait 3 Seconds to Start
 
  stepper1->setSpeed(100);                 // 100 rpm   
  stepper2->setSpeed(100);                 // 100 rpm   
}

void loop() {
 
  Serial.println("FORWARD or BACKWARD?");         //Orientation
  while (Serial.available()==0) { }              //Wait for user input
      Orientation=Serial.parseInt();            //Assign to Orientation

 Serial.println("How many steps?");        //Ask user for distance
 while (Serial.available()==0) { }              //Wait for user input
      Distance=Serial.parseInt();               //Assign to Distance

 Serial.println("MICROSTEP or DOUBLE?");        //Ask user for speed
 while (Serial.available()==0) { }              //Wait for user input
      Speed=Serial.parseInt();                  //Assign to Speed
   
  stepper1->step(Distance, Orientation, Speed);

}


Thank you in advance!!!

Aaronkoe

I added the user prompts as pointers, which seems to get some action from the stepper, but its does the same thing every time no matter what the user inputs.

Code: [Select]
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2
---->  http://www.adafruit.com/products/1438
*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_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 *stepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *stepper2 = AFMS.getStepper(200, 2);

int Orientation;
float Distance;
int Speed;
 
void setup() {
  Serial.begin(9600);                           // set up Serial library at 9600 bps
  AFMS.begin();                                // create with the default frequency 1.6KHz
                                             //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
String Welcome="Welcome to my Program!";     //Welcome Message
  Serial.println(Welcome);
  Serial.println(" ");
  delay(1000);                              //Wait 3 Seconds to Start
 
  stepper1->setSpeed(10000);                 // 100 rpm   
  stepper2->setSpeed(10000);                 // 100 rpm   
}

void loop() {
 
  Serial.println("FORWARD or BACKWARD?");         //Orientation
  while (Serial.available()==0) { }              //Wait for user input
      Orientation=Serial.parseInt();            //Assign to Orientation

 Serial.println("How many steps?");        //Ask user for distance
 while (Serial.available()==0) { }              //Wait for user input
      Distance=Serial.parseInt();               //Assign to Distance

 Serial.println("MICROSTEP or DOUBLE?");        //Ask user for speed
 while (Serial.available()==0) { }              //Wait for user input
      Speed=Serial.parseInt();                  //Assign to Speed
   
  stepper1->step(&Distance, &Orientation, &Speed);

}

756E6C

What do you enter for "orientation"?, Try a sane speed like 30 first.

Robin2

Effective user input needs much more sophisticated handling. Receive the value. Check if it is in a sensible range. If not warn the user. If it is an OK value show it to the user and get him to confirm it. Then show him all three values for final confirmation.

It is very much easier to do that stuff in a program on a PC which will only send the validated values to the Arduino.

There is a simple example of Arduino user input in Planning and Implementing a Program (without any of the confirmation or checking).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

Code: [Select]
  stepper1->step(&Distance, &Orientation, &Speed);
Why are you passing the addresses of the variables?

The step() method wants values, not addresses.
The art of getting good answers lies in asking good questions.

Aaronkoe

Yes the step() command does want values. What is the proper reference to input the users values into my step() command?

Thank you

PaulS

Quote
What is the proper reference to input the users values into my step() command?
Get rid of the address-of operators (&).
The art of getting good answers lies in asking good questions.

Go Up