Hi! I am trying to drive 2 stepper motors to move independently when a command is sent via bluetooth through Matlab (Example: "a" for just stepper 1, "b" for just stepper 2 or "c" for both).
I have two 28BYJ-48 stepper motors, each connected via ULN2003 Stepper Motor Driver Module to an Arduino Uno board. Additionally, attached to this Arduino Uno board is the BlueSmirf bluetooth silver module to send a command from Matlab to the Arduino serial monitor.
The serial monitor prints what character is transmitted from Matlab via bluetooth, but the serial monitor doesn't communicate this command to the stepper motors so they do not move. When I type the character into the command line of the serial monitor, the corresponding stepper moves. (png image attached for clarification)
#include <SoftwareSerial.h>
//BLUETOOTH
int bluetoothTx = 13; // TX-O pin of bluetooth mate, Arduino D12
int bluetoothRx = 12; // RX-I pin of bluetooth mate, Arduino D13
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
//STEPPER MOTOR
#include <AccelStepper.h>
AccelStepper stepper1(AccelStepper::FULL4WIRE, 2, 4, 3, 5); // Pins for Stepper 1
AccelStepper stepper2(AccelStepper::FULL4WIRE, 6, 8, 7, 9); // Pins for Stepper 2
int start_var = 0;
int movement = 500; // number of steps
char inBuffer ="";
long newStepper1Pos = 0; // variable for the new stepper position for variable 1
long newStepper2Pos = 0; // variable for the new stepper position for variable 2
void setup()
{
//BLUETOOTH
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
bluetooth.begin(9600); // Start bluetooth serial at 9600
//STEPPER MOTOR
{
stepper1.setMaxSpeed(200); //Sets the max speed for stepper 1
stepper1.setAcceleration(100); //Sets the acceleration for stepper 1
stepper1.moveTo(movement);
stepper2.setMaxSpeed(200); //Sets the max speed for stepper 2
stepper2.setAcceleration(100); //Sets the acceleration for stepper 2
stepper2.moveTo(movement);
}
}
void loop() {
{
if(bluetooth.available())
{
Serial.print((char)bluetooth.read());
inBuffer = (char)bluetooth.read();
}
if(Serial.available()>0)
{
bluetooth.print((char)Serial.read());
}
if (inBuffer == 'a')
{
Serial.println(inBuffer);
if (stepper1.distanceToGo() == 0) {
if (stepper1.currentPosition() == 0) {
stepper1.moveTo(movement);
}
else {
stepper1.moveTo(0);
}
}
}
if (inBuffer == 'b')
{
Serial.println(inBuffer);
if (stepper2.distanceToGo() == 0) {
if (stepper2.currentPosition() == 0) {
stepper2.moveTo(movement);
}
else {
stepper2.moveTo(0);
}
}
}
if (inBuffer == 'c')
{
Serial.println(inBuffer);
if (stepper2.distanceToGo() == 0) {
if (stepper2.currentPosition() == 0) {
stepper2.moveTo(movement);
}
else {
stepper2.moveTo(0);
}
}
if (stepper1.distanceToGo() == 0) {
if (stepper1.currentPosition() == 0) {
stepper1.moveTo(movement);
}
else {
stepper1.moveTo(0);
}
}
}
inBuffer = ""; // clear this immediately once the 'a' or 'b' or 'c' has been used
}
stepper1.run();
stepper2.run();
}
I think my problem is either using the incorrect variables and values
int start_var = 0;
int movement = 500; // number of steps
char inBuffer ="";
or the incorrect type of serial communication call-out to properly communicate from Bluetooth > Serial monitor > Stepper motors.
{
if(bluetooth.available())
{
Serial.print((char)bluetooth.read());
inBuffer = (char)bluetooth.read();
}
if(Serial.available()>0)
{
bluetooth.print((char)Serial.read());
}
Thank you in advanced for your help! ![]()
AccelStepper Library- AccelStepper: File List
