Stepper control via bluetooth and android app.

#include <NeoSWSerial.h> 
#include <AccelStepper.h>
#define motorInterfaceType 1
#define dirPin 7
#define stepPin 6

NeoSWSerial Bluetooth(10, 11);
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int Joint1[50];
int speedDelay = 20;
int index = 0;    //index corresonding to the robot position
int dataIn, tempdataIn=0;
int m = 0;
int tempSingle=1;
const int stepsPerRevolution = 1; // stepper speed
String state = ""; 
int currentHeading=0;
int currentAngle=0;
int lastAngle=0;
int angle=0;
int rotate=0;
int runContinuously=0;
String mode = "Manual";
boolean dirRotation = HIGH;
int rotSpeed = 1500;

void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);     
  Bluetooth.begin(9600); 
  Bluetooth.setTimeout(5);  
  delay(20);   
      
}
//App buttons:
//1 - Save
//2 - Reset
//3 - AUTO
//4 - Pause
//10 - Stepper 1 DOWN
//11 - Stepper 1 UP


void loop() {
  delayMicroseconds(1);
  if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.read();
  if (tempdataIn != dataIn) {
      tempdataIn = dataIn;
      m = dataIn;
    }
  while (m == 11) {
      if (Bluetooth.available() > 0) {
        m = Bluetooth.read();
      }  
      digitalWrite(dirPin, HIGH);     
    
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(4000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(4000);
  }
  
  delay(10);
}

 while (m == 10) {
      if (Bluetooth.available() > 0) {
        m = Bluetooth.read();
      }        
    digitalWrite(dirPin, LOW);
 for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(4000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(4000);
  }
  
  delay(10); // Wait a second
 }
//Works till here:------------------------------------------------------------------------
      
      // If arm speed slider is changed
    if (dataIn > 50 & dataIn < 150) {
      speedDelay = dataIn / 10; // Change servo speed (delay time)
    }    
//Definetly needs changes here:------------------------------------------------------------------------     
    // If button "SAVE" is pressed
    if (m == 1) {     
      // Needs something like this: Joint1[index] = curPos1;       
      // With servo it was: servo1SP[index] = servo1PPos;
     
                                 // make tempSingle max possible value, before entering auto mode
                                // tempSingle = curPos1;                                  
      index++;
      }
//Needs changes at least to here:------------------------------------------------------------------------ 

      if (m == 3) { // AUTO button pressed
      auto_mode();
      
      if (dataIn != 3) {    // RESET button pressed                  
        index = 0;  // Index i 0
      }
    }
    
    m = 0;
    }
}

// auto mode activated:
void auto_mode() {
  while (dataIn != 2) {   // Run the steps over and over again until "RESET" button is pressed
    for (int i = 0; i <= index - 2; i++) {  // Run through all steps(index)
      if (Bluetooth.available() > 0) {      // Check for incomding data
        dataIn = Bluetooth.read();
        if ( dataIn == 4) {           // If button "PAUSE" is pressed
          while (dataIn != 3) {         // Wait until "AUTO" is pressed again
            if (Bluetooth.available() > 0) {
              dataIn = Bluetooth.read();
              if ( dataIn == 2) {
                break;
              }
            }
          }
        }             
      }   
     }       
    }
  }

At the moment I'm building robot 6 axis. With most servo motors and 1 NEMA17 stepper. No problems with servos, just can't figure out NEMA stepper.
I manage to make it work through a bluetooth app. But just in manual mode. Can someone help me with auto mode please? I simplified the code, that it will be simple to understand. With explanations. The problem is that I want to save positions and then run them repeatedly. Since my NEMA drives clockwise and counter clockwise manually. I don't know how to save stepper driver positions to run it on AUTO mode. Maybe someone can help me?

I would suggest checking out this tutorial: several things at the same time and re-factor your code and get rid of all those while() loops and for() loops. It will make it much easier to control.

Your auto_mode() is getting stuck checking for
while (dataIn != 3)
Which blocks the rest of the loop from running
Break your code into two (or more) parts,
i) read and process the bluetooth input
ii) act on the commands just received

See my tutorial on Multi-tasking in Arduino
Which includes a stepper motor control from bluetooth example (among others)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.