Hi, I am currently making a Bluetooth car using Nema 17 stepper motors as the wheel driver and am trying to figure out how to use the AccelStepper library to move all the wheels at a set speed to a set position or set number of rotations. Everything works great in the code except for the stuff under the moveTest() subfunction. Thats the function I am trying to set up to move the car to a set position, but thought I'd upload the entire thing to give you a better idea. Thank you so much for your help!
//Wheel Numbers
/* _______________
___ | F | ___
| | | | | |
|TLW|--| |--|TRW|
|___| | | |___|
| |
| |
___ | | ___
| | | | | |
|BLW|--| |--|BRW|
|___| | | |___|
|_______________|
*/
#include <SoftwareSerial.h>
#include <ArduinoBlue.h>
#include <AccelStepper.h>
const int bluetoothTX = 8;
const int bluetoothRX = 7;
int prevThrottle = 49;
int prevSteering = 49;
int throttle, throttleSpeed, steering, sliderVal, button, sliderId;
int wheelSpeed = 500;
// Define the stepper motors and the pins the will use
AccelStepper topRightWheel(1, 3, 4); // (Type:driver(1 is default driver), STEP, DIR) - Stepper1
AccelStepper topLeftWheel(1, 5, 6); // Stepper2
AccelStepper backRightWheel(1, 9, 10); // Stepper3
AccelStepper backLeftWheel(1, 11, 12); // Stepper4
SoftwareSerial bluetooth(bluetoothTX, bluetoothRX);
ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor.
// Setup code runs once after program starts.
void setup() {
// Start serial monitor at 9600 bps.
Serial.begin(9600);
// Start bluetooth serial at 9600 bps.
bluetooth.begin(9600);
// delay just in case bluetooth module needs time to get ready.
delay(100);
Serial.println("Setup Complete");
//Set parameters for stepper motor speed
topLeftWheel.setMaxSpeed(3000);
topRightWheel.setMaxSpeed(3000);
backLeftWheel.setMaxSpeed(3000);
backRightWheel.setMaxSpeed(3000);
}
// Put your main code here, to run repeatedly:
void loop() {
// ID of the button pressed pressed.
button = phone.getButton();
// Returns the text data sent from the phone.
// After it returns the latest data, empty string "" is sent in subsequent.
// calls until text data is sent again.
String str = phone.getText();
// Throttle and steering values go from 0 to 99.
// When throttle and steering values are at 99/2 = 49, the joystick is at center.
throttle = phone.getThrottle() - 49;
steering = phone.getSteering() - 49;
// ID of the slider moved.
sliderId = phone.getSliderId();
// Slider value goes from 0 to 200.
sliderVal = phone.getSliderVal() - 100;
// Display button data whenever its pressed.
if (button != -1) {
Serial.print("Button: ");
Serial.println(button);
}
//Button Commands
if (button == 1) {
moveForward();
}
if(button == 2) {
moveBackward();
}
if(button == 3) {
turnRight();
}
if(button == 4) {
turnLeft();
}
if(button == 5) {
moveTest();
}
if (button == 0) {
stopMovement();
}
// Display slider data when slider moves
if (sliderId != -1) {
Serial.print("Slider ID: ");
Serial.print(sliderId);
Serial.print("\tValue: ");
Serial.println(sliderVal);
speedSlider();
}
// Display throttle and steering data if steering or throttle value is changed
if (prevThrottle != throttle || prevSteering != steering) {
Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\tSteering: "); Serial.println(steering);
prevThrottle = throttle;
prevSteering = steering;
}
// If a text from the phone was sent print it to the serial monitor
if (str != "") {
Serial.println(str);
}
// Send string from serial command line to the phone. This will alert the user.
if (Serial.available()) {
Serial.write("send: ");
String str = Serial.readString();
phone.sendMessage(str); // phone.sendMessage(str) sends the text to the phone.
Serial.print(str);
Serial.write('\n');
}
//Run stepper motor commands (Move the stepper motors)
topLeftWheel.runSpeed();
topRightWheel.runSpeed();
backLeftWheel.runSpeed();
backRightWheel.runSpeed();
}
//Movement Command Sub-Functions (Stepper spins counterclockwise looking directly at motor)
void moveForward() {
topLeftWheel.setSpeed(wheelSpeed);
topRightWheel.setSpeed(-wheelSpeed);
backLeftWheel.setSpeed(wheelSpeed);
backRightWheel.setSpeed(-wheelSpeed);
}
void moveBackward() {
topLeftWheel.setSpeed(-wheelSpeed);
topRightWheel.setSpeed(wheelSpeed);
backLeftWheel.setSpeed(-wheelSpeed);
backRightWheel.setSpeed(wheelSpeed);
}
void turnLeft() {
topLeftWheel.setSpeed(0);
topRightWheel.setSpeed(-wheelSpeed);
backLeftWheel.setSpeed(0);
backRightWheel.setSpeed(-wheelSpeed);
}
void turnRight() {
topLeftWheel.setSpeed(wheelSpeed);
topRightWheel.setSpeed(0);
backLeftWheel.setSpeed(wheelSpeed);
backRightWheel.setSpeed(0);
}
void stopMovement() {
topLeftWheel.setSpeed(0);
topRightWheel.setSpeed(0);
backLeftWheel.setSpeed(0);
backRightWheel.setSpeed(0);
}
void speedSlider() {
if (sliderId == 100) {
wheelSpeed = sliderVal * 5;
}
topLeftWheel.setSpeed(wheelSpeed);
topRightWheel.setSpeed(-wheelSpeed);
backLeftWheel.setSpeed(wheelSpeed);
backRightWheel.setSpeed(-wheelSpeed);
Serial.print("WheelSpeed: ");
Serial.println(wheelSpeed);
}
//The subfunction I need help to move all steppers to set position
void moveTest() {
topLeftWheel.moveTo(50);
topLeftWheel.setSpeed(wheelSpeed);
topRightWheel.moveTo(50);
topRightWheel.setSpeed(wheelSpeed);
backLeftWheel.moveTo(50);
backLeftWheel.setSpeed(wheelSpeed);
backRightWheel.moveTo(50);
backRightWheel.setSpeed(wheelSpeed);
while (topLeftWheel.currentPosition() != topLeftWheel.targetPosition()) {
topLeftWheel.runSpeedToPosition();
topRightWheel.runSpeedToPosition();
backLeftWheel.runSpeedToPosition();
backRightWheel.runSpeedToPosition();
}
}